工作臺
需求分析
Apache POI
介紹
入門案例
package com.sky.test;
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.FileOutputStream;
/**
* 使用POI操作excel文件
*/
public class POITest {
/**
* 通過POI創(chuàng)建excel文件并寫入文件內(nèi)容
*/
public static void write() throws Exception{
//在內(nèi)存中創(chuàng)建excel文件
XSSFWorkbook excel = new XSSFWorkbook();
//在excel文件中創(chuàng)建sheet頁
XSSFSheet sheet = excel.createSheet("info");
//在sheet中創(chuàng)建行對象 rownum編號是從0開始的
XSSFRow row = sheet.createRow(1);
//在行上面創(chuàng)建單元格 并寫入文件內(nèi)容
row.createCell(1).setCellValue("姓名");
//這一句和上一句相當于在第二行的第二格與第三格 寫入信息
row.createCell(2).setCellValue("城市");
//創(chuàng)建一個新行
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)存中的excel文件寫出到磁盤
FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(out);
//關閉資源
out.close();
excel.close();
}
public static void main(String[] args) throws Exception{
write();
}
}
結(jié)果
public static void read() throws Exception{
FileInputStream inputStream = new FileInputStream(new File("D:\\info.xlsx"));
//讀取磁盤上已存放的excel文件
XSSFWorkbook excel = new XSSFWorkbook(inputStream);
//讀取excel文件中的第一個sheet頁
XSSFSheet sheet = excel.getSheet("info");
//獲取最后一行的行號
int lastRowNum = sheet.getLastRowNum();
for (int i = 1;i <= lastRowNum; i++){
//獲得某一行
XSSFRow row = sheet.getRow(i);
//獲得單元格對象
String cellValue1 = row.getCell(1).getStringCellValue();
String cellValue2 = row.getCell(2).getStringCellValue();
System.out.println(cellValue1 + " " + cellValue2);
}
//關閉資源
excel.close();
inputStream.close();
public static void main(String[] args) throws Exception{
// write();
read();
}
}
導出運營數(shù)據(jù)Excel報表
需求分析
代碼開發(fā)
Controller層文章來源:http://www.zghlxwxcb.cn/news/detail-823174.html
/**
* 導出運營數(shù)據(jù)報表
* @param response
*/
@GetMapping("/export")
@ApiOperation("導出運營數(shù)據(jù)報表")
public void export(HttpServletResponse response){
reportService.exportBusinessData(response);
}
Service實現(xiàn)類文章來源地址http://www.zghlxwxcb.cn/news/detail-823174.html
/**
* 導出運營數(shù)據(jù)報表
* @param response
*/
@Override
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ù)
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);
//填充數(shù)據(jù)--時間
XSSFSheet sheet1 = excel.getSheet("sheet1");
sheet1.getRow(1).getCell(1).setCellValue("時間:" + dateBegin + "至" + dateEnd);
//獲得第四行
sheet1.getRow(3).getCell(2).setCellValue(businessDataVO.getTurnover());
sheet1.getRow(3).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
sheet1.getRow(3).getCell(6).setCellValue(businessDataVO.getNewUsers());
//獲得第五行
XSSFRow row = sheet1.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 = 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(6).setCellValue(businessData.getNewUsers());
}
//3、通過輸出流將excel文件下載到客戶端瀏覽器
ServletOutputStream out = response.getOutputStream();
excel.write(out);
//關閉資源
out.close();
excel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
到了這里,關于黑馬蒼穹外賣學習Day12的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!