无法打开 excel 文件。引发 "Unreadable format" 错误

Unable to open excel file. Throws "Unreadable format" error

我实际上是在尝试将 String[] 复制到 excel 中。代码是这样的。

    int ExRows = 2, ExCols = 2;
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet xs = wb.createSheet("Sheet");
    try {
            String[] inputArr = {"Gender", "Age", "Male", "45"};              
            int val =0;
            for(int i =0;i<ExRows;i++){
                XSSFRow row = xs.createRow(i);
                for(int j=0;j<ExCols;j++){
                    FileOutputStream DestFile = new FileOutputStream("H:\Docs\DestExcelRecord\DestMedicalRecord.xls");
                    String str = inputArr[val];                        
                    row.createCell(j).setCellValue(str);                        
                    wb.write(DestFile);
                    val= val+1;
                    DestFile.close();
                }                   
            }
        }


When I try opening the file, this is the first error I get, when I click on YES, I get the second error

我到底错过了什么?

首先,解决了一些问题。 (我不想编辑你的问题)

对了,你用PIO吗API? Hello Word 应用程序是否有效?

int exRows = 2, exCols = 2;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet xs = wb.createSheet("Sheet");
String input;
FileOutputStream destFile; // declare out of try
try {
        destFile = new FileOutputStream("H:\Docs\DestExcelRecord\DestMedicalRecord.xlsx");
        String[] inputArr = {"Gender", "Age", "Male", "45"};              
        int val =0;
        for(int i =0;i<exRows;i++){
            XSSFRow row = xs.createRow(i);
            for(int j=0;j<exCols;j++){
                String str = inputArr[i];                        
                row.createCell(j).setCellValue(str);                        

                val +=val; // is it val += 1;?
            }                   
        }
        wb.write(destFile);
    } finally {
        destFile.close(); // close in finally
    }

你的代码适用于 POI 3.11 和 MS Office 2013。尽管它给出了与你的相同的警告。但是,使用 SXSSFWorkbook 会使它更容易。 org.apache.poi 点 3.11 org.apache.poi poi-ooxml 3.11