求一个使用POI读取execl2010的完整例子。。
希望能把代码也发上来。。
包括jar包地址。。
我网上搜了好多,但是好多都不行。。。
版本太低还是什么其他的原因,说不清楚。。
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++) {和读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.jar
poi-ooxml-schemas.jar
poi.jar
从官网上把poi相关的都加到项目里.http://poi.apache.org/download
官网也有例子,各种操作,呵呵.