Excel格式報表生成 (POI技術)
阿新 • • 發佈:2018-10-31
匯入poi報表需要的jar包
<poi.version>3.11</poi.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
< groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId >
<version>${poi.version}</version>
</dependency>
1 waybill_manage.html 頁面新增匯出按鈕
2 後臺新增Controller,並提供下載方法
3 Service提供查詢所有運單資料的方法
1、 新增匯出按鈕(匯出excel報表)
<a id="exportXlsBtn" icon="icon-print" href="#" class="easyui-linkbutton" plain="true">匯出Excel報表</a>
2、 新增匯出事件
// 匯出Excel 按鈕
$("#exportXlsBtn").click(function(){
// 下載效果
$.ajax({
type: "GET",
url: "/report/exportXls"
});
});
3、 建立包com.czxy.bos.print.ReportController,編寫ReportController 新增 exportXls 方法
• POI生成Excel 步驟寫Excel過程一樣:
新建Excel文件(HSSFWorkbook) – 新建(Sheet) – 新建(Row) – 新建(Cell單元格) – 寫單元格資料
• POI 生成HSSF (xls)和XSSF (xlsx)
POI匯出的步驟:
1 建立工作簿
2 建立工作表
3 建立行
4 建立單元格
5 設定內容
6 設定內容格式
7 下載
設定列寬
設定小標題樣式
完整程式碼
package com.czxy.bos.controller.print;
import com.czxy.bos.domain.take_delivery.WayBill;
import com.czxy.bos.service.take_delivery.WayBillService;
import com.czxy.bos.util.DownloadUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private WayBillService wayBillService;
@GetMapping("exportXls")
public void exportXls(HttpServletResponse response) throws Exception{
/**
* 查詢資料之後,下面只需要將內容寫進xls中,然後下載
*/
List<WayBill> wayBillList = wayBillService.findAllWayBill();
//1 建立工作簿 xls HSSFWorkbook xlsx XSSFWorkbook
Workbook wb = new XSSFWorkbook();
//2 建立工作表
Sheet sheet = wb.createSheet();
// 設定列寬---1/256 一個字元的寬度
sheet.setColumnWidth(0,15*256);
sheet.setColumnWidth(1,15*256);
sheet.setColumnWidth(2,15*256);
sheet.setColumnWidth(3,25*256);
sheet.setColumnWidth(4,25*256);
sheet.setColumnWidth(5,25*256);
sheet.setColumnWidth(6,25*256);
sheet.setColumnWidth(7,25*256);
sheet.setColumnWidth(8,25*256);
/**
* 定義公共變數
*/
int rowNo=0,cellNo=0;//行號 和 列號
Row nRow = null;// 行物件通用變數
Cell nCell = null;// 單元格物件通用變數
/****************大標題列印****************/
//3 建立行
nRow = sheet.createRow(rowNo);
//4 建立單元格
nCell = nRow.createCell(cellNo);
//5 設定內容
nCell.setCellValue("bos專案運單表統計"+new Date().toLocaleString());
//6 設定內容格式
// 合併單元格 //引數1:起始行 引數2:終止行 引數3:起始列 引數4:終止列
sheet.addMergedRegion(new CellRangeAddress(0, 0, (short) 0, (short) 9));
// 垂直居中 + 水平居中 + 加粗
CellStyle bigTitleCellStyle = bigTitleStyle(wb);
nCell.setCellStyle(bigTitleCellStyle);
/****************小標題列印****************/
String[] titles={"編號id","運單編號","訂單編號","寄件人姓名","寄件人電話","寄件人地址","收件人姓名","收件人電話","收件人地址"};
// 進入小標題列印的時候,行號變化嗎?rowNo=0
rowNo++;
// 進入小標題列印的時候,列號需要變化嗎?cellNo = 0;
//3 建立行
nRow = sheet.createRow(rowNo);
for (String title:titles){
//4 建立單元格
nCell = nRow.createCell(cellNo++);// 先建立單元格,然後在新增
//5 設定內容
nCell.setCellValue(title);
//6 設定內容格式
nCell.setCellStyle(titleStyle(wb));
}
/****************內容列印****************/
// 單元格需要變化嗎
rowNo++;
cellNo=0;
for(WayBill wayBill:wayBillList){
//3 建立行
nRow = sheet.createRow(rowNo++);
//4 建立單元格
//id
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getId());
nCell.setCellStyle(contentStyle(wb));
//wayBillNum
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getWayBillNum());
nCell.setCellStyle(contentStyle(wb));
//orderid
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getOrderId());
nCell.setCellStyle(contentStyle(wb));
//寄件人姓名
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getSendName());
nCell.setCellStyle(contentStyle(wb));
//寄件人電話
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getSendMobile());
nCell.setCellStyle(contentStyle(wb));
//寄件人地址
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getSendAddress());
nCell.setCellStyle(contentStyle(wb));
//收件人姓名
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getRecName());
nCell.setCellStyle(contentStyle(wb));
//收件人電話
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getRecMobile());
nCell.setCellStyle(contentStyle(wb));
//收件人地址
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(wayBill.getRecAddress());
nCell.setCellStyle(contentStyle(wb));
//cellNo規0
cellNo = 0;
}
/****************下載****************/
DownloadUtil downloadUtil = new DownloadUtil();
//ByteArrayOutputStream byteArrayOutputStream -- 輸出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 將wb寫進流
wb.write(byteArrayOutputStream);
// HttpServletResponse response -- response
// String returnName -- 下載的名字
downloadUtil.download(byteArrayOutputStream,response,"運單表.xlsx");
System.out.println("okokokok....");
}
/**
* 垂直居中 + 水平居中 + 加粗
* @param wb
* @return
*/
public CellStyle bigTitleStyle(Workbook wb){
// 建立格式
CellStyle cellStyle = wb.createCellStyle();
// 水平對齊方式
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 垂直居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 設定字型
Font font = wb.createFont();
// 是數值的1/20 大小
font.setFontHeight((short) 480);
font.setBold(true);
font.setColor(Font.COLOR_RED);
cellStyle.setFont(font);
return cellStyle;
}
public CellStyle titleStyle(Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
Font font = wb.createFont();
font.setFontHeight((short)300);
cellStyle.setFont(font);
return cellStyle;
}
public CellStyle contentStyle(Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
Font font = wb.createFont();
font.setFontHeight((short)200);
cellStyle.setFont(font);
return cellStyle;
}
}
4、 編寫WayBillService.java程式碼 (程式碼效果類似之前運單查詢程式碼 )
public List<WayBill> findAll() {
return wayBillMapper.selectAll();
}
【匯出excel的效果圖】
總結:
1 確認POI匯出的步驟
2 先匯出大標題+下載—合併單元格,橫向縱向居中
3 小標題–邊框線的設定
4 內容