使用java写入.xlsx。发生BiffViewer错误

时间:2012-11-18 10:17:47

标签: java apache-poi

我用过的jars:dom4j poi-3.8.jar poi-ooxml-3.8.jar poi-ooxml-schemas-3.8.jar xbean.jar

代码:

package org.capgemini.ui;

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 Exception {      

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("D:/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 (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

错误::

org.apache.poi.hssf.dev.BiffViewer$CommandParseException: Biff viewer needs a filename
 at org.apache.poi.hssf.dev.BiffViewer$CommandArgs.parse(BiffViewer.java:333)
 at org.apache.poi.hssf.dev.BiffViewer.main(BiffViewer.java:386)

1 个答案:

答案 0 :(得分:0)

BiffViewer是HSSF的一部分,它只适用于较旧的.xls文件(基于OLE2)。您正在使用XSSF生成.xlsx文件,这是一种不同的低级格式。

如果你真的想使用BiffViewer(不知道为什么,它通常只用于调试,但仍然),那么你需要将你的XSSF代码更改为HSSF代码。否则,如果您的意思是使用XSSF生成.xlsx文件,则无法使用HSSF调试工具。如果您想知道.xlsx文件中的内容,请将其解压缩(.xlsx是xml文件的zip文件)并查看XML。