|
| 1 | +package com.caozj.test; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Font; |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.text.DecimalFormat; |
| 9 | +import java.text.NumberFormat; |
| 10 | + |
| 11 | +import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| 12 | +import org.apache.poi.xwpf.usermodel.XWPFTable; |
| 13 | +import org.apache.poi.xwpf.usermodel.XWPFTableRow; |
| 14 | +import org.jfree.chart.ChartFactory; |
| 15 | +import org.jfree.chart.ChartUtilities; |
| 16 | +import org.jfree.chart.JFreeChart; |
| 17 | +import org.jfree.chart.labels.StandardPieSectionLabelGenerator; |
| 18 | +import org.jfree.chart.plot.PiePlot; |
| 19 | +import org.jfree.chart.plot.PlotOrientation; |
| 20 | +import org.jfree.data.category.DefaultCategoryDataset; |
| 21 | +import org.jfree.data.general.DefaultPieDataset; |
| 22 | + |
| 23 | +public class CreateTablesWithPOI { |
| 24 | + public static void main(String[] args) { |
| 25 | + String outputFile = "D:\\test222.docx"; |
| 26 | + XWPFDocument document = new XWPFDocument(); |
| 27 | + XWPFTable tableOne = document.createTable(); |
| 28 | + XWPFTableRow tableOneRowOne = tableOne.getRow(0); |
| 29 | + tableOneRowOne.getCell(0).setText("第1行第1列"); |
| 30 | + tableOneRowOne.addNewTableCell().setText("第1行第2列"); |
| 31 | + tableOneRowOne.addNewTableCell().setText("第1行第3列"); |
| 32 | + tableOneRowOne.addNewTableCell().setText("第1行第4列"); |
| 33 | + XWPFTableRow tableOneRowTwo = tableOne.createRow(); |
| 34 | + tableOneRowTwo.getCell(0).setText("第2行第1列"); |
| 35 | + tableOneRowTwo.getCell(1).setText("第2行第2列"); |
| 36 | + tableOneRowTwo.getCell(2).setText("第2行第3列"); |
| 37 | + FileOutputStream fOut; |
| 38 | + try { |
| 39 | + fOut = new FileOutputStream(outputFile); |
| 40 | + ByteArrayInputStream in = getPieChartImage(); |
| 41 | + String ind = document.addPictureData(in, XWPFDocument.PICTURE_TYPE_JPEG); |
| 42 | + System.out.println("pic ID=" + ind); |
| 43 | +// document.createPicture(document.getAllPictures().size() - 1, 200, 200, " "); |
| 44 | + // 放第二张图 |
| 45 | + ind = document.addPictureData(getBarChartImage(), XWPFDocument.PICTURE_TYPE_JPEG); |
| 46 | + System.out.println("pic ID=" + ind); |
| 47 | +// document.createPicture(document.getAllPictures().size() - 1, 200, 200, " "); |
| 48 | + document.write(fOut); |
| 49 | + fOut.flush(); |
| 50 | + // 操作结束,关闭文件 |
| 51 | + fOut.close(); |
| 52 | + } catch (Exception e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static ByteArrayInputStream getPieChartImage() { |
| 58 | + ByteArrayInputStream in = null; |
| 59 | + DefaultPieDataset pieDataset = new DefaultPieDataset(); |
| 60 | + pieDataset.setValue(" 北京局 ", 20); |
| 61 | + pieDataset.setValue(" 上海局 ", 18); |
| 62 | + pieDataset.setValue(" 天津局 ", 16); |
| 63 | + pieDataset.setValue(" 重庆局 ", 15); |
| 64 | + pieDataset.setValue(" 山东局 ", 45); |
| 65 | + JFreeChart chart = ChartFactory.createPieChart3D(" 企业备案图 ", pieDataset, true, false, false); |
| 66 | + // 设置标题字体样式 |
| 67 | + chart.getTitle().setFont(new Font(" 黑体 ", Font.BOLD, 20)); |
| 68 | + // 设置饼状图里描述字体样式 |
| 69 | + PiePlot piePlot = (PiePlot) chart.getPlot(); |
| 70 | + piePlot.setLabelFont(new Font(" 黑体 ", Font.BOLD, 10)); |
| 71 | + // 设置显示百分比样式 |
| 72 | + piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator((" {0}({2}) "), |
| 73 | + NumberFormat.getNumberInstance(), new DecimalFormat(" 0.00% "))); |
| 74 | + // 设置统计图背景 |
| 75 | + piePlot.setBackgroundPaint(Color.white); |
| 76 | + // 设置图片最底部字体样式 |
| 77 | + chart.getLegend().setItemFont(new Font(" 黑体 ", Font.BOLD, 10)); |
| 78 | + try { |
| 79 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 80 | + ChartUtilities.writeChartAsPNG(out, chart, 400, 300); |
| 81 | + in = new ByteArrayInputStream(out.toByteArray()); |
| 82 | + } catch (Exception e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + return in; |
| 86 | + } |
| 87 | + |
| 88 | + public static ByteArrayInputStream getBarChartImage() { |
| 89 | + ByteArrayInputStream in = null; |
| 90 | + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
| 91 | + dataset.addValue(100, "Spring Security", "Jan"); |
| 92 | + dataset.addValue(200, "jBPM 4", "Jan"); |
| 93 | + dataset.addValue(300, "Ext JS", "Jan"); |
| 94 | + dataset.addValue(400, "JFreeChart", "Jan"); |
| 95 | + JFreeChart chart = ChartFactory.createBarChart("chart", "num", "type", dataset, |
| 96 | + PlotOrientation.VERTICAL, true, false, false); |
| 97 | + // 设置标题字体样式 |
| 98 | + chart.getTitle().setFont(new Font(" 黑体 ", Font.BOLD, 20)); |
| 99 | + // 设置饼状图里描述字体样式 |
| 100 | + // 设置图片最底部字体样式 |
| 101 | + chart.getLegend().setItemFont(new Font(" 黑体 ", Font.BOLD, 10)); |
| 102 | + try { |
| 103 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 104 | + ChartUtilities.writeChartAsPNG(out, chart, 400, 300); |
| 105 | + in = new ByteArrayInputStream(out.toByteArray()); |
| 106 | + } catch (Exception e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + return in; |
| 110 | + } |
| 111 | +} |
0 commit comments