引入poi及JavaBean的beanutils依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
创建导出Excel的工具类:
public class ExportExcelUtil {
/**
* 导出Excel表格的头部标题
* @param headTitle
* @param cellRangeAddressLength
* @return
*/
public static HSSFWorkbook makeExcelHead(String headTitle,int cellRangeAddressLength) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle styleTitle = createStyle(workbook, (short) 16);
HSSFSheet sheet = workbook.createSheet(headTitle);
sheet.setDefaultColumnWidth(25);
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, cellRangeAddressLength);
sheet.addMergedRegion(cellRangeAddress);
HSSFRow rowTitle = sheet.createRow(0);
HSSFCell cellTitle = rowTitle.createCell(0);
// 为标题设置背景颜色
styleTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
styleTitle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellTitle.setCellValue(headTitle);
cellTitle.setCellStyle(styleTitle);
return workbook;
}
/**
* 设定二级标题
* @param workbook
* @param secondTitles
* @return
*/
public static HSSFWorkbook makeSecondHead(HSSFWorkbook workbook, String[] secondTitles){
// 创建用户属性栏
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow rowField = sheet.createRow(1);
HSSFCellStyle styleField = createStyle(workbook, (short)13);
for (int i = 0; i < secondTitles.length; i++) {
HSSFCell cell = rowField.createCell(i);
cell.setCellValue(secondTitles[i]);
cell.setCellStyle(styleField);
}
return workbook;
}
/**
* 添加导出数据
* @param workbook
* @param dataList
* @param beanProperties
* @return
*/
public static <T> HSSFWorkbook exportExcelData(HSSFWorkbook workbook, List<T> dataList, String[] beanProperties) {
HSSFSheet sheet = workbook.getSheetAt(0);
// 填充数据
HSSFCellStyle styleData = workbook.createCellStyle();
styleData.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleData.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
for (int j = 0; j < dataList.size(); j++) {
HSSFRow rowData = sheet.createRow(j + 2);
T t = dataList.get(j);
for(int k = 0; k < beanProperties.length; k++){
Object value = null;
try {
value = BeanUtils.getProperty(t,beanProperties[k]);
} catch (Exception e) {
e.printStackTrace();
}
HSSFCell cellData = rowData.createCell(k);
cellData.setCellValue(value.toString());
cellData.setCellStyle(styleData);
}
}
return workbook;
}
/**
* 添加导出文件名称并指定文件存放位置
* @param fileName
* @param response
*/
public static void makeExportExcelFileName(HSSFWorkbook workbook,String fileName,HttpServletResponse response) {
//添加导出文件名称
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
} catch (Exception e) {
e.printStackTrace();
}
//将文件存到指定位置
try {
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 提取公共的样式
* @param workbook
* @param fontSize
* @return
*/
private static HSSFCellStyle createStyle(HSSFWorkbook workbook, short fontSize){
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 创建一个字体样式
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(fontSize);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
return style;
}
}
业务代码部分:
//生成RecoupedTradeInfo实体类的List集合
List<RecoupedTradeInfo> tradeInfoList = new ArrayList<RecoupedTradeInfo>();
//1.生成头部标题 cellRangeAddressLength:从0开始
HSSFWorkbook workbook1 = ExportExcelUtil.makeExcelHead("号码营销订单分析",5);
//2.二级标题名称 创建用户属性栏
String[] secondTitles = {"订单编号","拍下时间","订单状态","订单金额","付款时间","订单来源"};
HSSFWorkbook workbook2 = ExportExcelUtil.makeSecondHead(workbook1,secondTitles);
//3.添加对应entity的属性名称
String[] beanProperty = {"tradeId","taobaoCreate","status","payment","payTime","tradeFrom"};
//4.添加数据
HSSFWorkbook workbook = ExportExcelUtil.exportExcelData(workbook2,tradeInfoList,beanProperty);
//5.添加Excel文件名称并指定文件存放位置
String fileName = "号码营销订单分析"+ DateUtil.yyyyMMddHHmmss.format(new Date())+".xls";
ExportExcelUtil.makeExportExcelFileName(workbook,fileName,response);
网友评论