public String bomExportService(HttpServletRequest request,HttpServletResponse resp,String bomCode) throws UnsupportedEncodingException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("主BOM");//创建一个sheet-test1
HSSFSheet sheet1 = wb.createSheet("子BOM");//创建一个sheet-test1
//调用浏览器的下载器
request.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/x-download");
String fileName = "导出的数据.xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
//设置单元格风格,居中对齐.
HSSFCellStyle cs = wb.createCellStyle();
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cs.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cs.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cs.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//设置单元格风格,居中对齐.
HSSFCellStyle cs1 = wb.createCellStyle();
cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cs1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cs1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cs1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cs1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//设置字体:
HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 12);//设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
cs.setFont(font);//要用到的字体格式
//sheet.setColumnWidth(0, 3766); //第一个参数代表列下标(从0开始),第2个参数代表宽度值
//cs.setWrapText(true);//设置字体超出宽度自动换行
//设置背景颜色
//cs.setFillBackgroundColor(HSSFColor.BLUE.index);
//cs.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
//创建第一行
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell ;
HSSFRow row1 = sheet1.createRow((short) 0);
HSSFCell cell1 ;
//设置合并的标题头(注意:横向合并的时候,标题头单元格必须长度和内容单元格一致否则合并时会把其他标题头单元格内容挤掉)
//合并(行到行, 列到列)
sheet.addMergedRegion(new CellRangeAddress(0,0,0,13));
sheet.addMergedRegion(new CellRangeAddress(1,1,1,2));
sheet.addMergedRegion(new CellRangeAddress(1,1,6,7));
sheet.addMergedRegion(new CellRangeAddress(2,2,1,2));
sheet.addMergedRegion(new CellRangeAddress(2,2,6,7));
sheet1.addMergedRegion(new CellRangeAddress(0,0,0,13));
sheet1.addMergedRegion(new CellRangeAddress(1,1,1,2));
sheet1.addMergedRegion(new CellRangeAddress(1,1,6,7));
sheet1.addMergedRegion(new CellRangeAddress(2,2,1,2));
sheet1.addMergedRegion(new CellRangeAddress(2,2,6,7));
//设置对应的合并单元格标题
row = sheet.createRow(0);
for (int j = 0; j < 12; j++) {
cell = row.createCell((short)j);
cell.setCellStyle(cs);
cell.setCellValue("BOM清单");
sheet.autoSizeColumn(j);//自动设宽
}
//=================================================================================
// //设置对应的合并单元格标题
row1 = sheet1.createRow(0);
for (int j = 0; j < 12; j++) {
cell1 = row1.createCell((short)j);
cell1.setCellStyle(cs);
cell1.setCellValue("BOM清单");
sheet1.autoSizeColumn(j);//自动设宽
}
//========================================================
String[]arr = {"层级","物料编号","物料名称","物料规格","颜色","材质","单价","用量","单位","制作单位","MOQ(最低库存量)","交期","工位","标准工时(H)"};
String[]arr1 = {"层级","物料编号","物料名称","物料规格","颜色","材质","单价","用量","单位","制作单位","MOQ(最低库存量)","交期","工位","标准工时(H)"};
row = sheet.createRow(3);
for (int j = 0; j < arr.length; j++) {
cell = row.createCell((short)j);
cell.setCellStyle(cs);
cell.setCellValue(arr[j]);
sheet.autoSizeColumn(j);//自动设宽
}
row1 = sheet1.createRow(3);
for (int j = 0; j < arr1.length; j++) {
cell1 = row1.createCell((short)j);
cell1.setCellStyle(cs);
cell1.setCellValue(arr[j]);
sheet1.autoSizeColumn(j);//自动设宽
}
List<Map<String,Object>> list = bomExportDao.queryBomAll(bomCode);
// 设置列值-内容
String grade = null;
List<Map<String,Object>> list1s = new ArrayList<Map<String,Object>>();
for (int i = 0; i < list.size(); i++) {
grade=list.get(i).get("grade").toString();
Map<String,Object> mm = list.get(i);
if(grade.equals("1")){
if(mm!=null){
list1s.add(mm);
}
}
}
for (int i = 0; i < list1s.size(); i++) {
Map map= list1s.get(i);
row = sheet.createRow(1);
cell = row.createCell((short)0);
cell.setCellStyle(cs);
cell.setCellValue("产品名称");
sheet.autoSizeColumn(0);//自动设宽
cell = row.createCell((short)3);
cell.setCellStyle(cs);
cell.setCellValue("版次");
sheet.autoSizeColumn(3);//自动设宽
cell = row.createCell((short)5);
cell.setCellStyle(cs);
cell.setCellValue("客户");
sheet.autoSizeColumn(5);//自动设宽
cell = row.createCell((short)8);
cell.setCellStyle(cs);
cell.setCellValue("编制");
sheet.autoSizeColumn(8);//自动设宽
cell = row.createCell((short)10);
cell.setCellStyle(cs);
cell.setCellValue("审核");
sheet.autoSizeColumn(10);//自动设宽
cell = row.createCell((short)12);
cell.setCellStyle(cs);
cell.setCellValue("批核");
sheet.autoSizeColumn(12);//自动设宽
//-------------------------------------------
row.createCell(1).setCellValue((String)map.get("product_name"));
row.createCell(4).setCellValue((String)map.get("bom_code"));
row.createCell(13).setCellValue("");
row.getCell(13).setCellStyle(cs1);
row = sheet.createRow(2);
cell = row.createCell((short)0);
cell.setCellStyle(cs);
cell.setCellValue("产品编号");
sheet.autoSizeColumn(0);//自动设宽
cell = row.createCell((short)3);
cell.setCellStyle(cs);
cell.setCellValue("颜色");
sheet.autoSizeColumn(3);//自动设宽
cell = row.createCell((short)5);
cell.setCellStyle(cs);
cell.setCellValue("配色");
sheet.autoSizeColumn(5);//自动设宽
cell = row.createCell((short)8);
cell.setCellStyle(cs);
cell.setCellValue("日期");
sheet.autoSizeColumn(8);//自动设宽
cell = row.createCell((short)10);
cell.setCellStyle(cs);
cell.setCellValue("日期");
sheet.autoSizeColumn(10);//自动设宽
cell = row.createCell((short)12);
cell.setCellStyle(cs);
cell.setCellValue("日期");
sheet.autoSizeColumn(12);//自动设宽
cell = row.createCell((short)3);
cell.setCellStyle(cs);
cell.setCellValue("配色");
sheet.autoSizeColumn(0);//自动设宽
row.createCell(1).setCellValue((String)map.get("product_code"));
row.getCell(1).setCellStyle(cs1);
row.createCell(2).setCellValue("");
row.getCell(2).setCellStyle(cs1);
row.createCell(4).setCellValue((String)map.get("color"));
row.getCell(4).setCellStyle(cs1);
row.createCell(6).setCellValue("");
row.getCell(6).setCellStyle(cs1);
row.createCell(7).setCellValue("");
row.getCell(7).setCellStyle(cs1);
row.createCell(9).setCellValue((String)map.get("bom_update_time"));
row.getCell(9).setCellStyle(cs1);
row.createCell(11).setCellValue("");
row.getCell(11).setCellStyle(cs1);
row.createCell(13).setCellValue("");
row.getCell(13).setCellStyle(cs1);
row = sheet.createRow(i + 4);
row.createCell(0).setCellValue((String)map.get("hierarchy"));
row.getCell(0).setCellStyle(cs1);
row.createCell(1).setCellValue((String)map.get("material_code"));
row.getCell(1).setCellStyle(cs1);
row.createCell(2).setCellValue((String)map.get("material_name"));
row.getCell(2).setCellStyle(cs1);
row.createCell(3).setCellValue((String)map.get("spec"));
row.getCell(3).setCellStyle(cs1);
row.createCell(4).setCellValue((String)map.get("colour"));
row.getCell(4).setCellStyle(cs1);
row.createCell(5).setCellValue((String)map.get("raw_material"));
row.getCell(5).setCellStyle(cs1);
row.createCell(6).setCellValue((double)map.get("unitprice"));
row.getCell(6).setCellStyle(cs1);
row.createCell(7).setCellValue((int)map.get("quantity"));
row.getCell(7).setCellStyle(cs1);
row.createCell(8).setCellValue((String)map.get("unit"));
row.getCell(8).setCellStyle(cs1);
row.createCell(9).setCellValue((String)map.get("type"));
row.getCell(9).setCellStyle(cs1);
row.createCell(10).setCellValue((String)map.get("MOQ"));
row.getCell(10).setCellStyle(cs1);
row.createCell(11).setCellValue((String)map.get("delivery_time"));
row.getCell(11).setCellStyle(cs1);
row.createCell(12).setCellValue((String)map.get("station_code"));
row.getCell(12).setCellStyle(cs1);
row.createCell(13).setCellValue((double)map.get("working_hours"));
row.getCell(13).setCellStyle(cs1);
}
String grade1 = null;
List list2s = new ArrayList();
for (int i = 0; i < list.size(); i++) {
grade1=list.get(i).get("grade").toString();
Map mm = list.get(i);
if(grade1.equals("2")){
if(mm!=null){
list2s.add(mm);
}
}
}
for (int i = 0; i < list2s.size(); i++) {
Map map = (Map) list2s.get(i);
grade1=list.get(i).get("grade").toString();
row1 = sheet1.createRow(1);
cell1 = row1.createCell((short)0);
cell1.setCellStyle(cs);
cell1.setCellValue("产品名称");
sheet1.autoSizeColumn(0);//自动设宽
cell1 = row1.createCell((short)3);
cell1.setCellStyle(cs);
cell1.setCellValue("版次");
sheet1.autoSizeColumn(3);//自动设宽
cell1 = row1.createCell((short)5);
cell1.setCellStyle(cs);
cell1.setCellValue("客户");
sheet1.autoSizeColumn(5);//自动设宽
cell1 = row1.createCell((short)8);
cell1.setCellStyle(cs);
cell1.setCellValue("编制");
sheet1.autoSizeColumn(8);//自动设宽
cell1 = row1.createCell((short)10);
cell1.setCellStyle(cs);
cell1.setCellValue("审核");
sheet1.autoSizeColumn(10);//自动设宽
cell1 = row1.createCell((short)12);
cell1.setCellStyle(cs);
cell1.setCellValue("批核");
sheet1.autoSizeColumn(12);//自动设宽
// //-------------------------------------------
row1.createCell(1).setCellValue((String)map.get("product_name"));
row1.createCell(4).setCellValue((String)map.get("bom_code"));
row1.createCell(13).setCellValue("");
row1.getCell(13).setCellStyle(cs1);
//
row1 = sheet1.createRow(2);
cell1 = row1.createCell((short)0);
cell1.setCellStyle(cs);
cell1.setCellValue("产品编号");
sheet1.autoSizeColumn(0);//自动设宽
cell1 = row1.createCell((short)3);
cell1.setCellStyle(cs);
cell1.setCellValue("颜色");
sheet1.autoSizeColumn(3);//自动设宽
cell1 = row1.createCell((short)5);
cell1.setCellStyle(cs);
cell1.setCellValue("配色");
sheet1.autoSizeColumn(5);//自动设宽
cell1 = row1.createCell((short)8);
cell1.setCellStyle(cs);
cell1.setCellValue("日期");
sheet1.autoSizeColumn(8);//自动设宽
cell1 = row1.createCell((short)10);
cell1.setCellStyle(cs);
cell1.setCellValue("日期");
sheet1.autoSizeColumn(10);//自动设宽
cell1 = row1.createCell((short)12);
cell1.setCellStyle(cs);
cell1.setCellValue("日期");
sheet1.autoSizeColumn(12);//自动设宽
cell1 = row1.createCell((short)3);
cell1.setCellStyle(cs);
cell1.setCellValue("配色");
sheet1.autoSizeColumn(0);//自动设宽
row1.createCell(1).setCellValue((String)map.get("product_code"));
row1.getCell(1).setCellStyle(cs1);
row1.createCell(2).setCellValue("");
row1.getCell(2).setCellStyle(cs1);
row1.createCell(4).setCellValue((String)map.get("color"));
row1.getCell(4).setCellStyle(cs1);
row1.createCell(6).setCellValue("");
row1.getCell(6).setCellStyle(cs1);
row1.createCell(7).setCellValue("");
row1.getCell(7).setCellStyle(cs1);
row1.createCell(9).setCellValue((String)map.get("bom_update_time"));
row1.getCell(9).setCellStyle(cs1);
row1.createCell(11).setCellValue("");
row1.getCell(11).setCellStyle(cs1);
row1.createCell(13).setCellValue("");
row1.getCell(13).setCellStyle(cs1);
row1 = sheet1.createRow(i + 4);
row1.createCell(0).setCellValue((String)map.get("hierarchy"));
row1.getCell(0).setCellStyle(cs1);
row1.createCell(1).setCellValue((String)map.get("material_code"));
row1.getCell(1).setCellStyle(cs1);
row1.createCell(2).setCellValue((String)map.get("material_name"));
row1.getCell(2).setCellStyle(cs1);
row1.createCell(3).setCellValue((String)map.get("spec"));
row1.getCell(3).setCellStyle(cs1);
row1.createCell(4).setCellValue((String)map.get("colour"));
row1.getCell(4).setCellStyle(cs1);
row1.createCell(5).setCellValue((String)map.get("raw_material"));
row1.getCell(5).setCellStyle(cs1);
row1.createCell(6).setCellValue((double)map.get("unitprice"));
row1.getCell(6).setCellStyle(cs1);
row1.createCell(7).setCellValue((int)map.get("quantity"));
row1.getCell(7).setCellStyle(cs1);
row1.createCell(8).setCellValue((String)map.get("unit"));
row1.getCell(8).setCellStyle(cs1);
row1.createCell(9).setCellValue((String)map.get("type"));
row1.getCell(9).setCellStyle(cs1);
row1.createCell(10).setCellValue((String)map.get("MOQ"));
row1.getCell(10).setCellStyle(cs1);
row1.createCell(11).setCellValue((String)map.get("delivery_time"));
row1.getCell(11).setCellStyle(cs1);
row1.createCell(12).setCellValue((String)map.get("station_code"));
row1.getCell(12).setCellStyle(cs1);
row1.createCell(13).setCellValue((double)map.get("working_hours"));
row1.getCell(13).setCellStyle(cs1);
}
try {
//调用浏览器的下载器
OutputStream out = resp.getOutputStream();
wb.write(out);
out.close();
System.out.println("输出完成");
return "输出完成";
} catch (Exception e) {
System.out.println("文件输出失败!");
e.printStackTrace();
return "文件输出失败!";
}
}