echart统计图导出到word工具类

package com.cnpc.dqhse.hazardcontrol.util.word;

import com.alibaba.fastjson.JSONArray;

import com.cnpc.dqhse.hazardcontrol.util.word.entity.ChartData;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.ss.usermodel.VerticalAlignment;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.util.Units;

import org.apache.poi.xddf.usermodel.XDDFColor;

import org.apache.poi.xddf.usermodel.XDDFShapeProperties;

import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;

import org.apache.poi.xddf.usermodel.chart.*;

import org.apache.poi.xddf.usermodel.text.XDDFRunProperties;

import org.apache.poi.xddf.usermodel.text.XDDFTextBody;

import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTDLbls;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTManualLayout;

import org.openxmlformats.schemas.drawingml.x2006.chart.STLayoutMode;

import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;

import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;

import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;

import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.*;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xwpf.usermodel.XWPFDocument;

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

public class ChartUtils {

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

// createLineChart("D:\\导出测试\\lineChart1.docx");

// createBarChart("D:\\导出测试\\lineChart2.docx");

// createPieChart("D:\\导出测试\\lineChart3.docx");

// 1、创建word文档对象

XWPFDocument document = new XWPFDocument();

// 2、创建chart图表对象,抛出异常

XWPFChart chart1 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

XWPFChart chart2 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

XWPFChart chart3 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

XWPFChart chart4 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

XWPFChart chart5 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

XWPFChart chart6 = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

JSONArray data = JSONArray.parseArray("[{\"ORG_ID\":\"00054620\",\"ORG_NAME\":\"机构1\",\"HIDDENNUMS\":10,\"EJDW\":1,\"TGXC\":4,\"JDJYSYH\":3,\"JDYJS\":5,\"HJBHWT\":3,\"SJDW\":1,\"XCZS\":3},{\"ORG_ID\":\"00054621\",\"ORG_NAME\":\"机构2\",\"HIDDENNUMS\":0,\"EJDW\":0,\"TGXC\":0,\"JDJYSYH\":0,\"JDYJS\":0,\"HJBHWT\":0,\"SJDW\":0,\"XCZS\":0},{\"ORG_ID\":\"00054622\",\"ORG_NAME\":\"机构3\",\"HIDDENNUMS\":0,\"EJDW\":0,\"TGXC\":0,\"JDJYSYH\":0,\"JDYJS\":0,\"HJBHWT\":0,\"SJDW\":0,\"XCZS\":0},{\"ORG_ID\":\"00054623\",\"ORG_NAME\":\"机构4\",\"HIDDENNUMS\":0,\"EJDW\":0,\"TGXC\":0,\"JDJYSYH\":0,\"JDYJS\":0,\"HJBHWT\":0,\"SJDW\":0,\"XCZS\":0},{\"ORG_ID\":\"00054624\",\"ORG_NAME\":\"机构5\",\"HIDDENNUMS\":0,\"EJDW\":0,\"TGXC\":0,\"JDJYSYH\":0,\"JDYJS\":0,\"HJBHWT\":0,\"SJDW\":0,\"XCZS\":0},{\"ORG_NAME\":\"总计\",\"HIDDENNUMS\":10,\"EJDW\":1,\"TGXC\":4,\"JDJYSYH\":3,\"JDYJS\":5,\"HJBHWT\":3,\"SJDW\":1,\"XCZS\":3}]");

List<Map<String, Object>> data1 = new ArrayList<>();

for (Object datum : data) {

data1.add((Map<String, Object>) datum);

}

ChartData chartData = createChartDate(data1);

chartData.setPieValues(new Double[]{15.2, 152.3, 32.0, 112.0, 554.22});

createPieChart(chart1, chartData, true, 10.0, false);

createBarChart(chart2, chartData, false, true);

createBarChart(chart3, chartData, true, true);

createBarChart(chart4, chartData, false, false);

createBarChart(chart5, chartData, true, false);

// createBarChart(chart2, chartData, true, true, 0.005, 5.0, true, 8.0, true, 10.0, false, 0.9, false);

// createBarChart(chart3, chartData, true, true, 0.005, 5.0, true, 8.0, true, 10.0, false, 0.9, true);

createBarLineChart(chart6, chartData);

// 10、输出到word文档

FileOutputStream fos = new FileOutputStream("D:\\导出测试\\lineChart6.docx");

document.write(fos); // 导出word

// 11、关闭流

fos.close();

document.close();

}

