Java读取Excel2007

2003能正常读取到里面的内容,但是,2007为什么读出来的全是像xml的内容。求解!

7个回答

这是我前段时间写的代码,在jdk1.4也能用。你可以参考下

[code="java"] public String readExcel(String url) {
String content = "";
ZipFile xlsxFile = null;
HttpURLConnection conn = null;
BufferedReader br = null;
InputStream is = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
is = conn.getInputStream();
//1.先将2007文件临时保存到本地硬盘中
String tempfilename = "D:\xlsx_2007.xlsx";
FileOutputStream fos = new FileOutputStream(tempfilename);
byte[] bytes = new byte[8 * 1204];
int length = -1;
while ( (length = is.read(bytes)) != -1) {
fos.write(bytes, 0, length);
}
//2.再将文件中硬盘中读取出来
xlsxFile = new ZipFile(new File(tempfilename));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//先读取sharedStrings.xml这个文件备用
ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml");
InputStream sharedStringXMLIS = xlsxFile.getInputStream(sharedStringXML);
org.w3c.dom.Document sharedString = dbf.newDocumentBuilder().parse(sharedStringXMLIS);
org.w3c.dom.NodeList str = sharedString.getElementsByTagName("t");
String sharedStrings[] = new String[str.getLength()];
for (int n = 0; n < str.getLength(); n++) {
org.w3c.dom.Element element = (org.w3c.dom.Element) str.item(n);
if(element.getFirstChild() != null){
sharedStrings[n] = element.getFirstChild().getNodeValue();
content = content + sharedStrings[n];
}
}
//找到解压文件夹里的workbook.xml,此文件中包含了这张工作表中有几个sheet
ZipEntry workbookXML = xlsxFile.getEntry("xl/workbook.xml");
InputStream workbookXMLIS = xlsxFile.getInputStream(workbookXML);
org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(workbookXMLIS);
//获取一共有几个sheet
org.w3c.dom.NodeList nl = doc.getElementsByTagName("sheet");
for (int i = 0; i < nl.getLength(); i++) {
org.w3c.dom.Element element = (org.w3c.dom.Element) nl.item(i); //将node转化为element,用来得到每个节点的属性
content = content + element.getAttribute("name");
ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet" + (i +1) +".xml");
InputStream sheetXMLIS = xlsxFile.getInputStream(sheetXML);
org.w3c.dom.Document sheetdoc = dbf.newDocumentBuilder().parse(sheetXMLIS);
org.w3c.dom.NodeList rowdata = sheetdoc.getElementsByTagName("row");
for (int j = 1; j < rowdata.getLength(); j++) {
org.w3c.dom.Element row = (org.w3c.dom.Element) rowdata.item(j);
//根据行得到每个行中的列
org.w3c.dom.NodeList columndata = row.getElementsByTagName("c");
List tempList = new ArrayList(); ///放置页面需要导入的数据
for (int k = 0; k < columndata.getLength(); k++) {
org.w3c.dom.Element column = (org.w3c.dom.Element) columndata.item(k);
org.w3c.dom.NodeList values = column.getElementsByTagName("v");
org.w3c.dom.Element value = (org.w3c.dom.Element) values.item(0);
if (column.getAttribute("t") != null & column.getAttribute("t").equals("s")) {
//如果是共享字符串则在sharedstring.xml里查找该列的值
String c = value.getFirstChild().getNodeValue();
content = content + c;
}
else {
//System.out.println(value.getFirstChild().getNodeValue());
}
}
}
}
Pattern p = Pattern.compile("\s*|\t|\r|\n");
Matcher m = p.matcher(content);
content = m.replaceAll("");
}
catch (Exception ex) {
}finally{
if(is != null){
try {
is.close();
}
catch (IOException ex1) {
}
is = null;
}
if(conn != null){
conn.disconnect();
}
}
return content;
}[/code]

office 2007的结构跟2003是不一样的,office2007可以用winrar打开,里面就是许多xml文档组成的,你可以打开看看

你是用什么读的?有很多第三方库的啊 jxl poi,poi比较犀利

用POI啊,很好用的,我一直用他

推荐使用[code="java"]http://poi.apache.org/[/code]
简单,容易,demo全,祝你好运!

呃 不过发现上面的poi3.7 rc1好像不支持office2007 晕
试试这个poi3.7 rc2
[url]http://zh.sourceforge.jp/projects/poi-jdk14/releases/51911[/url]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