Poi 导出ppt
// 开始导出ppt
// 设置第一页ppt
XSLFSlide xslfSlide = xmlSlideShow.createSlide();
XSLFSlideLayout slideLayout = xslfSlide.getSlideLayout();
Style style = dashBoardObjectVo.getStyle();
String backgroud = style.getBackgroundColor();
String color = backgroud.substring(backgroud.indexOf("(") + 1, backgroud.indexOf(")"));
String[] colorrgb = color.split(",");
int r, g, b;
r = Integer.valueOf(colorrgb[0].trim());
g = Integer.valueOf(colorrgb[1].trim());
b = Integer.valueOf(colorrgb[2].trim());
// 设置背景色
slideLayout.getBackground().setFillColor(new Color(r, g, b));
// 设置大小
int width = style.getWidth();
int height = style.getHeight();
xmlSlideShow.setPageSize(new Dimension(width, height));
private static void addText(XSLFSlide xslfSlide, ChartsVo chartsVo, XMLSlideShow xmlSlideShow) {
// 文本框
Style style = chartsVo.getStyle();
int x = style.getLeft();
int y = style.getTop();
int width = style.getWidth();
int height = style.getHeight();
// 设置表格的x,y,width,height;
Rectangle2D rectangle2d = new Rectangle(x,y,width,height);
XSLFTextBox xslfTextBox = xslfSlide.createTextBox();
xslfTextBox.setAnchor(rectangle2d);
xslfTextBox.setFlipHorizontal(true);
// 段落
XSLFTextParagraph paragraph = xslfTextBox.addNewTextParagraph();
paragraph.setTextAlign(TextAlign.LEFT);
// 标题
XSLFTextRun xslfTextRun = paragraph.addNewTextRun();
// 宋体 (标题)
xslfTextRun.setFontFamily("\u5b8b\u4f53 (\u6807\u9898)");
String content = chartsVo.getContent();
xslfTextRun.setText(content.replace("<p>", "").replace("</p>", "").replace("\n\n", "\n").replace(" ", " "));
}
/**
* 添加折线
*
* @param xslfSlide
*/
private static void addLineChart(XSLFSlide xslfSlide, ChartsVo chartsVo, XMLSlideShow xmlSlideShow, legend legend,
List<series> series, xAxis xAxis) {
// 开始绘制折线
// 创建一个图表
XSLFChart chart = xmlSlideShow.createChart();
Widget widget = chartsVo.getJson();
String dimension = widget.getWidgetdimension();
String indexes = widget.getWidgetindexes();
Style style = chartsVo.getStyle();
int x = style.getLeft();
int y = style.getTop();
int width = style.getWidth();
int height = style.getHeight();
// 设置表格的x,y,width,height;
Rectangle2D rectangle2d = new Rectangle(x * WidgetMultiple, y * WidgetMultiple, width * SizeMultiple,
height * SizeMultiple);
// 填充数据
String[] seri = legend.getData().toArray(new String[legend.getData().size()]);
// x轴
String[] categories = xAxis.getData().toArray(new String[xAxis.getData().size()]);
List<Double[]> vDoubles = new ArrayList<>();
for (series dataSource : series) {
List<String> datas = dataSource.getData();
List<Double> daDoubles = new LinkedList<>();
for (String string : datas) {
daDoubles.add(Double.valueOf(string));
}
vDoubles.add(daDoubles.toArray(new Double[daDoubles.size()]));
}
// Double[] values1 = { (double) 123, (double) 123 };
// Double[] values2 = { (double) 456, (double) 456 };
XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle(indexes);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(dimension);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFLineChartData line = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
for (int i = 1; i <= seri.length; i++) {
String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, i, i));
XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(vDoubles.get(i - 1),
valuesDataRange, i);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) line.addSeries(categoriesData, valuesData);
series1.setTitle(seri[i - 1], chart.setSheetTitle(seri[i - 1], i));
series1.setMarkerStyle(MarkerStyle.NONE);
series1.setMarkerSize((short) 2);
XDDFShapeProperties xddfShapeProperties = new XDDFShapeProperties();
series1.setShapeProperties(xddfShapeProperties);
}
chart.plot(line);
XDDFChartLegend xddfChartLegend = chart.getOrAddLegend();
xddfChartLegend.setPosition(LegendPosition.TOP_RIGHT);
xddfChartLegend.setOverlay(false);
// 添加到ppt中
xslfSlide.addChart(chart, rectangle2d);
}
/**
* 添加饼图
*
* @param xslfSlide
*/
private static void addPieChart(XSLFSlide xslfSlide, ChartsVo chartsVo, XMLSlideShow xmlSlideShow,
PieVo.legend legend, List<PieVo.series> series) {
// 创建一个图表
XSLFChart chart = xmlSlideShow.createChart();
// 设置表格的x,y,width,height;
Style style = chartsVo.getStyle();
int x = style.getLeft();
int y = style.getTop();
int width = style.getWidth();
int height = style.getHeight();
// 设置表格的x,y,width,height;
Rectangle2D rectangle2d = new Rectangle(x * WidgetMultiple, y * WidgetMultiple, width * SizeMultiple,
height * SizeMultiple);
// 设置维度
// String[] categories = legend.getData().toArray(new
// String[legend.getData().size()]);
// List<Double[]> vDoubles = new ArrayList<>();
// for (PieVo.series dataSource : series) {
// List<String> datas = dataSource.getData();
// List<Double> daDoubles = new LinkedList<>();
// for (String string : datas) {
// daDoubles.add(Double.valueOf(string));
// }
// vDoubles.add(daDoubles.toArray(new Double[daDoubles.size()]));
// }
List<PieVo.seriesdata> sList = series.get(0).getData();
String[] categories = new String[sList.size()];
Double[] values = new Double[sList.size()];
int i = 0;
for (seriesdata seriesdata : sList) {
categories[i] = seriesdata.getName();
values[i] = Double.valueOf(seriesdata.getValue());
i++;
}
// 初始化表格样式
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
// 根据数据创建excel第一行标题行
// 填充数据
for (int index = 0; index < values.length; index++) {
// 设置每一行的字段标题和数据
XSSFRow rowi = sheet.createRow(index + 1);
rowi.createCell(0).setCellValue(categories[index]);
// 设置左边字段样式
rowi.createCell(1).setCellValue(values[index]);
}
final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values,
valuesDataRange);
CTChart ctChart = chart.getCTChart();
CTPlotArea ctPlotArea = ctChart.getPlotArea();
CTPieChart ctPieChart = ctPlotArea.addNewPieChart();
CTBoolean ctBoolean = ctPieChart.addNewVaryColors();
ctBoolean.setVal(true);
CTPieSer ctPieSer = ctPieChart.addNewSer();
CTSerTx ctSerTx = ctPieSer.addNewTx();
CTStrRef ctStrRefTx = ctSerTx.addNewStrRef();
ctStrRefTx.setF(categoryDataRange);
ctPieSer.addNewIdx().setVal(0);
CTAxDataSource cttAxDataSource = ctPieSer.addNewCat();
CTStrRef ctStrRef = cttAxDataSource.addNewStrRef();
ctStrRef.setF(categoryDataRange);
CTNumDataSource ctNumDataSource = ctPieSer.addNewVal();
CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();
ctNumRef.setF(valuesDataRange);
CTStrData strData = ctStrRef.addNewStrCache();
CTNumData numData = ctNumRef.addNewNumCache();
strData.setPtArray(null);
numData.setPtArray(null);
int idx = 0;
int rowNum = 1;
for (int j = 0; j < values.length; j++) {
// 主要改这,前面看不懂是什么,也不知道在干什么
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(idx);
numVal.setV(values[j] + "");
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(idx);
sVal.setV(categories[j]);
idx++;
XSSFRow row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(categories[j]);
row.createCell(1).setCellValue(values[j]);
}
ctPieSer.addNewDLbls().addNewShowLeaderLines();// 有无此行代码,图上是否显示文字
// legend图注
CTLegend ctLegend = ctChart.addNewLegend();
ctLegend.addNewLegendPos().setVal(STLegendPos.TR);
ctLegend.addNewOverlay().setVal(true);
ctPieSer.addNewExplosion().setVal(1);// 各块之间间隙大小
ctPieSer.addNewOrder().setVal(0);//
xslfSlide.addChart(chart, rectangle2d);
}
/**
* 添加柱状图
*
* @param xslfSlide
*/
private static void addHistogramChart(XSLFSlide xslfSlide, ChartsVo chartsVo, XMLSlideShow xmlSlideShow,
legend legend, List<series> series, xAxis xAxis) {
// 创建一个图表
XSLFChart chart = xmlSlideShow.createChart();
Style style = chartsVo.getStyle();
int x = style.getLeft();
int y = style.getTop();
int width = style.getWidth();
int height = style.getHeight();
// 设置表格的x,y,width,height;
Rectangle2D rectangle2d = new Rectangle(x * WidgetMultiple, y * WidgetMultiple, width * SizeMultiple,
height * SizeMultiple);
// 填充数据
String[] seri = legend.getData().toArray(new String[legend.getData().size()]);
// x轴
String[] categories = xAxis.getData().toArray(new String[xAxis.getData().size()]);
List<Double[]> vDoubles = new ArrayList<>();
for (series dataSource : series) {
List<String> datas = dataSource.getData();
List<Double> daDoubles = new LinkedList<>();
for (String string : datas) {
daDoubles.add(Double.valueOf(string));
}
vDoubles.add(daDoubles.toArray(new Double[daDoubles.size()]));
}
// Double[] values1 = { (double) 123, (double) 123 };
// Double[] values2 = { (double) 456, (double) 456 };
XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("bottom");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("left");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
for (int i = 1; i <= seri.length; i++) {
String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, i, i));
XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(vDoubles.get(i - 1),
valuesDataRange, i);
XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
series1.setTitle(seri[i - 1], chart.setSheetTitle(seri[i - 1], i));
}
bar.setVaryColors(true);
bar.setBarDirection(BarDirection.COL);
chart.plot(bar);
XDDFChartLegend xddfChartLegend = chart.getOrAddLegend();
xddfChartLegend.setPosition(LegendPosition.LEFT);
xddfChartLegend.setOverlay(false);
// 添加到ppt中
xslfSlide.addChart(chart, rectangle2d);
}
/**
* 添加表格
*
* @param xslfSlide
*/
private static void addTable(XSLFSlide xslfSlide, ChartsVo chartsVo, List<String> tableHeadList,
List<Map<String, Object>> tableDataList) {
// 开始添加表格
XSLFTable xslfTable = xslfSlide.createTable();
Style style = chartsVo.getStyle();
int x = style.getLeft();
int y = style.getTop();
// width和height没什么用好像
int width = style.getWidth();
int height = style.getHeight();
// 设置表格的x,y,width,height;
Rectangle2D rectangle2d = new Rectangle(x, y, 0, 0);
// 生成 表头第一行
XSLFTableRow head = xslfTable.addRow();
int headRow = 0;
for (String headtext : tableHeadList) {
XSLFTableCell headcell = head.addCell();
// 设置样式
headcell.setFillColor(new Color(248, 248, 248));
// headcell.setTextDirection(TextDirection.STACKED);
XSLFTextParagraph p = headcell.addNewTextParagraph();
XSLFTextRun tr = p.addNewTextRun();
tr.setText(headtext);
tr.setFontSize((double) 14L);
double colwidth = headtext.length() * 10 + 150;
xslfTable.setColumnWidth(headRow, colwidth);
p.setTextAlign(TextAlign.CENTER);
headcell.setVerticalAlignment(VerticalAlignment.MIDDLE);
headcell.setBorderWidth(BorderEdge.left, 1);
headcell.setBorderWidth(BorderEdge.right, 1);
headcell.setBorderWidth(BorderEdge.bottom, 1);
headcell.setBorderWidth(BorderEdge.top, 1);
headcell.setBorderColor(BorderEdge.left, Color.BLACK);
headcell.setBorderColor(BorderEdge.right, Color.BLACK);
headcell.setBorderColor(BorderEdge.bottom, Color.BLACK);
headcell.setBorderColor(BorderEdge.top, Color.BLACK);
}
// 开始填充数据
for (Map<String, Object> dataMap : tableDataList) {
XSLFTableRow dataRow = xslfTable.addRow();
dataRow.setHeight(20);
for (String datekey : tableHeadList) {
XSLFTableCell tableCell = dataRow.addCell();
XSLFTextParagraph p = tableCell.addNewTextParagraph();
XSLFTextRun tr = p.addNewTextRun();
tr.setText(dataMap.get(datekey) + "");
tr.setFontSize((double) 14L);
// tableCell.setTextDirection(TextDirection.STACKED);
p.setTextAlign(TextAlign.CENTER);
tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
tableCell.setBorderWidth(BorderEdge.left, 1);
tableCell.setBorderWidth(BorderEdge.right, 1);
tableCell.setBorderWidth(BorderEdge.bottom, 1);
tableCell.setBorderWidth(BorderEdge.top, 1);
tableCell.setBorderColor(BorderEdge.left, Color.BLACK);
tableCell.setBorderColor(BorderEdge.right, Color.BLACK);
tableCell.setBorderColor(BorderEdge.bottom, Color.BLACK);
tableCell.setBorderColor(BorderEdge.top, Color.BLACK);
}
}
// 开始设置样式
xslfTable.setAnchor(rectangle2d);
}