public static void createLineChart(String filePath) throws Exception {

// 1、创建word文档对象

XWPFDocument document = new XWPFDocument();

// 2、创建chart图表对象,抛出异常

XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

// 3、图表相关设置

chart.setTitleText("使用POI创建的折线图"); // 图表标题

chart.setTitleOverlay(false); // 图例是否覆盖标题

// 4、图例设置

XDDFChartLegend legend = chart.getOrAddLegend();

legend.setPosition(LegendPosition.TOP); // 图例位置:上下左右

// 5、X轴(分类轴)相关设置

XDDFCategoryAxis xAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); // 创建X轴,并且指定位置

xAxis.setTitle("日期(年月)"); // x轴标题

String[] xAxisData = new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12",};

XDDFCategoryDataSource xAxisSource = XDDFDataSourcesFactory.fromArray(xAxisData); // 设置X轴数据

// 6、Y轴(值轴)相关设置

XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT); // 创建Y轴,指定位置

yAxis.setTitle("粉丝数(个)"); // Y轴标题

Integer[] yAxisData = new Integer[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57};

XDDFNumericalDataSource<Integer> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置Y轴数据

// 7、创建折线图对象

XDDFLineChartData lineChart = (XDDFLineChartData) chart.createData(ChartTypes.LINE, xAxis, yAxis);

// 8、加载折线图数据集

XDDFLineChartData.Series lineSeries = (XDDFLineChartData.Series) lineChart.addSeries(xAxisSource, yAxisSource);

lineSeries.setTitle("粉丝数", null); // 图例标题

lineSeries.setSmooth(true); // 线条样式:true平滑曲线,false折线

lineSeries.setMarkerSize((short) 6); // 标记点大小

lineSeries.setMarkerStyle(MarkerStyle.CIRCLE); // 标记点样式

// 9、绘制折线图

chart.plot(lineChart);

// 10、输出到word文档

FileOutputStream fos = new FileOutputStream(filePath);

document.write(fos); // 导出word

// 11、关闭流

fos.close();

document.close();

}

public static void createBarChart(String filePath) throws Exception {

// 1、创建word文档对象

XWPFDocument document = new XWPFDocument();

// 2、创建chart图表对象,抛出异常

XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

// 3、图表相关设置

chart.setTitleText("使用POI创建的柱状图"); // 图表标题

chart.setTitleOverlay(false); // 图例是否覆盖标题

// 4、图例设置

XDDFChartLegend legend = chart.getOrAddLegend();

legend.setPosition(LegendPosition.TOP); // 图例位置:上下左右

// 5、X轴(分类轴)相关设置

XDDFCategoryAxis xAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); // 创建X轴,并且指定位置

xAxis.setTitle("日期(年月)"); // x轴标题

String[] xAxisData = new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12",};

XDDFCategoryDataSource xAxisSource = XDDFDataSourcesFactory.fromArray(xAxisData); // 设置X轴数据

// 6、Y轴(值轴)相关设置

XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT); // 创建Y轴,指定位置

yAxis.setTitle("粉丝数(个)"); // Y轴标题

yAxis.setCrossBetween(AxisCrossBetween.BETWEEN); // 设置图柱的位置:BETWEEN居中

Integer[] yAxisData = new Integer[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57};

XDDFNumericalDataSource<Integer> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置Y轴数据

