Java 使用 Apache POI 生成 Excel 折线图并插入 Word 文档
将代码中的main方法的声明修改为throws InvalidFormatException, IOException,即可解决该异常未处理的错误。
修改后的代码如下:
public static void main(String[] args) throws InvalidFormatException, IOException {
// 读取Excel文件
FileInputStream fileIn = new FileInputStream("巴新EDEVU水库数据处理表.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet1 = workbook.getSheet("P9");
XSSFSheet sheet2 = workbook.getSheet("P10");
// 创建折线图
XDDFChart chart = createChart(sheet1, sheet2);
// 创建Word文档
XWPFDocument document = new XWPFDocument();
// 添加折线图到文档
addChartToDocument(document, chart);
// 保存Word文档
FileOutputStream fileOut = new FileOutputStream("温度.docx");
document.write(fileOut);
fileOut.close();
System.out.println("折线图已创建并保存到Word文档中。");
}
这样修改后,编译器将不再报告未处理的异常错误。
原文地址: https://www.cveoy.top/t/topic/f4ii 著作权归作者所有。请勿转载和采集!