【JAVA】POI生成EXCEL圖表(柱狀圖、折線等)

一、使用excel工具自帶的圖形工具建立一個圖:apache

二、綁定數據區域:xss

三、數據區域綁定完成,咱們要作的就是將數據寫入到數據區域中:工具

四、標記3d

五、POI 引入包excel

<!-- 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>

六、代碼:code

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();
相關文章
相關標籤/搜索