导入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 内容