一.Apache POI
可以通過Apache POI處理excel文件,核心操作是讀和寫
應(yīng)用場景
- 銀行網(wǎng)銀交易明細(xì)
- 各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報表
- 批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
使用步驟
1.導(dǎo)入maven坐標(biāo)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
2.測試代碼(寫操作)
public class PoiTest {
/**
* 寫操作
* 通過POI創(chuàng)建excel文件并寫入文件內(nèi)容
*/
public static void write() throws IOException {
//在內(nèi)存中創(chuàng)建一個excel文件XSSFWorkbook表示excel文件
XSSFWorkbook excel = new XSSFWorkbook();
//在excel文件中創(chuàng)建一個sheet頁
XSSFSheet sheet = excel.createSheet("info");
//在sheet頁中創(chuàng)建行對象,i表示第i+1行
XSSFRow row = sheet.createRow(1);
//在行上創(chuàng)建單元格,并寫入內(nèi)容
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("城市");
row = sheet.createRow(2);
row.createCell(1).setCellValue("張三");
row.createCell(2).setCellValue("北京");
row = sheet.createRow(3);
row.createCell(1).setCellValue("李四");
row.createCell(2).setCellValue("東京");
//通過輸出流將內(nèi)存中的內(nèi)容輸出到文件中
FileOutputStream fileOutputStream=new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(fileOutputStream);
fileOutputStream.close();
excel.close();
}
public static void main(String[] args) throws Exception {
write();
}
}
3.運(yùn)行結(jié)果(寫操作)
4.測試代碼(讀操作)
public class PoiTest {
/**
* 通過POI讀取excel文件中內(nèi)容
*/
public static void read() throws IOException {
//通過輸入流讀取一個磁盤中的文件內(nèi)容;
FileInputStream inputStream = new FileInputStream(new File("D:\\info.xlsx"));
//在內(nèi)存中創(chuàng)建一個excel文件XSSFWorkbook表示excel文件,并傳入輸入流
XSSFWorkbook excel=new XSSFWorkbook(inputStream);
//按照sheet頁的名稱讀取
XSSFSheet sheet=excel.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();//獲取有文字的最后一行的行號(從0開始);
for (int i = 1; i < lastRowNum; i++) {
//獲取某一行
XSSFRow row=sheet.getRow(i);
if (row==null) continue;
//獲取單元格對象
String cellValue=row.getCell(1).getStringCellValue();
String cellValue1=row.getCell(2).getStringCellValue();
System.out.println(cellValue+" "+cellValue1);
}
//關(guān)閉輸入流
inputStream.close();
//關(guān)閉資源
excel.close();
}
public static void main(String[] args) throws Exception {
// write();
read();
}
}
5.運(yùn)行結(jié)果(讀操作)
二.導(dǎo)出Excel報表
由于實(shí)際業(yè)務(wù)中可能會有復(fù)雜的報表格式,如果直接使用POI進(jìn)行讀取操作十分繁瑣,通常是先在windows上對報表格式進(jìn)行設(shè)計,得到一個模版文件,直接讀入模版文件進(jìn)行操作即可
- 設(shè)計模版文件
- 查詢xx天的數(shù)據(jù)
- 將查詢到的數(shù)據(jù)寫入模版文件
- 通過輸出流將excel文件下載到客戶端瀏覽器
實(shí)現(xiàn)步驟
- 項(xiàng)目中導(dǎo)入模版文件,在src/main/resources/下新建template目錄用來存放模版文件,將模版.xlsx導(dǎo)入
- 編寫代碼,對指定位置進(jìn)行填充,寫入excel文件中的格式基本是固定的,我認(rèn)為關(guān)鍵在于傳遞的參HttpServletResponse response,response.getOutputStream(): 通過調(diào)用response對象的getOutputStream()方法,獲取到一個Servlet輸出流。Servlet輸出流是用于向客戶端發(fā)送數(shù)據(jù)的輸出流,通過這個輸出流可以將數(shù)據(jù)發(fā)送到客戶端。excel.write(outputStream): 調(diào)用excel對象的write()方法,將Excel文件內(nèi)容寫入到之前獲取的Servlet輸出流中。這會將Excel文件的內(nèi)容寫入到HTTP響應(yīng)的輸出流中,實(shí)際上是將Excel文件的字節(jié)流發(fā)送到客戶端瀏覽器。然后客戶端瀏覽器獲得響應(yīng)后自動開始下載文件.
//通過輸出流將excel下載到客戶瀏覽器
ServletOutputStream outputStream = response.getOutputStream();
excel.write(outputStream);
//關(guān)閉資源
outputStream.close();
excel.close();
業(yè)務(wù)層完整代碼文章來源:http://www.zghlxwxcb.cn/news/detail-825842.html
/**
* 導(dǎo)出運(yùn)行數(shù)據(jù)報表
*
* @param response
*/
@Override
public void exportBusinessData(HttpServletResponse response) {
//查數(shù)據(jù)庫獲取營業(yè)數(shù)據(jù)
LocalDate dateBegin = LocalDate.now().minusDays(30);
LocalDate dateEnd = LocalDate.now().minusDays(1);
//將日期轉(zhuǎn)換為詳細(xì)時間
LocalDateTime dateStartPoint = LocalDateTime.of(dateBegin, LocalTime.MIN);
LocalDateTime dateEndPoint = LocalDateTime.of(dateEnd, LocalTime.MAX);
BusinessDataVO businessData = workspaceService.getBusinessData(dateStartPoint, dateEndPoint);
ClassLoader loader = getClass().getClassLoader();
InputStream stream = loader.getResourceAsStream("template/運(yùn)營數(shù)據(jù)報表模板.xlsx");
log.info("讀入的輸入流stream{}",stream);
//從類路徑下邊讀取資源
InputStream inputStream = ReportServiceImpl.class.getClassLoader().getResourceAsStream("template/運(yùn)營數(shù)據(jù)報表模板.xlsx");
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/運(yùn)營數(shù)據(jù)報表模板.xlsx");
try {
//基于模版文件創(chuàng)建一個新的excel文件
XSSFWorkbook excel=new XSSFWorkbook(inputStream);
XSSFSheet sheet1 = excel.getSheet("Sheet1");
//填充數(shù)據(jù),填充時間
sheet1.getRow(1).getCell(1).setCellValue("時間"+dateBegin+"至"+dateEnd);
//獲得第四行
XSSFRow row=sheet1.getRow(3);
row.getCell(2).setCellValue(businessData.getTurnover());
row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
row.getCell(6).setCellValue(businessData.getNewUsers());
//獲得第五行
row=sheet1.getRow(4);
row.getCell(2).setCellValue(businessData.getValidOrderCount());
row.getCell(4).setCellValue(businessData.getUnitPrice());
//填充明細(xì)數(shù)據(jù)
for (int i=0;i<30;i++){
LocalDate date=dateBegin.plusDays(1);
//查詢某一天的數(shù)據(jù)
workspaceService.getBusinessData(LocalDateTime.of(dateBegin,LocalTime.MIN),
LocalDateTime.of(dateBegin,LocalTime.MAX));
//獲取某一行
row = sheet1.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(5).setCellValue(businessData.getNewUsers());
}
//通過輸出流將excel下載到客戶瀏覽器
ServletOutputStream outputStream = response.getOutputStream();
excel.write(outputStream);
//關(guān)閉資源
outputStream.close();
excel.close();
} catch (IOException e) {
e.printStackTrace();
}
//將查詢到的數(shù)據(jù)寫入到Excel文件中
}
實(shí)現(xiàn)效果
備注:并不是下載了5個(是我之前測試用的提前下載了4個)文章來源地址http://www.zghlxwxcb.cn/news/detail-825842.html
到了這里,關(guān)于《蒼穹外賣》知識梳理P11-Apache POI導(dǎo)出報表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!