實現(xiàn):
-
讀(獲?。?/p>
-
寫(導(dǎo)入)
2.1 介紹
Apache POI 是一個處理Miscrosoft Office各種文件格式的開源項目。簡單來說就是,我們可以使用 POI 在 Java 程序中對Miscrosoft Office各種文件進行讀寫操作。 一般情況下,POI 都是用于操作 Excel 文件。
Apache POI 的應(yīng)用場景:
-
銀行網(wǎng)銀系統(tǒng)導(dǎo)出交易明細
-
各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報表
-
批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
2.2 入門案例
Apache POI既可以將數(shù)據(jù)寫入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來分別進行實現(xiàn)。
Apache POI的maven坐標(biāo):
<dependency>
? ?<groupId>org.apache.poi</groupId>
? ?<artifactId>poi</artifactId>
? ?<version>3.16</version>
</dependency>
<dependency>
? ?<groupId>org.apache.poi</groupId>
? ?<artifactId>poi-ooxml</artifactId>
? ?<version>3.16</version>
</dependency>
2.2.1 將數(shù)據(jù)寫入Excel文件
1). 代碼開發(fā)
?
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
?
public class POITest {
?
? ?/**
? ? * 基于POI向Excel文件寫入數(shù)據(jù)
? ? * @throws Exception
? ? */
? ?public static void write() throws Exception{
? ? ? ?//在內(nèi)存中創(chuàng)建一個Excel文件對象
? ? ? ?XSSFWorkbook excel = new XSSFWorkbook();
? ? ? ?//創(chuàng)建Sheet頁
? ? ? ?XSSFSheet sheet = excel.createSheet("itcast");
?
? ? ? ?//在Sheet頁中創(chuàng)建行,0表示第1行
? ? ? ?XSSFRow row1 = sheet.createRow(0);
? ? ? ?//創(chuàng)建單元格并在單元格中設(shè)置值,單元格編號也是從0開始,1表示第2個單元格
? ? ? ?row1.createCell(1).setCellValue("姓名");
? ? ? ?row1.createCell(2).setCellValue("城市");
?
? ? ? ?XSSFRow row2 = sheet.createRow(1);
? ? ? ?row2.createCell(1).setCellValue("張三");
? ? ? ?row2.createCell(2).setCellValue("北京");
?
? ? ? ?XSSFRow row3 = sheet.createRow(2);
? ? ? ?row3.createCell(1).setCellValue("李四");
? ? ? ?row3.createCell(2).setCellValue("上海");
?
? ? ? ?FileOutputStream out = new FileOutputStream(new File("D:\\itcast.xlsx"));
? ? ? ?//通過輸出流將內(nèi)存中的Excel文件寫入到磁盤上
? ? ? ?excel.write(out);
?
? ? ? ?//關(guān)閉資源
? ? ? ?out.flush();
? ? ? ?out.close();
? ? ? ?excel.close();
? }
? ?public static void main(String[] args) throws Exception {
? ? ? ?write();
? }
}
2). 實現(xiàn)效果
在D盤中生成itcast.xlsx文件,創(chuàng)建名稱為itcast的Sheet頁,同時將內(nèi)容成功寫入。
2.2.2 讀取Excel文件中的數(shù)據(jù)
1). 代碼開發(fā)
package com.sky.test;
?
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
?
public class POITest {
? ?/**
? ? * 基于POI讀取Excel文件
? ? * @throws Exception
? ? */
? ?public static void read() throws Exception{
? ? ? ?FileInputStream in = new FileInputStream(new File("D:\\itcast.xlsx"));
? ? ? ?//通過輸入流讀取指定的Excel文件
? ? ? ?XSSFWorkbook excel = new XSSFWorkbook(in);
? ? ? ?//獲取Excel文件的第1個Sheet頁
? ? ? ?XSSFSheet sheet = excel.getSheetAt(0);
?
? ? ? ?//獲取Sheet頁中的最后一行的行號
? ? ? ?int lastRowNum = sheet.getLastRowNum();
?
? ? ? ?for (int i = 0; i <= lastRowNum; i++) {
? ? ? ? ? ?//獲取Sheet頁中的行
? ? ? ? ? ?XSSFRow titleRow = sheet.getRow(i);
? ? ? ? ? ?//獲取行的第2個單元格
? ? ? ? ? ?XSSFCell cell1 = titleRow.getCell(1);
? ? ? ? ? ?//獲取單元格中的文本內(nèi)容
? ? ? ? ? ?String cellValue1 = cell1.getStringCellValue();
? ? ? ? ? ?//獲取行的第3個單元格
? ? ? ? ? ?XSSFCell cell2 = titleRow.getCell(2);
? ? ? ? ? ?//獲取單元格中的文本內(nèi)容
? ? ? ? ? ?String cellValue2 = cell2.getStringCellValue();
?
? ? ? ? ? ?System.out.println(cellValue1 + " " +cellValue2);
? ? ? }
?
? ? ? ?//關(guān)閉資源
? ? ? ?in.close();
? ? ? ?excel.close();
? }
?
? ?public static void main(String[] args) throws Exception {
? ? ? ?read();
? }
}
?
2). 實現(xiàn)效果
將itcast.xlsx文件中的數(shù)據(jù)進行讀取到控制臺
3. 導(dǎo)出運營數(shù)據(jù)Excel報表
3.1 需求分析和設(shè)計
3.1.1 產(chǎn)品原型
在數(shù)據(jù)統(tǒng)計頁面,有一個數(shù)據(jù)導(dǎo)出的按鈕,點擊該按鈕時,其實就會下載一個文件。這個文件實際上是一個Excel形式的文件,文件中主要包含最近30日運營相關(guān)的數(shù)據(jù)。表格的形式已經(jīng)固定,主要由概覽數(shù)據(jù)和明細數(shù)據(jù)兩部分組成。真正導(dǎo)出這個報表之后,相對應(yīng)的數(shù)字就會填充在表格中,就可以進行存檔。
導(dǎo)出的Excel報表格式:
(復(fù)雜的表格可以手動設(shè)計。)
業(yè)務(wù)規(guī)則:
-
導(dǎo)出Excel形式的報表文件
-
導(dǎo)出最近30天的運營數(shù)據(jù)
3.1.2 接口設(shè)計
通過上述原型圖設(shè)計對應(yīng)的接口。(查詢get)
注意:
-
當(dāng)前接口沒有傳遞參數(shù),因為導(dǎo)出的是最近30天的運營數(shù)據(jù),后端計算即可,所以不需要任何參數(shù)
-
當(dāng)前接口沒有返回數(shù)據(jù),因為報表導(dǎo)出功能本質(zhì)上是文件下載,服務(wù)端會通過輸出流將Excel文件下載到客戶端瀏覽器
3.2 代碼開發(fā)
3.2.1 實現(xiàn)步驟
1). 設(shè)計Excel模板文件(實現(xiàn)后端直接寫入!)
2). 查詢近30天的運營數(shù)據(jù)
3). 將查詢到的運營數(shù)據(jù)寫入模板文件
4). 通過輸出流將Excel文件下載到客戶端瀏覽器
3.2.2 Controller層
根據(jù)接口定義,在ReportController中創(chuàng)建export方法:
/**
? ? * 導(dǎo)出運營數(shù)據(jù)報表
? ? * @param response
? ? */
? ?@GetMapping("/export")
? ?@ApiOperation("導(dǎo)出運營數(shù)據(jù)報表")
? ?public void export(HttpServletResponse response){
? ? ? ?reportService.exportBusinessData(response);
? }
3.2.3 Service層接口
在ReportService接口中聲明導(dǎo)出運營數(shù)據(jù)報表的方法:
/**
? ? * 導(dǎo)出近30天的運營數(shù)據(jù)報表
? ? * @param response
? ? **/
? ?void exportBusinessData(HttpServletResponse response);
提前將運營數(shù)據(jù)報表模板.xlsx拷貝到項目的resources/template目錄中
3.2.4 Service層實現(xiàn)類
在ReportServiceImpl實現(xiàn)類中實現(xiàn)導(dǎo)出運營數(shù)據(jù)報表的方法:
? /**
? ? * 導(dǎo)出當(dāng)前30天運營數(shù)據(jù)報表
? ? * @param response
? ? */
? ?public void exportBusinessData(HttpServletResponse response) {
? ? ? ?//1. 查詢數(shù)據(jù)庫,獲取營業(yè)數(shù)據(jù)---查詢最近30天的運營數(shù)據(jù)
? ? ? ?LocalDate dateBegin = LocalDate.now().minusDays(30);
? ? ? ?LocalDate dateEnd = LocalDate.now().minusDays(1);
?
? ? ? ?//查詢概覽數(shù)據(jù)workspaceService數(shù)據(jù)獲取。
? ? ? ?BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));
?
? ? ? ?//2. 通過POI將數(shù)據(jù)寫入到Excel文件中
? ? ? ?InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/運營數(shù)據(jù)報表模板.xlsx");
?
? ? ? ?try {
? ? ? ? ? ?//基于模板文件創(chuàng)建一個新的Excel文件
? ? ? ? ? ?XSSFWorkbook excel = new XSSFWorkbook(in);
?
? ? ? ? ? ?//獲取表格文件的Sheet頁
? ? ? ? ? ?XSSFSheet sheet = excel.getSheet("Sheet1");
?
? ? ? ? ? ?//填充數(shù)據(jù)--時間
? ? ? ? ? ?sheet.getRow(1).getCell(1).setCellValue("時間:" + dateBegin + "至" + dateEnd);
?
? ? ? ? ? ?//獲得第4行
? ? ? ? ? ?XSSFRow row = sheet.getRow(3);
? ? ? ? ? ?row.getCell(2).setCellValue(businessDataVO.getTurnover());
? ? ? ? ? ?row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
? ? ? ? ? ?row.getCell(6).setCellValue(businessDataVO.getNewUsers());
?
? ? ? ? ? ?//獲得第5行
? ? ? ? ? ?row = sheet.getRow(4);
? ? ? ? ? ?row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
? ? ? ? ? ?row.getCell(4).setCellValue(businessDataVO.getUnitPrice());
?
? ? ? ? ? ?//填充明細數(shù)據(jù)
? ? ? ? ? ?for (int i = 0; i < 30; i++) {
? ? ? ? ? ? ? ?LocalDate date = dateBegin.plusDays(i);
? ? ? ? ? ? ? ?//查詢某一天的營業(yè)數(shù)據(jù)
? ? ? ? ? ? ? ?BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
?
? ? ? ? ? ? ? ?//獲得某一行
? ? ? ? ? ? ? ?row = sheet.getRow(7 + i);
? ? ? ? ? ? ? ?row.getCell(1).setCellValue(date.toString());
? ? ? ? ? ? ? ?row.getCell(2).setCellValue(businessData.getTurnover());
? ? ? ? ? ? ? ?row.getCell(3).setCellValue(businessData.getValidOrderCount());
? ? ? ? ? ? ? ?row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
? ? ? ? ? ? ? ?row.getCell(5).setCellValue(businessData.getUnitPrice());
? ? ? ? ? ? ? ?row.getCell(6).setCellValue(businessData.getNewUsers());
? ? ? ? ? }
?
? ? ? ? ? ?//3. 通過輸出流將Excel文件下載到客戶端瀏覽器
? ? ? ? ? ?ServletOutputStream out = response.getOutputStream();
? ? ? ? ? ?excel.write(out);
?
? ? ? ? ? ?//關(guān)閉資源
? ? ? ? ? ?out.close();
? ? ? ? ? ?excel.close();
? ? ? } catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? }
?
? }
3.3 功能測試
直接使用前后端聯(lián)調(diào)測試。
進入數(shù)據(jù)統(tǒng)計
點擊數(shù)據(jù)導(dǎo)出:Excel報表下載成功文章來源:http://www.zghlxwxcb.cn/news/detail-793082.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-793082.html
3.4 代碼提交
到了這里,關(guān)于基于Apache POI-操作Excel數(shù)據(jù)-讀寫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!