使用带有 Java 的 Apache POI 创建 excel (.xlsx) 文件后文件已损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33148355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:15:06  来源:igfitidea点击:

File is corrupted after creating excel (.xlsx) file by using Apache POI with Java

javaexcelapache-poi

提问by Ripon Al Wasim

I have created a Workbook/Excel in .xlsx format with Java using Apache POI API successfully. My code is as below that is created a file named "RiponAlWasim.xlsx" in D drive:

我已经成功地使用 Apache POI API 用 Ja​​va 创建了一个 .xlsx 格式的工作簿/Excel。我的代码如下,在 D 盘中创建了一个名为“RiponAlWasim.xlsx”的文件:

Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("D:\RiponAlWasim.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();

When I tried to open "RiponAlWasim.xlsx" it was shown the file is corrupted. What's the wrong?

当我尝试打开“RiponAlWasim.xlsx”时,显示文件已损坏。怎么了?

回答by Ripon Al Wasim

It needs to be added at least one sheet to the workbook. So, after creating a worksheet the following code is working well:

需要将至少一张工作表添加到工作簿中。因此,在创建工作表后,以下代码运行良好:

Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("Ripon");
FileOutputStream fileOut = new FileOutputStream("D:\RiponAlWasim.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();

回答by Abdul Samad

I was facing the same issue but I found the solution. Below are my complete code to read and write the excel without getting corrupt of file.

我遇到了同样的问题,但我找到了解决方案。以下是我在不损坏文件的情况下读取和写入 excel 的完整代码。

package utility;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 ExcelPOI {

private static File file;

public ExcelPOI(String path){
    file = new File(path);

     if (!file.exists()) {
         System.out.println("File does not exist.");
                file=null;
        }
}

public String getText(String sheetName, int row, int col) throws Exception
{
    InputStream ExcelFileToRead = new FileInputStream(file);
    String Value;
    XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
    XSSFSheet sheet=wb.getSheet(sheetName);

    if(row>=0 && col>=0){
        try{
            Value = sheet.getRow(row).getCell(col).getStringCellValue();
            ExcelFileToRead.close();
            wb.close();
        }catch(Exception e){
            System.out.println("Row or Cell not created.");
            ExcelFileToRead.close();
            wb.close();
            return null;
        }

        return Value;

    }else{
        System.out.println("Plz Enter a positive Row and Column");

    }
    ExcelFileToRead.close();
    wb.close();
    return null;

}

public void setText(String sheetName, int rowNum, int col, String value) throws IOException, InvalidFormatException {

    FileInputStream fio = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fio);
    XSSFSheet sheet = wb.getSheet(sheetName);
    XSSFRow row;

    if(rowNum>=0 && col>=0){
        try{
            row = sheet.getRow(rowNum);
            XSSFCell cell = row.createCell(col);
            cell.setCellValue(value);
        }catch(Exception e){
            System.out.println("Row Creation Required..");
            row = sheet.createRow(rowNum);
            XSSFCell cell = row.createCell(col);
            cell.setCellValue(value);
        }

    }else{
        System.out.println("Plz Enter a positive Row and Column");
    }

    fio.close();

    FileOutputStream  fileOut = new FileOutputStream(file);

    //write this workbook to an Outputstream.
    wb.write(fileOut);
    wb.close();
    fileOut.flush();
    fileOut.close();
}

}