// 7、创建柱状图对象

XDDFBarChartData barChart = (XDDFBarChartData) chart.createData(ChartTypes.BAR, xAxis, yAxis);

barChart.setBarDirection(BarDirection.COL); // 设置柱状图的方向:BAR横向,COL竖向,默认是BAR

// 8、加载柱状图数据集

XDDFBarChartData.Series barSeries = (XDDFBarChartData.Series) barChart.addSeries(xAxisSource, yAxisSource);

barSeries.setTitle("粉丝数", null); // 图例标题

// 9、绘制柱状图

chart.plot(barChart);

// 10、输出到word文档

FileOutputStream fos = new FileOutputStream(filePath);

document.write(fos); // 导出word

// 11、关闭流

fos.close();

document.close();

}

public static void createPieChart(String filePath) throws Exception {

// 1、创建word文档对象

XWPFDocument document = new XWPFDocument();

// 2、创建chart图表对象,抛出异常

XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

// 3、图表相关设置

chart.setTitleText("使用POI创建的饼图"); // 图表标题

chart.setTitleOverlay(false); // 图例是否覆盖标题

// 4、图例设置

XDDFChartLegend legend = chart.getOrAddLegend();

legend.setPosition(LegendPosition.TOP); // 图例位置:上下左右

// 5、X轴(分类轴)相关设置:饼图中的图例显示

String[] xAxisData = new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12",};

XDDFCategoryDataSource xAxisSource = XDDFDataSourcesFactory.fromArray(xAxisData); // 设置分类数据

// 6、Y轴(值轴)相关设置:饼图中的圆形显示

Integer[] yAxisData = new Integer[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57};

XDDFNumericalDataSource<Integer> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置值数据

// 7、创建饼图对象,饼状图不需要X,Y轴,只需要数据集即可

XDDFPieChartData pieChart = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null);

// 8、加载饼图数据集

XDDFPieChartData.Series pieSeries = (XDDFPieChartData.Series) pieChart.addSeries(xAxisSource, yAxisSource);

pieSeries.setTitle("粉丝数", null); // 系列提示标题

// 9、绘制饼图

chart.plot(pieChart);

// 10、输出到word文档

FileOutputStream fos = new FileOutputStream(filePath);

document.write(fos); // 导出word

// 11、关闭流

fos.close();

document.close();

}

public static void createGridLine(XWPFChart chart) {

//网格线颜色

CTSolidColorFillProperties fillProp = CTSolidColorFillProperties.Factory.newInstance();

CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();

rgb.setVal(new byte[]{(byte) 217, (byte) 217, (byte) 217});

fillProp.setSrgbClr(rgb);

//设置图表背后的网格线

CTLineProperties ctLine = chart.getCTChart().getPlotArea().getValAxArray(0).addNewMajorGridlines().addNewSpPr().addNewLn();

ctLine.addNewPrstDash().setVal(STPresetLineDashVal.SOLID);

ctLine.setSolidFill(fillProp);

//设置x y 轴 颜色

CTLineProperties xAxisProp = chart.getCTChart().getPlotArea().getCatAxArray(0).addNewSpPr().addNewLn();

CTLineProperties yAxisProp = chart.getCTChart().getPlotArea().getValAxArray(0).addNewSpPr().addNewLn();

xAxisProp.addNewPrstDash().setVal(STPresetLineDashVal.SOLID);

xAxisProp.setSolidFill(fillProp);

yAxisProp.addNewPrstDash().setVal(STPresetLineDashVal.SOLID);

yAxisProp.setSolidFill(fillProp);

}

public static void createBarLineChart(XWPFChart chart, ChartData chartData) throws IOException, InvalidFormatException {

// 创建Excel工作簿,用于存放图表数据

XSSFWorkbook workbook = chart.getWorkbook();

XSSFSheet sheet = workbook.getSheetAt(0);

// 填充图表数据

fillSheetBarLine(sheet);

// 创建图表

chart.setTitleText("柱状图和折线图");

chart.setTitleOverlay(false);

// 设置图表中的数据范围

XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);

XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);

leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);// 设置图柱的位置:BETWEEN居中

// 数据范围

XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 10, 0, 0)); // X轴数据范围

XDDFNumericalDataSource<Double> valuesBar = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 10, 1, 1)); // 柱状图数据范围

XDDFNumericalDataSource<Double> valuesLine = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 10, 2, 2)); // 折线图数据范围

// 创建柱状图数据

XDDFBarChartData barChart = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);

barChart.setBarDirection(BarDirection.COL); // 设置柱状图的方向:BAR横向,COL竖向,默认是BAR

XDDFChartData.Series barSeries = barChart.addSeries(categories, valuesBar);

barSeries.setTitle("柱状图", null);

chart.plot(barChart);

// 创建折线图数据

XDDFChartData lineData = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);

XDDFChartData.Series lineSeries = lineData.addSeries(categories, valuesLine);

lineSeries.setTitle("折线图", null);

createGridLine(chart);

chart.plot(lineData);

}

// 填充Excel工作表中的数据

private static void fillSheetBarLine(Sheet sheet) {

Row headerRow = sheet.createRow(0);

headerRow.createCell(0).setCellValue("类别");

headerRow.createCell(1).setCellValue("柱状图数据");

headerRow.createCell(2).setCellValue("折线图数据");

String[] categories = {"一月", "二月", "三月", "四月", "五月", "一1月", "二2月", "三3月", "四4月", "五5月"};

double[] barValues = {10, 20, 30, 40, 50, 15, 25, 35, 45, 55};

double[] lineValues = {15, 25, 35, 45, 55, 10, 20, 30, 40, 50};

for (int i = 0; i < categories.length; i++) {

Row row = sheet.createRow(i + 1);

row.createCell(0).setCellValue(categories[i]);

row.createCell(1).setCellValue(barValues[i]);

row.createCell(2).setCellValue(lineValues[i]);

}

}

public static void createBarChart(XWPFChart chart, ChartData chartData, boolean stacked, boolean isBar) throws IOException, InvalidFormatException {

// 填充图表数据

fillSheetBar(chart, chartData);

// 创建Excel工作簿,用于存放图表数据

XSSFWorkbook workbook = chart.getWorkbook();

XSSFSheet sheet = workbook.getSheetAt(0);

// 创建图表

chart.setTitleText("柱状图");

chart.setTitleOverlay(false);

// 设置图表中的数据范围

XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);

XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);

leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);// 设置图柱的位置:BETWEEN居中

// 数据范围

XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, chartData.getCategorys().length, 0, 0)); // X轴数据范围

// 创建柱状图数据

XDDFBarChartData barChart = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);

barChart.setBarDirection(isBar ? BarDirection.COL : BarDirection.BAR); // 设置柱状图的方向:BAR横向,COL竖向,默认是BAR - 条形、柱状

List<String> titles = new ArrayList<>();

for (String s : chartData.getValues().keySet()) {

titles.add(s);

}

for (int i = 0; i < titles.size(); i++) {

//选择 series 数据源---excel

XDDFNumericalDataSource<Double> valuesData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, chartData.getCategorys().length, i + 1, i + 1));

XDDFChartData.Series series = barChart.addSeries(categories, valuesData);

series.setTitle(titles.get(i), null);

}

if (chartData.getValues().size() > 1) {

// 多个柱子间距

barChart.setOverlap((byte) -20);

}

//堆叠

if (stacked && chartData.getValues().size() > 1) {

// 堆积条形图,将2个条形图重叠起来

barChart.setBarGrouping(BarGrouping.STACKED);

//修正重叠,使钢筋真正堆叠而不是并排

chart.getCTChart().getPlotArea().getBarChartArray(0).addNewOverlap().setVal((byte) 100);

}

