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();