繁体   English   中英

在Apache POI中创建Excel 2007文件后损坏

[英]Excel 2007 file corrupted after being created in Apache POI

我使用NetBeans8。编译以下简单代码后出现问题:

package file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class File {

public static void main(String[] args) throws FileNotFoundException, IOException 

{   Workbook wb = new XSSFWorkbook();

    String name = "charlie.xlsx";

    FileOutputStream fileOut = new FileOutputStream(name);

    wb.write(fileOut);
    fileOut.close();

我是Java的新手,所以基本上我是从Apache POI文档重写了该代码,试图了解它是如何工作的。 好吧-它运行良好,直到我尝试在MS Excel中打开输出文件为止-因为然后我收到一条消息,指出文件无法打开,因为文件已损坏。

什么地方出了错?

您需要创建一个工作表。 将此添加到您的代码中,它将起作用。

wb.createSheet("Test1");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM