gpt4 book ai didi

java - 打开使用 POI API 创建的文件时 Excel 被损坏

转载 作者:行者123 更新时间:2023-12-01 21:09:06 25 4
gpt4 key购买 nike

public static void createworkbook() throws IOException, InvalidFormatException{

File file = new File("C:\\Workspace\\Selenium_2.53\\src\\InputFiles\\webuyPurchase.xlsx");
file.createNewFile();
fileOut = new FileOutputStream(file);

XSSFWorkbook workbook = new XSSFWorkbook();
workbook.write(fileOut);
XSSFSheet spreadsheet = workbook.createSheet("Products");
String sheetname = workbook.getSheetAt(0).getSheetName();
System.out.println(sheetname);
fileOut.close();
// workbook.createSheet("Products");
System.out.println("workbook is created");
}

执行上述代码时,文件创建成功。但打开时发现文件已损坏。

最佳答案

当您更正语句的顺序后,它应该看起来像这样......
因此无需事先通过 file.createNewFile() 创建文件

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class CreateWorkbook {
public static void main(String args[]) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("Products");
spreadsheet.createRow(0).createCell(0).setCellValue("this is a test");
FileOutputStream fos = new FileOutputStream("bla.xlsx");
workbook.write(fos);
fos.close();
workbook.close();
}
}

关于java - 打开使用 POI API 创建的文件时 Excel 被损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com