zoukankan      html  css  js  c++  java
  • 【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();
  • 相关阅读:
    apex Error : Given no hashes to check 137 links for project 'pip': discarding no candidates
    源码编译pytorch-1.2.0
    proxychains
    git简单教程
    caffe ssd + cuda9.0
    ros+pcl error recoder
    硬盘挂载
    opencv3.4.1+ubuntu18.04+anaconda 源码编译
    从RocksDBStateBackend讲述Flink的State机制
    由Rocksdb状态后端引出的Tree的应用
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054078.html
Copyright © 2011-2022 走看看