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();
  • 相关阅读:
    Linux基本权限管理
    Spring JMS
    消息中间件 ActiveMQ的简单使用
    Ionic slides 轮播图
    Spring 3 MVC and XML example
    Java 数组
    Java String类
    Java static 使用
    http://blog.csdn.net/huang_xw/article/details/7090173
    http://blog.chinaunix.net/uid-20577907-id-3519578.html
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054078.html
Copyright © 2011-2022 走看看