【发布时间】:2017-01-26 15:25:05
【问题描述】:
我正在尝试使用 Apache POI 写入 excel。代码(如下)执行良好,但是当我试图打开 excel 时,它显示 excel 内的数据已损坏,无法打开。
Excel 版本:Microsoft Office Excel 2007 和 Microsoft Office Excel 2003(都试过了)
Apache POI 版本:3.6
import java.io.FileOutputStream;
import java.io.IOException;
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 WriteExcel{
public String FilePath;
XSSFWorkbook wb= null;
XSSFSheet ws= null;
XSSFRow xr=null;
XSSFCell xc=null;
FileOutputStream fout = null;
public WriteExcel(String FilePath) throws IOException
{
this.FilePath=FilePath;
fout=new FileOutputStream(FilePath);
wb=new XSSFWorkbook();
wb.write(fout);
}
//Write to a specific Cell
public void writeToCell(String SheetName, int RowNum, int ColNum, String Data) throws IOException
{
ws=wb.createSheet(SheetName);
xr=ws.createRow(RowNum);
xc=xr.createCell(ColNum);
xc.setCellValue(Data);
fout.close();
}
public static void main(String[] args) throws IOException {
WriteExcel WE= new WriteExcel("E://Learning//Learning//SoapUI//TestFiles//Write.xls");
WE.writeToCell("Sheet1", 2, 2, "Pi");
System.out.println("Written");
}
}
我相信代码没问题,因为每次我执行代码时,“修改日期”都会显示最新的时间戳;所以我怀疑 Microsoft Office (Excel) 或 Apache POI 的版本控制可能存在问题。
你能帮忙吗?
【问题讨论】:
标签: java excel apache-poi