public void exportManySheetExcel(HttpServletResponse response, Map pageRequest) {
List<String> policys = (List<String>) pageRequest.get("policys");
List<Map> title = (List<Map>) pageRequest.get("title");
int l = title.size();
String myfileName = (String) pageRequest.get("fileName");
String[] handerscustom = {"客户名称", "客户ID", "收单方名称", "收单方ID", "客户销售属性",
"客户分类", "类型", "条件客户标识"};//汉字 客户
String[] handerscustomyw = {"customerName", "customerId", "customerReceiveName",
"customerReceiveId", "customerSaleAttr"};//字母 客户
String[] myhandersgoods = {"通用名称", "货品ID","货品编号", "商品规格", "商品单位", "产地",
"生产厂商", "协议价", "最高价", "折扣价", "明细说明","采购员编码", "采购员姓名"};//汉字 商品
String[] myhandersgoodsyw = {"goodsName", "goodsId", "goodsCode", "goodsSpec", "goodsUnit", "goodsOrigin",
"goodsMake", "agreementPrice", " maxPrice", "discountPrice", "memo",};//字母 商品
int cusl = handerscustom.length;
int goodsl = myhandersgoods.length;
String[] handers = new String[l + cusl];//汉字
String[] handersyw = new String[l + cusl];//字母
String[] myhanders = new String[l + goodsl];//汉字
String[] myhandersyw = new String[l + goodsl];//字母
int num = 0;
ZipOutputStream zipOutputStream = null;
String downloadFilename = null;//转换中文否则可能会产生乱码
try {
downloadFilename = URLEncoder.encode(myfileName + ".zip", "UTF-8");
zipOutputStream = new ZipOutputStream(response.getOutputStream());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
for (Map m : title) {
String t = (String) m.get("title");
String n = (String) m.get("key");
handers[num] = t;
myhanders[num] = t;
handersyw[num] = n;
myhandersyw[num] = n;
num++;
}
for (int i = 0; i < handerscustom.length; i++) {
handers[num + i] = handerscustom[i];
handersyw[num + i] = handerscustomyw[i];
}
for (int i = 0; i < myhandersgoods.length; i++) {
myhanders[num + i] = myhandersgoods[i];
myhandersyw[num + i] = myhandersgoodsyw[i];
}
ByteArrayInputStream is = null;
BufferedInputStream bis = null;
HSSFWorkbook wb = null;
List<PolicyCustomerDto> customerList = null;
try {
for (String id : policys) {
wb = new HSSFWorkbook();//创建工作薄
HSSFCellStyle style = wb.createCellStyle();//表头样式
//字体样式
HSSFFont fontStyle = wb.createFont();
fontStyle.setFontName("微软雅黑");
fontStyle.setFontHeightInPoints((short) 12);
style.setFont(fontStyle);
//新建一个sheet
HSSFSheet sheet = wb.createSheet("客户");
HSSFRow rowFirst = sheet.createRow(0);//第一个sheet的第一行为标题
customerList = policyCustomerService.queryPolicyCustomerDtoList(id);
for (int i = 0; i < handers.length; i++) {
HSSFCell cell = rowFirst.createCell(i);
cell.setCellValue(handers[i]);
cell.setCellStyle(style); //加样式
}
for (int line = 0; line < customerList.size(); line++) {
PolicyCustomerDto policyDto = customerList.get(line);
HSSFRow row = sheet.createRow(line + 1);
for (int i = 0; i < handersyw.length; i++) {
Object val = getFieldValueByName(handersyw[i], policyDto);
String vals = val == null ? "" : val.toString();
if ("customerClassify".equals(handersyw[i])){
vals = (String) customerClassifyMap.get(vals);
}
if ("customerSaleAttr".equals(handersyw[i])){
vals = (String) salesPropertyMap.get(vals);
}
row.createCell(i).setCellValue(vals);
}
}
//新建一个sheet
HSSFSheet mysheet = wb.createSheet("商品");
HSSFRow myrowFirst = mysheet.createRow(0);//第一个sheet的第一行为标题
List<PolicyGoodsDto> goodsList = policyGoodsService.queryPolicyGoodsDtoList(id);
for (int i = 0; i < myhanders.length; i++) {
//获取第一行的每个单元格
HSSFCell cell2 = myrowFirst.createCell(i);
//往单元格里写数据
cell2.setCellValue(myhanders[i]);
cell2.setCellStyle(style); //加样式
}
for (int line = 0; line < goodsList.size(); line++) {
PolicyGoodsDto policyGoodsDto = goodsList.get(line);
HSSFRow row = mysheet.createRow(line + 1);
for (int i = 0; i < myhandersyw.length; i++) {
Object val = getFieldValueByName(myhandersyw[i], policyGoodsDto);
String vals = val == null ? "" : val.toString();
row.createCell(i).setCellValue(vals);
}
}
// 压缩文件名
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
byte[] content = baos.toByteArray();
is = new ByteArrayInputStream(content);
bis = new BufferedInputStream(is);
// 添加zip实体到zip流中
zipOutputStream.putNextEntry(new ZipEntry(customerList.get(0).getPolicyName() + ".xls"));
// 将字节从文件传输到ZIP文件
int len;
while ((len = bis.read(buf)) > 0) {
zipOutputStream.write(buf, 0, len);
}
is.close();
bis.close();// 记得关闭流
zipOutputStream.closeEntry();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////////////////
单元格填充
private static Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[]{});
Object value = method.invoke(o, new Object[]{});
return value;
} catch (Exception e) {
return null;
}
}