Java POI Excel 折线图生成 Word 文档教程

本教程将指导您使用 Java POI 库从 Excel 数据创建折线图并将其插入 Word 文档。

步骤:

  1. 导入必要库:
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.*;
  1. 读取 Excel 数据:
// 读取 Excel 文件
FileInputStream fileIn = new FileInputStream('巴新EDEVU水库数据处理表.xlsx');
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet1 = workbook.getSheet('P9');
XSSFSheet sheet2 = workbook.getSheet('P10');
  1. 创建折线图:
// 创建折线图
XDDFChart chart = createChart(sheet1, sheet2);
  1. 创建 Word 文档:
// 创建 Word 文档
XWPFDocument document = new XWPFDocument();
  1. 将折线图添加到 Word 文档:
// 添加折线图到文档
addChartToDocument(document, chart);
  1. 保存 Word 文档:
// 保存 Word 文档
FileOutputStream fileOut = new FileOutputStream('温度.docx');
document.write(fileOut);
fileOut.close();

完整代码:

public class WordChartExample {

    public static void main(String[] args) {
        try {
            // 读取 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 文档中。');

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

    private static XDDFChart createChart(XSSFSheet sheet1, XSSFSheet sheet2) {
        // 创建折线图
        XSSFWorkbook workbook = sheet1.getWorkbook();
        XSSFDrawing drawing = sheet2.createDrawingPatriarch();
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 9, 9, 19);
        XSSFChart chart = drawing.createChart(anchor);
        chart.setTitleText('数据折线图');
        chart.setTitleOverlay(false);

        // 设置图例位置
        XDDFChartLegend legend = chart.getOrAddLegend();
        legend.setPosition(LegendPosition.BOTTOM);

        // 创建数据系列
        XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
        XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        // 创建数据源
        XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 0, 0));
        XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 2, 2));
        XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 0, 0));
        XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 2, 2));

        // 添加数据系列
        XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
        XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);
        series1.setTitle('P9数据折线图', null);
        XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2);
        series2.setTitle('P10数据折线图', null);

        // 绘制图表
        chart.plot(data);

        return chart;
    }

    private static void addChartToDocument(XWPFDocument document, XDDFChart chart) throws IOException {
        // 创建一个空白段落
        XWPFParagraph paragraph = document.createParagraph();

        // 创建一个新的图表段落
        XWPFRun run = paragraph.createRun();
        run.addBreak();
        run.setText('数据折线图');
        run.addBreak();

        // 创建图表图片
        ByteArrayOutputStream chartOut = new ByteArrayOutputStream();
        chart.plot(chartOut);
        chartOut.close();

        // 将图表图片插入到 Word 文档中
        XWPFRun chartRun = paragraph.createRun();
        chartRun.addPicture(new ByteArrayInputStream(chartOut.toByteArray()), XWPFDocument.PICTURE_TYPE_PNG, 'chart.png', Units.toEMU(500), Units.toEMU(300));
    }
}

注意:

  • 请根据您的实际情况修改代码中的 Excel 文件路径和 Word 文档文件名。
  • 以上代码示例仅供参考,您可以根据需要调整图表类型、数据源和样式等参数。
  • 确保您的项目中已包含 Apache POI 库。

希望本教程能帮助您使用 Java POI 创建 Excel 折线图并将其插入 Word 文档。


原文地址: https://www.cveoy.top/t/topic/f4ip 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录