【JAVA】POI生成EXCEL图表(柱状图、折线等)

1、使用excel工具自带的图形工具创建一个图:

2、绑定数据区域:

3、数据区域绑定完成,我们要做的就是将数据写入到数据区域中:

4、标记

5、POI 引入包

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

6、代码:

FileInputStream is = new FileInputStream("刚才创建的文件所在目录+文件名");
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
            FileOutputStream os = new FileOutputStream("导出的位置");
            //获取创建工作簿的第一页
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            //自动计算
            sheet.setForceFormulaRecalculation(true);
            //给指定的sheet命名
            xssfWorkbook.setSheetName(0, "sheet0");
            //初始化当前的索引,设为当前sheet的最后一行行数
            int allRows = sheet.getLastRowNum();
            //存储当前表格的样式
            XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
            //填充数据
          for(int i=allRows;i<=allRows;i++){
            XSSFRow row = sheet.getRow(i);
            if (row == null) {
                continue;
            }

            //遍历列
            for (int j = 1; j <=dailyReportPart8.size(); j++) {
                XSSFCell cell = row.getCell(j) != null ? row.getCell(j) : row.createCell(j);
                String cellValue = cell.getStringCellValue();
                if (cellValue.startsWith("#a1")) {
                    cell.setCellValue(1);
                }
               
            }

        }
            //写出
            xssfWorkbook.write(os);
            //TODO 流的处理
            is.close();
            os.flush();
            os.close();

 

posted @ 2019-01-22 11:58  黑猫先生  阅读(16688)  评论(4编辑  收藏  举报