1.公用工具类
/**
* 导出
*
* @param sheetName
* @param titleName
* @param fileName
* @param columnNumber
* @param columnWidth
* @param columnName
* @param dataList
* @param response
* @throws Exception
*/
public static void exportWithResponse(String sheetName, String titleName,
String fileName, int columnNumber, int[] columnWidth,
String[] columnName, String[][] dataList,
HttpServletResponse response) throws Exception {
log.info("columnNumber:{}" , columnName.length);
log.info("columnWidth.length:{}" , columnWidth.length);
log.info("columnName.length:{}" , columnName.length);
if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //统一设置列宽
for (int i = 0; i < columnNumber; i++) {
for (int j = 0; j <= i; j++) {
if (i == j) {
sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
}
}
}
// 创建第0行 也就是标题
HSSFRow row1 = sheet.createRow((int) 0);
row1.setHeightInPoints(50);// 设备标题的高度
// 第三步创建标题的单元格样式style2以及字体样式headerFont1
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HorizontalAlignment.CENTER);
style2.setVerticalAlignment(VerticalAlignment.CENTER);
style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
//headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
headerFont1.setFontName("黑体"); // 设置字体类型
headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
style2.setFont(headerFont1); // 为标题样式设置字体样式
HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
columnNumber - 1)); // 合并列标题
cell1.setCellValue(titleName); // 设置值标题
cell1.setCellStyle(style2); // 设置标题样式
// 创建第1行 也就是表头
HSSFRow row = sheet.createRow((int) 1);
row.setHeightInPoints(37);// 设置表头高度
// 第四步,创建表头单元格样式 以及表头的字体样式
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true);// 设置自动换行
style.setBottomBorderColor(HSSFColor.BLACK.index);
HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont.setFontName("黑体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
style.setFont(headerFont); // 为标题样式设置字体样式
// 第四.一步,创建表头的列
for (int i = 0; i < columnNumber; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(columnName[i]);
cell.setCellStyle(style);
}
// 第五步,创建单元格,并设置值
for (int i = 0; i < dataList.length; i++) {
row = sheet.createRow((int) i + 2);
// 为数据内容设置特点新单元格样式1 自动换行 上下居中
// HSSFCellStyle zidonghuanhang = wb.createCellStyle();
// zidonghuanhang.setWrapText(true);// 设置自动换行
// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
// HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
// zidonghuanhang2.setWrapText(true);// 设置自动换行
// 设置边框
HSSFCell datacell = null;
for (int j = 0; j < columnNumber; j++) {
datacell = row.createCell(j);
datacell.setCellValue(dataList[i][j]);
// datacell.setCellStyle(zidonghuanhang2);
}
}
// 第六步,将文件存到浏览器设置的下载位置
String filename = fileName + ".xls";
response.setContentType("application/ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
OutputStream out = response.getOutputStream();
try {
wb.write(out);// 将数据写出去
String str = "导出" + fileName + "成功!";
log.info(str);
} catch (Exception e) {
e.printStackTrace();
String str1 = "导出" + fileName + "失败!";
log.info(str1);
} finally {
out.close();
}
} else {
log.info("列数目长度名称三个数组长度要一致");
}
}
2.调用
/**
* 订单列表导出
*
* @param request
* @param response
*/
@RequestMapping(value = "/toExcel", produces = "application/json;charset=utf-8")
@ResponseBody
public void toExcel(HttpServletRequest request, HttpServletResponse response) {
//查询
List<OrderModel> orderModels = orderService.getPageList(orderRequest);
String sheetName = "订单";
String titleName = "订单导出";
String fileName = "订单导出" + CommonTool.getDateTime();
int[] columnWidth = {20, 20, 20};
String[] columnName = {"订单号", "内部订单号", "备注"
};
String[][] dataList = new String[orderDTOS.size()][columnName.length];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int columnNumber = columnName.length;
try {
for (int i = 0; i < orderDTOS.size(); i++) {
dataList[i][0] = orderDTOS.get(i).getOrderModel().getNumber();
dataList[i][1] = orderDTOS.get(i).getOrderModel().getUnionOrderNumber();
dataList[i][2] = orderDTOS.get(i).getOrderModel().getRemark();
if (orderDTOS.get(i).getOrderModel().getState() == OrderConst.ORDER_STATE_CANCELLED) {
dataList[i][3] = "已取消";
} else if (orderDTOS.get(i).getOrderModel().getState() == OrderConst.ORDER_STATE_COMPLETED) {
dataList[i][3] = "已完成";
} else if (orderDTOS.get(i).getOrderModel().getState() == OrderConst.ORDER_STATE_TO_BE_CONFIRMED) {
if (orderDTOS.get(i).getOrderModel().getDriverUid() == 0) {
dataList[i][3] = "已发布";
} else {
dataList[i][3] = "待确认协议";
}
} else if (orderDTOS.get(i).getOrderModel().getState() == OrderConst.ORDER_STATE_TO_CONFIRM_RECEIPT) {
dataList[i][3] = "待签收";
} else {
dataList[i][3] = "运输中";
}
}
CommonTool.exportWithResponse(sheetName, titleName, fileName,
columnNumber, columnWidth, columnName, dataList, response);
return;
} catch (Exception e) {
e.printStackTrace();
}
return;
}