gpt4 book ai didi

java - 如何使用 apache poi 将数据写入现有 Excel

转载 作者:行者123 更新时间:2023-12-02 23:30:45 24 4
gpt4 key购买 nike

我的 Excel 工作表包含 5 行和 2 列。我想在该 Excel 中再添加一列。但是当我使用 WorkbookFactory 时,它显示错误。我导入了 poi-3.8.jar 和 poi-ooxml-3.5- beta5.jar.它给出错误线程“main”java.lang.Error 中出现异常: Unresolved 编译问题: WorkbookFactory 无法解析。请帮助我做什么。

最佳答案

try this


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelExample {

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

try {

FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;

//Update the value of cell
cell = sheet.getRow(1).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(2).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
Row row = sheet.getRow(0);
row.createCell(3).setCellValue("Value 2");

file.close();

FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
workbook.write(outFile);
outFile.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于java - 如何使用 apache poi 将数据写入现有 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15471565/

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