package 折线图;import orgapachepoiopenxml4jexceptionsInvalidFormatException;import orgapachepoissutilCellRangeAddress;import orgapachepoiutilUnits;import orgapachepoixddfusermodelchart;import orgapachepo
折线图代码如下:
package 折线图;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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");
// 创建折线图
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);
// 创建Word文档
XWPFDocument document = new XWPFDocument();
// 创建段落
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
// 添加图表到段落
XWPFRun run = paragraph.createRun();
String fileName = "温度.docx";
// 将图表写入临时文件
String tempFileName = "temp.xlsx";
FileOutputStream tempFileOut = new FileOutputStream(tempFileName);
workbook.write(tempFileOut);
tempFileOut.close();
// 将临时文件中的图表添加到Word文档中
FileInputStream tempFileIn = new FileInputStream(tempFileName);
run.addPicture(tempFileIn, XWPFDocument.PICTURE_TYPE_EMF, fileName, Units.toEMU(400), Units.toEMU(300));
tempFileIn.close();
// 删除临时文件
File tempFile = new File(tempFileName);
tempFile.delete();
// 保存Word文档
FileOutputStream fileOut = new FileOutputStream(fileName);
document.write(fileOut);
fileOut.close();
System.out.println("折线图已创建并保存到Word文档中。");
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}
请确保在运行代码之前已经将名为“巴新EDEVU水库数据处理表.xlsx”的文件放置在正确的路径下,并且文件中包含名为“P9”和“P10”的工作表。
运行代码后,将生成一个名为“温度.docx”的Word文档,其中包含了根据指定的数据绘制的折线图。
原文地址: https://www.cveoy.top/t/topic/hDxM 著作权归作者所有。请勿转载和采集!