需要加的依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
工具类具体实现
/**
* @Title: service
* @Package cn.wine.supplier.ms.business.util
*/
package cn.wine.supplier.ms.business.util;
import cn.wine.supplier.model.business.enums.OrderStatusEnum;
import cn.wine.supplier.model.business.enums.PurchaseCoinTypeEnum;
import cn.wine.supplier.model.business.vo.resp.BusinessResourceOrderRespVO;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import java.io.FileOutputStream;
import java.util.List;
/***********************************
* @ClassName: ToExclUtils
* @Description: TODO
* @author: tangh
* @createdAt: 2019年01月29日 10:51
***********************************/
public class ToExclUtils {
public ToExclUtils() {
}
public static HSSFCell getCell(HSSFSheet sheet, int row, int col) {
HSSFRow sheetRow = sheet.getRow(row);
if (sheetRow == null){
sheetRow = sheet.createRow(row);
}
HSSFCell cell = sheetRow.getCell(col);
if (cell == null){
cell = sheetRow.createCell(col);
}
return cell;
}
public static void setText(HSSFCell cell, String text) {
cell.setCellType(CellType.STRING);
cell.setCellValue(text);
}
public static void toExcel(List<String> titles, List<BusinessResourceOrderRespVO> list) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
HSSFSheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
HSSFRow row = sheet.createRow(0); // 创建一个行
HSSFCell cell = row.createCell(0); // 创建一个单元格 第1列
// cell.setCellValue(new Date()); // 给单元格设置值
for (int i = 0; i < titles.size(); i++){ // 设置标题
String title = titles.get(i);
cell = getCell(sheet, 0, i);
setText(cell, title);
cell.setCellStyle(style);
}
for (int i = 0; i < list.size(); i++){
BusinessResourceOrderRespVO vpd = list.get(i);
for (int j = 0; j < titles.size(); j++){
StringBuilder str = new StringBuilder();
//订单编号
if (j == 0){
str.append(vpd.getOrderCode() != null ? vpd.getOrderCode() : "");
}//下单时间
else if (j == 1){
str.append(BusinessLocalDateTimeUtils.timestampToLocalDateTime(vpd.getOrderPlaceTime()) != null ? BusinessLocalDateTimeUtils.timestampToLocalDateTime(vpd.getOrderPlaceTime()) : "");
}//买家名字
else if (j == 2){
str.append(vpd.getSupplierName() != null ? vpd.getSupplierName() : "");
}
cell = getCell(sheet, i + 1, j);
setText(cell, String.valueOf(str));
cell.setCellStyle(style);
}
}
FileOutputStream fileOut = new FileOutputStream("D://成员信息表.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}
方法调用
public void toExcel(BusinessResourceOrderManagementPageVo vo) {
//查询所有数据
SpringPageVO<BusinessResourceOrderRespVO> result=findResouceOrderByOwnerOrCode(vo);
List<BusinessResourceOrderRespVO> orderRespVOS=result.getContent();
List<String> title=Lists.newArrayList();
title.add("订单编号");
title.add("下单时间");
try{
ToExclUtils.toExcel(title,orderRespVOS);
}
catch (Exception e){
e.printStackTrace();
AssertUtils.isTrue(true, BusinessExceptionType.IS_NULL, "导出失败");
}