问题描述
2003能正常读取到里面的内容,但是,2007为什么读出来的全是像xml的内容。求解! 问题补充:blessed24 写道
解决方案
这是我前段时间写的代码,在jdk1.4也能用。你可以参考下 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; }
解决方案二:
呃 不过发现上面的poi3.7 rc1好像不支持office2007 晕试试这个poi3.7 rc2http://zh.sourceforge.jp/projects/poi-jdk14/releases/51911
解决方案三:
JDK1.4有的 试试http://download.csdn.net/detail/ch1986324/3379041http://zh.sourceforge.jp/projects/poi-jdk14/downloads/51695/poi-3.7-jdk1.4-20110417-rc1.jar/
解决方案四:
推荐使用http://poi.apache.org/简单,容易,demo全,祝你好运!
解决方案五:
用POI啊,很好用的,我一直用他
解决方案六:
你是用什么读的?有很多第三方库的啊 jxl poi,poi比较犀利
解决方案七:
office 2007的结构跟2003是不一样的,office2007可以用winrar打开,里面就是许多xml文档组成的,你可以打开看看