Файл поврежден после создания excel (.xlsx) файл с помощью Apache POI с Java

Я создал книгу / Excel in .формат xlsx с Java с использованием API POI Apache успешно. Мой код, как показано ниже, создается файл с именем " RiponAlWasim.xlsx " в приводе D:

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

когда я попытался открыть " RiponAlWasim.xlsx" было показано, что файл поврежден. Что случилось?

2 ответов


в книгу необходимо добавить хотя бы один лист. Итак, после создания рабочего листа хорошо работает следующий код:

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

я столкнулся с той же проблемой, но я нашел решение. Ниже приведен мой полный код для чтения и записи 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();
}

}