poi在Word中生成各种图表(支持柱状、组合、环状图、折线图、等常用图)

poi在Word中生成各种图表(支持柱状、组合、环状图、折线图、等常用图)

poi版本

1.maven坐标

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>

2.poi官网指出需要poi4.x.x版本抛弃了jdk1.7之前的版本,所以适应此版本需要将jdk升级,如果不想升级还有另一种办法就是,使用springBoot单独做一个服务为你的主项目提供一个接口,让主项目去调用生成word流让主项目去接收即可。

效果图

1.刷新之前
在这里插入图片描述
2.刷新之后
在这里插入图片描述

代码

public class PoiDemoWordTable {
public static void main(String[] args) throws Exception {
final String returnurl=“d://poi/test.docx”;
final String templateurl=“D://poi/onepage.docx”;
InputStream is = new FileInputStream(new File(templateurl));
XWPFDocument doc = new XWPFDocument(is);
replaceAll(doc);
//文件存在删除
try {
File file = new File(returnurl);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(returnurl);
doc.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
 * @Description: 替换段落和表格中
 */
public  static void replaceAll( XWPFDocument doc) throws InvalidFormatException, FileNotFoundException, IOException{
	/**----------------------------处理段落------------------------------------**/
	List<XWPFParagraph> paragraphList = doc.getParagraphs();
	if (paragraphList != null && paragraphList.size() > 0) {
		for (XWPFParagraph paragraph : paragraphList) {
			List<XWPFRun> runs = paragraph.getRuns();
			for (XWPFRun run : runs) {
				String text = run.getText(0);
				if (text != null) {
					if (text.contains("${table1}")) {
						run.setText("",0);
						XmlCursor cursor = paragraph.getCTP().newCursor();
						XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键
						XWPFTableRow tableOneRowOne = tableOne.getRow(0);//行
						setWordCellSelfStyle(tableOneRowOne.getCell(0),"微软雅黑","9",1,"left","top","#ffffff","#4472C4",1,"序号");
						XWPFTableCell cell12 = tableOneRowOne.createCell();
						setWordCellSelfStyle(cell12,"微软雅黑","9",1,"left","top","#ffffff","#4472C4",1,"公司名称(英文)");
						XWPFTableCell cell13 = tableOneRowOne.createCell();
						setWordCellSelfStyle(cell13,"微软雅黑","9",1,"left","top","#ffffff","#4472C4",1,"公司名称(中文)");

						XWPFTableRow tableOneRowTwo = tableOne.createRow();//行
						tableOneRowTwo.getCell(0).setText("第二行第一列");
						setWordCellSelfStyle(tableOneRowTwo.getCell(0),"微软雅黑","9",0,"left","top","#000000","#B4C6E7",1,"一行一列");
						tableOneRowTwo.getCell(1).setText("第二行第二列");
						setWordCellSelfStyle(tableOneRowTwo.getCell(1),"微软雅黑","9",0,"left","top","#000000","#B4C6E7",1,"一行一列");
						tableOneRowTwo.getCell(2).setText("第二行
  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 73
    评论
以下是使用POI导出Word柱状的步骤: 1. 导入POI库和相关依赖: ```java import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xddf.usermodel.chart.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; ``` 2. 创建一个新的Word文档: ```java XWPFDocument document = new XWPFDocument(); ``` 3. 创建一个柱状数据集: ```java XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Data"); Row row = sheet.createRow(0); row.createCell(0).setCellValue("Category"); row.createCell(1).setCellValue("Value1"); row.createCell(2).setCellValue("Value2"); row = sheet.createRow(1); row.createCell(0).setCellValue("Category 1"); row.createCell(1).setCellValue(10); row.createCell(2).setCellValue(20); row = sheet.createRow(2); row.createCell(0).setCellValue("Category 2"); row.createCell(1).setCellValue(30); row.createCell(2).setCellValue(40); row = sheet.createRow(3); row.createCell(0).setCellValue("Category 3"); row.createCell(1).setCellValue(50); row.createCell(2).setCellValue(60); XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 3, 0, 0)); XDDFNumericalDataSource<Double> values1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 1, 1)); XDDFNumericalDataSource<Double> values2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 2, 2)); ``` 4. 创建一个柱状: ```java XWPFChart chart = document.createChart(15, 15, 600, 400); chart.setTitleText("Bar Chart"); chart.setTitleOverlay(false); XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); XDDFCategoryAxis categoryAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); XDDFValueAxis valueAxis = chart.createValueAxis(AxisPosition.LEFT); valueAxis.setCrosses(AxisCrosses.AUTO_ZERO); XDDFChartData data = chart.createData(ChartTypes.BAR, categoryAxis, valueAxis); data.setVaryColors(true); XDDFChartData.Series series1 = data.addSeries(categories, values1); series1.setTitle("Value1", null); XDDFChartData.Series series2 = data.addSeries(categories, values2); series2.setTitle("Value2", null); chart.plot(data); ``` 5. 将图表插入到Word文档: ```java XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("Bar Chart:"); run.addBreak(); XDDFPicture picture = chart.getPreferredSize(); String id = document.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG); document.createPicture(id, document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG), picture.getWidth(), picture.getHeight()); ``` 6. 保存Word文档: ```java FileOutputStream out = new FileOutputStream("chart.docx"); document.write(out); out.close(); document.close(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 73
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值