createGridLine(chart);

chart.plot(barChart);

}

public static void fillSheetBar(XWPFChart chart, ChartData chartData) {

Map<String, Double[]> values = chartData.getValues();

XSSFWorkbook workbook;

//更新数据源

try {

workbook = chart.getWorkbook();

workbook.removeSheetAt(0);

XSSFSheet sheet = workbook.createSheet("Sheet1");

List<String> titles = new ArrayList<>();

for (String s : values.keySet()) {

titles.add(s);

}

for (int i = 0; i < titles.size(); i++) {

XSSFRow row = getRow(sheet, 0);

XSSFCell cell = getCell(workbook, row, i + 1);

cell.setCellValue(titles.get(i));

}

for (int i = 0; i < chartData.getCategorys().length; i++) {

XSSFRow row = getRow(sheet, i + 1);

XSSFCell first = getCell(workbook, row, 0);

first.setCellValue(chartData.getCategorys()[i]);

for (int k = 0; k < titles.size(); k++) {

XSSFCell cell = getCell(workbook, row, k + 1);

cell.setCellValue(values.get(titles.get(k))[i]);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 饼图

*

* @param chart 图表实例

* @param chartData 图表数据

* @param legendVisible 图例是否显示

* @throws IOException

* @throws InvalidFormatException

*/

public static void createPieChart(XWPFChart chart, ChartData chartData, Boolean legendVisible, double legendFontSize, boolean legendBold) throws IOException, InvalidFormatException {

//设置数据源

fillSheetPie(chart, chartData);

// 标题 -- 2003 版本报错

chart.setTitleText(chartData.getTitle());

// 标题覆盖

chart.setTitleOverlay(false);

// 图例位置

if (legendVisible) {

XDDFChartLegend legend = chart.getOrAddLegend();

legend.setPosition(LegendPosition.BOTTOM);

legend.setOverlay(true);// 图例是否覆盖标题

XDDFTextBody legendTextBody = new XDDFTextBody(legend);

legendTextBody.getXmlObject().addNewBodyPr();

legendTextBody.addNewParagraph().addDefaultRunProperties().setFontSize(legendFontSize);

legendTextBody.addNewParagraph().addDefaultRunProperties().setBold(legendBold);

legend.setTextBody(legendTextBody);

}

// 3、图表相关设置

chart.setTitleText("使用POI创建的饼图"); // 图表标题

chart.setTitleOverlay(false); // 图例是否覆盖标题

// 4、图例设置

XDDFChartLegend legend = chart.getOrAddLegend();

legend.setPosition(LegendPosition.TOP); // 图例位置:上下左右

XSSFWorkbook workbook = chart.getWorkbook();

XSSFSheet sheet = workbook.getSheetAt(0);

//设置数据范围-系列

XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, chartData.getCategorys().length - 1, 0, 0));

//设置数据范围-数值

XDDFNumericalDataSource<Double> valuesData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, chartData.getCategorys().length - 1, 1, 1));

// 7、创建饼图对象,饼状图不需要X,Y轴,只需要数据集即可

XDDFPieChartData pieChart = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null);

// 8、加载饼图数据集

XDDFPieChartData.Series pieSeries = (XDDFPieChartData.Series) pieChart.addSeries(categoriesData, valuesData);

pieSeries.setTitle("粉丝数", null); // 系列提示标题

// 9、绘制饼图

chart.plot(pieChart);

}

/**

* 设置数据源

*

* @param chart

* @param chartData

*/

