有人可以给我一个使用XSSF
来编写xlsx
个文件的示例吗?
编辑:啊哈......我自己做的就是你需要的罐子 -
DOM4J POI-3.8.jar POI-OOXML-3.8.jar POI-OOXML-模式-3.8.jar xbean.jar
示例代码 -
package com.angrydog.impl;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFWorkBookWriter {
public void writeWorkBook(Vector table) throws WorkBookException {
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
table=new Vector();
table.add(new String("Howdy"));
table.add(new java.sql.Date(2012,06,20));
table.add(new Double(13.35D));
table.add(new String("Fine"));
table.add(new java.sql.Date(2012,06,20));
table.add(new Double(13.38D));
Iterator rows=table.iterator();
Enumeration rowsOfVector=table.elements();
int totalNoOfRows=table.size()/2;
int currentRow=0;
while (rows.hasNext () && currentRow<totalNoOfRows){
XSSFRow row = sheet.createRow(currentRow++);
for (int i = 0; i < 3; i++) {
XSSFCell cell=row.createCell(i);
Object val=rows.next();
if( val instanceof String){
cell.setCellValue(val.toString());
}
else if(val instanceof Date){
cell.setCellValue((java.sql.Date)val);
}
else if(val instanceof Double){
cell.setCellValue((Double)val);
}
}
}
FileOutputStream outPutStream = null;
try {
outPutStream = new FileOutputStream("C:\\\\"+"Try.xlsx");
workBook.write(outPutStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outPutStream != null) {
try {
outPutStream.flush();
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
XSSFWorkBookWriter bookWriter=new XSSFWorkBookWriter();
try {
bookWriter.writeWorkBook(new Vector());
} catch (WorkBookException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}