求一个使用POI读取execl2010的完整例子。。 求一个使用POI读取execl2010的完整例子。。希望能把代码也发上来。。包括jar包地址。。我网上搜了好多,但是好多都不行版本太低还是什么其他的原因,说不清楚。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 和读excel03并没大区别.只是增加一个Workbook工厂即可import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.WorkbookFactory;public class OperExcel { public static void main(String[] args) throws Exception { InputStream inp = new FileInputStream("d://workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(2); Cell cell = row.getCell(3); if (cell == null) cell = row.createCell(3); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("a test"); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("d://workbook.xlsx"); wb.write(fileOut); fileOut.close(); inp.close(); }poi-ooxml.jarpoi-ooxml-schemas.jarpoi.jar从官网上把poi相关的都加到项目里.http://poi.apache.org/download.html官网也有例子,各种操作,呵呵. 找了一个挺全的例子:public static void main(String[] args) { // 解压Book1.xlsx ZipFile xlsxFile; try { xlsxFile = new ZipFile(new File("d:\\12.xlsx")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 先读取sharedStrings.xml这个文件备用 ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml"); InputStream sharedStringXMLIS = xlsxFile.getInputStream(sharedStringXML); Document sharedString; sharedString = dbf.newDocumentBuilder().parse(sharedStringXMLIS); NodeList str = sharedString.getElementsByTagName("t"); String sharedStrings[] = new String[str.getLength()]; for (int n = 0; n < str.getLength(); n++) { Element element = (Element) str.item(n); sharedStrings[n] = element.getTextContent(); } // 找到解压文件夹里的workbook.xml,此文件中包含了这张工作表中有几个sheet ZipEntry workbookXML = xlsxFile.getEntry("xl/workbook.xml"); InputStream workbookXMLIS = xlsxFile.getInputStream(workbookXML); Document doc = dbf.newDocumentBuilder().parse(workbookXMLIS); // 获取一共有几个sheet NodeList nl = doc.getElementsByTagName("sheet"); for (int i = 0; i < nl.getLength(); i++) { Element element = (Element) nl.item(i);// 将node转化为element,用来得到每个节点的属性 System.out.println(element.getAttribute("name"));// 输出sheet节点的name属性的值 // 接着就要到解压文件夹里找到对应的name值的xml文件,比如在workbook.xml中有<sheet name="Sheet1" // sheetId="1" r:id="rId1" /> 节点 // 那么就可以在解压文件夹里的xl/worksheets下找到sheet1.xml,这个xml文件夹里就是包含的表格的内容 ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/" + element.getAttribute("name").toLowerCase() + ".xml"); InputStream sheetXMLIS = xlsxFile.getInputStream(sheetXML); Document sheetdoc = dbf.newDocumentBuilder().parse(sheetXMLIS); NodeList rowdata = sheetdoc.getElementsByTagName("row"); for (int j = 0; j < rowdata.getLength(); j++) { // 得到每个行 // 行的格式: /* * <row r="1" spans="1:3">r表示第一行,spans表示有几列 <c r="A1" * t="s">//r表示该列的列表 * ,t="s"个人认为是表示这个单元格的内容可以在sharedStrings.xml这个文件里找到,对应的节点 * 下标就是v节点的值,即0,若没有t属性,则v的值就是该单元格的内容 <v>0</v> </c> <c r="B1" * t="s"> <v>1</v> </c> <c r="C1" t="s"> <v>2</v> </c> </row> */ Element row = (Element) rowdata.item(j); // 根据行得到每个行中的列 NodeList columndata = row.getElementsByTagName("c"); for (int k = 0; k < columndata.getLength(); k++) { Element column = (Element) columndata.item(k); NodeList values = column.getElementsByTagName("v"); Element value = (Element) values.item(0); if (column.getAttribute("t") != null & column.getAttribute("t").equals("s")) { // 如果是共享字符串则在sharedstring.xml里查找该列的值 System.out.print(sharedStrings[Integer.parseInt(value.getTextContent())] + " "); } else { if (value != null) { System.out.print(value.getTextContent() + " "); }else { System.out.println("j : " + j + " k : " + k + " null"); } } } System.out.println(); } } } catch (ZipException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } 电驴有视频:《Lucene、webservice、(svn_ant_maven)、DWR、Freeer、POI视频教程》 在eclipse中获取bin目录以外的文件路径问题 程序debug问题 JAVA基础 如何读取sql server中的image字段并转换成字符串? 谁有条件帮我做一下速度测试 急问:jsp往ACCESS添加记录可就是提示找不到驱动 高手指教 java生成word文档的问题 Swing高手请进!!!!!!!!! 求:scjp考试大纲中的一条 看看这是怎么回事? 邮件中附件是否需要保存到服务器? IO 怎么把String里的内容写入到file文件里
import java.io.FileOutputStream;
import java.io.InputStream;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;public class OperExcel { public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream("d://workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test"); // Write the output to a file
FileOutputStream fileOut = new FileOutputStream("d://workbook.xlsx");
wb.write(fileOut);
fileOut.close();
inp.close();
}poi-ooxml.jar
poi-ooxml-schemas.jar
poi.jar
从官网上把poi相关的都加到项目里.http://poi.apache.org/download.html
官网也有例子,各种操作,呵呵.
public static void main(String[] args) {
// 解压Book1.xlsx
ZipFile xlsxFile;
try {
xlsxFile = new ZipFile(new File("d:\\12.xlsx"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 先读取sharedStrings.xml这个文件备用
ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml");
InputStream sharedStringXMLIS = xlsxFile.getInputStream(sharedStringXML);
Document sharedString;
sharedString = dbf.newDocumentBuilder().parse(sharedStringXMLIS);
NodeList str = sharedString.getElementsByTagName("t");
String sharedStrings[] = new String[str.getLength()];
for (int n = 0; n < str.getLength(); n++) {
Element element = (Element) str.item(n);
sharedStrings[n] = element.getTextContent();
}
// 找到解压文件夹里的workbook.xml,此文件中包含了这张工作表中有几个sheet
ZipEntry workbookXML = xlsxFile.getEntry("xl/workbook.xml");
InputStream workbookXMLIS = xlsxFile.getInputStream(workbookXML);
Document doc = dbf.newDocumentBuilder().parse(workbookXMLIS);
// 获取一共有几个sheet
NodeList nl = doc.getElementsByTagName("sheet");
for (int i = 0; i < nl.getLength(); i++) {
Element element = (Element) nl.item(i);// 将node转化为element,用来得到每个节点的属性
System.out.println(element.getAttribute("name"));// 输出sheet节点的name属性的值
// 接着就要到解压文件夹里找到对应的name值的xml文件,比如在workbook.xml中有<sheet name="Sheet1"
// sheetId="1" r:id="rId1" /> 节点
// 那么就可以在解压文件夹里的xl/worksheets下找到sheet1.xml,这个xml文件夹里就是包含的表格的内容
ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/"
+ element.getAttribute("name").toLowerCase() + ".xml");
InputStream sheetXMLIS = xlsxFile.getInputStream(sheetXML);
Document sheetdoc = dbf.newDocumentBuilder().parse(sheetXMLIS);
NodeList rowdata = sheetdoc.getElementsByTagName("row");
for (int j = 0; j < rowdata.getLength(); j++) {
// 得到每个行
// 行的格式:
/*
* <row r="1" spans="1:3">r表示第一行,spans表示有几列 <c r="A1"
* t="s">//r表示该列的列表
* ,t="s"个人认为是表示这个单元格的内容可以在sharedStrings.xml这个文件里找到,对应的节点
* 下标就是v节点的值,即0,若没有t属性,则v的值就是该单元格的内容 <v>0</v> </c> <c r="B1"
* t="s"> <v>1</v> </c> <c r="C1" t="s"> <v>2</v> </c> </row>
*/
Element row = (Element) rowdata.item(j);
// 根据行得到每个行中的列
NodeList columndata = row.getElementsByTagName("c");
for (int k = 0; k < columndata.getLength(); k++) {
Element column = (Element) columndata.item(k);
NodeList values = column.getElementsByTagName("v");
Element value = (Element) values.item(0);
if (column.getAttribute("t") != null
& column.getAttribute("t").equals("s")) {
// 如果是共享字符串则在sharedstring.xml里查找该列的值
System.out.print(sharedStrings[Integer.parseInt(value.getTextContent())] + " ");
} else {
if (value != null) {
System.out.print(value.getTextContent() + " ");
}else {
System.out.println("j : " + j + " k : " + k + " null");
}
}
}
System.out.println();
}
}
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}