XSSFWorkbook实现导出excel
Posted wzj666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XSSFWorkbook实现导出excel相关的知识,希望对你有一定的参考价值。
1.pom.xml中加入poi依赖:
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.10-FINAL</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
2.创建导出excel文件模板;
3.controller层实现代码:
@PostMapping("/excelCarrierOrder")
public ResponseEntity<Resource> excelCarrierOrder(@RequestBody Map<String,Object> params,HttpServletResponse response) throws IOException{
//列表数据
XSSFWorkbook workbook = carrierOrderService.excelCarrierOrder(params);
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("charset", "utf-8");
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String name =sdf.format(new Date()) + ".xlsx";
response.setHeader("Content-Disposition", "attachment;filename=\\"" + URLEncoder.encode(name, "UTF-8") + "\\"");
workbook.write(response.getOutputStream());
if (null != os) {
os.close();
}
return new ResponseEntity<Resource>(null, null, HttpStatus.OK);
}
4.service实现类实现代码:
@Override
public XSSFWorkbook excelCarrierOrder(Map<String, Object> params) throws IOException {
//模板路径
String path = "/templates/excel/订单列表.xlsx";
XSSFWorkbook workboot = new XSSFWorkbook(this.getClass().getResourceAsStream(path));
//sheet页(默认sheet1页),也可通过workboot.createSheet("订单列表")创建sheet页
XSSFSheet sheetAt = workboot.getSheetAt(0);
//订单基本表数据
List<CarrierOrderPojo> list = mapper.getExcelCarrierOrder(params);
//开始创建行和表格并且加入数据
//行
XSSFRow row;
int i = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
if(null != list && !list.isEmpty()) {
for (CarrierOrderPojo dto : list) {
//根据订单id查询车辆信息
List<CarrierOrderTruck> truckList = truckMapper.getTruckList(dto.getId());
//根据订单id查询装卸车费用
List<CarrierOrderServe> serveList = serveMapper.getServeList(dto.getId());
//根据订单id查询采购详情
List<CarrierOrderDetail> detailList = detailMapper.getCarrierOrderDetailList(dto.getId());
String purchaseOrderNumber = "";
i += 1;
//创建行
row = sheetAt.createRow(i);
//承运商
if(null != dto.getCarrierName()) {
row.createCell(0).setCellValue(dto.getCarrierName());
}else {
row.createCell(0).setCellValue("");
}
//物流单号
if(null != dto.getLogisticsNumber()) {
row.createCell(1).setCellValue(dto.getLogisticsNumber());
}else {
row.createCell(1).setCellValue("");
}
//采购单号
if(null != detailList && !detailList.isEmpty()) {
for (CarrierOrderDetail detaildto : detailList) {
purchaseOrderNumber += detaildto.getPurchaseOrderNumber()+",";
}
row.createCell(2).setCellValue(purchaseOrderNumber.substring(0, purchaseOrderNumber.length()-1));
}else {
row.createCell(2).setCellValue("");
}
//发货时间
if(null != dto.getTakingDate()) {
row.createCell(3).setCellValue(sdf.format(dto.getTakingDate()));
}else {
row.createCell(3).setCellValue("");
}
//到货时间
if(null != dto.getDeliveryDate()) {
row.createCell(4).setCellValue(sdf.format(dto.getDeliveryDate()));
}else {
row.createCell(4).setCellValue("");
}
//总体积
if(null != dto.getTotalAllVolume()) {
row.createCell(5).setCellValue(dto.getTotalAllVolume()+"m³");
}else {
row.createCell(5).setCellValue("");
}
//箱型
if(null != truckList && !truckList.isEmpty()) {
for (CarrierOrderTruck truckdto : truckList) {
if("面包车".equals(truckdto.getTruckType())) {
row.createCell(6).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("金杯车".equals(truckdto.getTruckType())){
row.createCell(7).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("2米7平板".equals(truckdto.getTruckType())){
row.createCell(8).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("3米8平板".equals(truckdto.getTruckType())){
row.createCell(9).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("4米2平板".equals(truckdto.getTruckType())){
row.createCell(10).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("4米2厢车".equals(truckdto.getTruckType())){
row.createCell(11).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("4米2高栏".equals(truckdto.getTruckType())){
row.createCell(12).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("6米8厢车".equals(truckdto.getTruckType())){
row.createCell(13).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("6米8高栏".equals(truckdto.getTruckType())){
row.createCell(14).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("7米6厢车".equals(truckdto.getTruckType())){
row.createCell(15).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("9米6厢车".equals(truckdto.getTruckType())){
row.createCell(16).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("9米6高栏".equals(truckdto.getTruckType())){
row.createCell(17).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("12米5厢车".equals(truckdto.getTruckType())){
row.createCell(18).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("13米高栏".equals(truckdto.getTruckType())){
row.createCell(19).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("16米5厢车".equals(truckdto.getTruckType())){
row.createCell(20).setCellValue(truckdto.getTruckNumber()+"辆");
}else if("17米5厢车".equals(truckdto.getTruckType())){
row.createCell(21).setCellValue(truckdto.getTruckNumber()+"辆");
}
}
}
//送货距离
if(null != dto.getDistance()) {
row.createCell(22).setCellValue(dto.getDistance()+"km");
}else {
row.createCell(22).setCellValue("");
}
//运输费用
if(null != dto.getFreight()) {
row.createCell(23).setCellValue(dto.getFreight()+"元");
}else {
row.createCell(23).setCellValue("");
}
//装货费用,卸货费用
if(null != serveList && !serveList.isEmpty()) {
for (CarrierOrderServe servedto : serveList) {
if("装货".equals(servedto.getServiceType())) {
row.createCell(24).setCellValue(servedto.getPrice()+"元");
}else if("卸货".equals(servedto.getServiceType())){
row.createCell(25).setCellValue(servedto.getPrice()+"元");
}
}
}
//异常费用
if(null != dto.getOtherCost()) {
row.createCell(26).setCellValue(dto.getOtherCost()+"元");
}else {
row.createCell(26).setCellValue("");
}
//合计
if(null != dto.getTotalCost()) {
row.createCell(27).setCellValue(dto.getTotalCost()+"元");
}else {
row.createCell(27).setCellValue("");
}
//备注
if(null != dto.getCarrierRemark()) {
row.createCell(28).setCellValue(dto.getCarrierRemark());
}else {
row.createCell(28).setCellValue("");
}
}
}
return workboot;
}
5.项目中引入第2步创建的模板,要与Service实现类中path路径对应:
6.效果:
7.XSSFWorkbook 做导出也可以不引入模板,通过workboot.createSheet("订单列表")创建sheet页,并设定表格样式,可实现相同的效果。但是我觉得导入模板比较简单,容易上手。
如有错误,请指出,谢谢!
以上是关于XSSFWorkbook实现导出excel的主要内容,如果未能解决你的问题,请参考以下文章
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载