2. Apache POI
2.1 介紹
Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開源項(xiàng)目。簡單來說就是,我們可以使用 POI 在 Java 程序中對Miscrosoft Office各種文件進(jìn)行讀寫操作。
一般情況下,POI 都是用于操作 Excel 文件。
Apache POI 的應(yīng)用場景:
-
銀行網(wǎng)銀系統(tǒng)導(dǎo)出交易明細(xì)
-
各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報(bào)表
-
批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
入門案例
Apache POI既可以將數(shù)據(jù)寫入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來分別進(jìn)行實(shí)現(xiàn)。
Apache POI的maven坐標(biāo):(項(xiàng)目中已導(dǎ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ā)
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文件寫入數(shù)據(jù)
* @throws Exception
*/
public static void write() throws Exception{
//在內(nèi)存中創(chuàng)建一個(gè)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個(gè)單元格
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). 實(shí)現(xiàn)效果
在D盤中生成itcast.xlsx文件,創(chuàng)建名稱為itcast的Sheet頁,同時(shí)將內(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個(gè)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個(gè)單元格
XSSFCell cell1 = titleRow.getCell(1);
//獲取單元格中的文本內(nèi)容
String cellValue1 = cell1.getStringCellValue();
//獲取行的第3個(gè)單元格
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). 實(shí)現(xiàn)效果
將itcast.xlsx文件中的數(shù)據(jù)進(jìn)行讀取文章來源:http://www.zghlxwxcb.cn/news/detail-432488.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-432488.html
3. 導(dǎo)出運(yùn)營數(shù)據(jù)Excel報(bào)表
3.1controller層
/**
* 導(dǎo)出運(yùn)營數(shù)據(jù)報(bào)表
* @param response
*/
@GetMapping("/export")
@ApiOperation("導(dǎo)出運(yùn)營數(shù)據(jù)報(bào)表")
public void export(HttpServletResponse response){
reportService.exportBusinessData(response);
}
3.2service層
@Override
public void export(HttpServletResponse response) {
LocalDate begin = LocalDate.now().minusDays(30);
LocalDate end = LocalDate.now().minusDays(1);
//查詢概覽運(yùn)營數(shù)據(jù),提供給Excel模板文件
BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));
/**
class.getResourceAsStream
如果path不帶"/",那么就是從當(dāng)前class文件的路徑下找文件
如果path帶"/",那么就是從類路徑.classpath中去找文件
*/
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/運(yùn)營數(shù)據(jù)報(bào)表模板.xlsx");
try {
//基于提供好的模板文件創(chuàng)建一個(gè)新的Excel表格對象
XSSFWorkbook excel = new XSSFWorkbook(inputStream);
//獲得Excel文件中的一個(gè)Sheet頁
XSSFSheet sheet = excel.getSheet("Sheet1");
sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);
//獲得第4行
XSSFRow row = sheet.getRow(3);
//獲取單元格
row.getCell(2).setCellValue(businessData.getTurnover());
row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
row.getCell(6).setCellValue(businessData.getNewUsers());
row = sheet.getRow(4);
row.getCell(2).setCellValue(businessData.getValidOrderCount());
row.getCell(4).setCellValue(businessData.getUnitPrice());
for (int i = 0; i < 30; i++) {
LocalDate date = begin.plusDays(i);
//準(zhǔn)備明細(xì)數(shù)據(jù)
businessData = workspaceService.getBusinessData(LocalDateTime.of(date,LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
row = sheet.getRow(7 + i);
row.getCell(1).setCellValue(date.toString());
if (Objects.isNull(businessData.getTurnover())){
row.getCell(2).setCellValue(0.0);
}else {
row.getCell(2).setCellValue(businessData.getTurnover());
}
if (Objects.isNull(businessData.getValidOrderCount())){
row.getCell(3).setCellValue(0);
}else {
row.getCell(3).setCellValue(businessData.getValidOrderCount());
}
if (Objects.isNull(businessData.getOrderCompletionRate())){
row.getCell(4).setCellValue(0.0);
}else {
row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
}
if (Objects.isNull(businessData.getUnitPrice())){
row.getCell(5).setCellValue(0.0);
}else {
row.getCell(5).setCellValue(businessData.getUnitPrice());
}
if (Objects.isNull(businessData.getNewUsers())){
row.getCell(6).setCellValue(0);
}else {
row.getCell(6).setCellValue(businessData.getNewUsers());
}
}
//通過輸出流將文件下載到客戶端瀏覽器中
ServletOutputStream out = response.getOutputStream();
excel.write(out);
//關(guān)閉資源
out.flush();
out.close();
excel.close();
}catch (IOException e){
e.printStackTrace();
}
}
到了這里,關(guān)于Apache POI,springboot中導(dǎo)出excel報(bào)表的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!