1.package com.enation.app.javashop.framework.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class CreateWorkerSheetUtil {
public static HSSFWorkbook createWorkerSheet(String[] headers, String sheetName) {
HSSFWorkbook workBook = new HSSFWorkbook();// 创建excel工作簿
// 设置表头字体样式
HSSFFont headfont = workBook.createFont();
headfont.setFontName("黑体");
headfont.setFontHeightInPoints((short) 22);// 字体大小
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
HSSFCellStyle headstyle = workBook.createCellStyle();
headstyle.setFont(headfont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle.setLocked(true);
headstyle.setWrapText(true);// 自动换行
// 设置列头字体样式
HSSFFont columnHeadFont = workBook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 11);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置列头的样式
HSSFCellStyle columnHeadStyle = workBook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
HSSFFont font = workBook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 11);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置普通单元格样式
HSSFCellStyle style = workBook.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
style.setWrapText(true);
HSSFCellStyle centerstyle = workBook.createCellStyle();
centerstyle.setFont(font);
centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
centerstyle.setWrapText(true);
HSSFSheet sheet = workBook.createSheet(sheetName);// 创建一个工作表sheet,名为sheetName
// 对第一行做处理,标题的创建
HSSFRow row = sheet.createRow(0);// 创建第一行
row.setHeight((short) 600);// 设置表格行的高度
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(new HSSFRichTextString(sheetName));// 设置表名
cell0.setCellStyle(headstyle);
//合并第一行单元格
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 16);
sheet.addMergedRegion(range);
//第二行列名
HSSFRow row2 = sheet.createRow(1);
row2.setHeight((short) 500);
HSSFCell cell = null;
// 插入第一行数据标题
for (int i = 0; i < headers.length; i++) {
cell = row2.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(columnHeadStyle);
sheet.setColumnWidth(i, 5500);
}
return workBook;
}
}
2.调用
String sheetName = "预收统计表";
String[] headers = { "订单编号", "收款日期","对应日期","床位号", "管理老师", "护工姓名", "病人姓名", "预收金额", "支付方式",
"病患联系方式"};
// 创建excel工作簿
HSSFWorkbook workBook = CreateWorkerSheetUtil.createWorkerSheet(headers, sheetName);
//获取工作表sheet
HSSFSheet sheet = workBook.getSheet(sheetName);
//插入数据行
if (prepayInfoReportVos.size() > 0) {
for (int i = 0; i < prepayInfoReportVos.size(); i++) {
PrepayInfoReportVo prepayInfoReportVo = prepayInfoReportVos.get(i);
HSSFRow nextRow = sheet.createRow(i + 2);
nextRow.createCell(0).setCellValue(prepayInfoReportVo.getOrderSn());
nextRow.createCell(1).setCellValue(prepayInfoReportVo.getReceiveDate());
nextRow.createCell(2).setCellValue(prepayInfoReportVo.getCorrespondDate());
nextRow.createCell(3).setCellValue(prepayInfoReportVo.getShipAddr());
nextRow.createCell(4).setCellValue(prepayInfoReportVo.getManagerName());
// nextRow.createCell(4).setCellValue(String.valueOf(prepayInfoReportVo.getNursingName()));
nextRow.createCell(5).setCellValue(StringUtils.strip(prepayInfoReportVo.getNursingName().toString(),"[]"));
nextRow.createCell(6).setCellValue(prepayInfoReportVo.getShipName());
nextRow.createCell(7).setCellValue(prepayInfoReportVo.getPreReceiveSum());
nextRow.createCell(8).setCellValue(prepayInfoReportVo.getPayType());
nextRow.createCell(9).setCellValue(prepayInfoReportVo.getPatientMobile());
}
}
String sql = "select shop_name from nursing_member.es_shop where shop_id = ?";
String hospitalName = daoSupport.queryForString(sql, hospitalId);
String title = hospitalName + DateUtil.toString(startDate,"yyyy-MM-dd") + "至" + DateUtil.toString(endDate,"yyyy-MM-dd") + "预收统计报表";
response.reset();
try {
response.setHeader("Content-disposition", "attachment; filename=" + new String(title.getBytes("utf-8"),"ISO8859-1") + ".xls");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/msexcel; charset=utf-8");
response.setCharacterEncoding("utf-8");
try {
ServletOutputStream out = response.getOutputStream();
workBook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}