public static void fillSheetPie(XWPFChart chart, ChartData chartData) {

String[] categorys = chartData.getCategorys();

Double[] values = chartData.getPieValues();

XSSFWorkbook workbook;

//更新数据源

try {

workbook = chart.getWorkbook();

workbook.removeSheetAt(0);

XSSFSheet sheet = workbook.createSheet("Sheet1");

for (int i = 0; i < categorys.length; i++) {

XSSFRow row = getRow(sheet, i);

XSSFCell cate = getCell(workbook, row, 0);

cate.setCellValue(categorys[i]);

XSSFCell val = getCell(workbook, row, 1);

val.setCellValue(values[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* util

*

* @param sheet

* @param index

* @return

*/

public static XSSFRow getRow(XSSFSheet sheet, int index) {

XSSFRow row = sheet.getRow(index);

if (Objects.isNull(row)) {

row = sheet.createRow(index);

}

return row;

}

/**

* util

*

* @param row

* @param index

* @return

*/

public static XSSFCell getCell(XSSFWorkbook wb, XSSFRow row, int index) {

XSSFCell cell = row.getCell(index);

if (Objects.isNull(cell)) {

cell = row.createCell(index);

}

XSSFCellStyle cellStyle = wb.createCellStyle();

cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐

XSSFFont headerFont1 = wb.createFont(); // 创建字体样式

headerFont1.setBold(false); //字体加粗

headerFont1.setFontName("宋体"); // 设置字体类型

headerFont1.setFontHeightInPoints((short) 9); // 设置字体大小

cellStyle.setFont(headerFont1); // 为标题样式设置字体样式

cell.setCellStyle(cellStyle);

return cell;

}

public static ChartData createChartDate(List<Map<String, Object>> data) {

ChartData chartData = new ChartData();

List<String> types = new ArrayList<>();

for (int i = 0; i < data.size(); i++) {

if (data.size() - 1 > i) {

types.add(data.get(i).get("ORG_NAME").toString());

}

}

chartData.setCategorys(types.toArray(new String[types.size()]));

Map<String, Double[]> values = new TreeMap<>(new Comparator<String>() {

@Override

public int compare(String o1, String o2) {

return o2.compareTo(o1);

}

});

Double[] d1 = new Double[chartData.getCategorys().length];

Double[] d2 = new Double[chartData.getCategorys().length];

for (int i = 0; i < data.size(); i++) {

if (i < data.size() - 1) {

Map<String, Object> m = data.get(i);

d1[i] = Double.valueOf(m.get("HIDDENNUMS").toString());

d2[i] = Double.valueOf(m.get("JDJYSYH").toString());

}

}

values.put("香蕉(个)", d2);

values.put("总数(个)", d1);

chartData.setValues(values);

Map<String, XDDFColor> colors = new HashMap<>();

colors.put("总数(个)", XDDFColor.from(new byte[]{(byte) 91, (byte) 155, (byte) 213}));

colors.put("苹果(个)", XDDFColor.from(new byte[]{(byte) 237, (byte) 125, (byte) 49}));

chartData.setColors(colors);

return chartData;

}

}

相关推荐
万里沧海寄云帆10 小时前
Word 插入分节符页码更新问题
windows·microsoft·word
zls36536521 小时前
.NET高效下载word文件
开发语言·c#·word·.net
wqqqianqian21 小时前
国产linux系统(银河麒麟,统信uos)使用 PageOffice 动态生成word文件
word·动态·pageoffice·国产版
handuoduo12342 天前
windows10 | mathtype导致word中ctrl+v不能用
word·朵朵排版技巧
黄鹂绿柳2 天前
word设置交叉引用快捷键和居中快捷键
word
hibiscusxin3 天前
vue-office:word(.docx)、pdf、excel(.xlsx,.xls)格式文件预览
vue.js·word·excel
scutrobot5 天前
word转pdf保存高清图技巧
pdf·word
小小小CTFER6 天前
Word_小问题解决_1
word
response_L6 天前
PageOffice打开保存文件的执行流程(工作原理)
java·word·excel·在线编辑
大新新大浩浩6 天前
使用docker-compose单点搭建社区版seafile+onlyoffice在线word编辑平台
docker·容器·word