您的位置:首页 > 编程语言 > Java开发

Java后台list导出excel

2017-11-16 16:04 435 查看
1.

List<SalesOutstoreAll> list =service.export(salesOutstoreAll); //得到list数据

 

try {

String fileName= new String(("销售出库单列表").getBytes(), "ISO8859-1");

         response.setContentType("application/binary;charset=ISO8859-1");//配置响应头

         response.setHeader("Content-disposition","attachment; filename="+ fileName + ".xls");// 组装附件名称和格式

         ServletOutputStreamoutputStream = response.getOutputStream();//获取字节流

         ExcelProcessUtil.exportXSCKD(outputStream,list);//调用方法解析list

} catch (IOException e) {

         e.printStackTrace();

}

//方法

public static void exportPurchasePlan(ServletOutputStreamoutputStream,List<PurchasePlan> list) {

HSSFWorkbook wb = new HSSFWorkbook();// 创建一个Excel文件 

HSSFSheet sheet_1 = wb.createSheet("销售出库单列表");// 创建一个Sheet

HSSFRow sheet_1_row_0 =sheet_1.createRow(0); // 创建第一行

sheet_1.setColumnWidth((short)0,(short)4000); // 设置列宽

sheet_1.setColumnWidth((short)1,(short)4000);

sheet_1.setColumnWidth((short)2,(short)4000);

sheet_1.setColumnWidth((short)3,(short)4000);

sheet_1.setColumnWidth((short)4,(short)4000);

sheet_1.setColumnWidth((short)5,(short)6000);

sheet_1.setColumnWidth((short)6,(short)4000);

sheet_1.setColumnWidth((short)7,(short)4000);

sheet_1.setColumnWidth((short)8,(short)4000);

HSSFSheet sheet_2 = wb.createSheet("销售出库商品列表");//第二个sheet

HSSFRow sheet_2_row_0 =sheet_2.createRow(0); // 创建第一行

sheet_2.setColumnWidth((short)0,(short)4000); // 设置列宽

sheet_2.setColumnWidth((short)1,(short)4000);

sheet_2.setColumnWidth((short)2,(short)4000);

sheet_2.setColumnWidth((short)3,(short)4000);

sheet_2.setColumnWidth((short)4,(short)4000);

sheet_2.setColumnWidth((short)5,(short)4000);

sheet_2.setColumnWidth((short)6,(short)4000);

//设置标题样式

HSSFCellStyle headerStyle =setTitleStyle(wb);

//sheet1

HSSFCell cell0 =sheet_1_row_0.createCell((short)0);

cell0.setCellValue(newHSSFRichTextString("单据类型"));cell0.setCellStyle(headerStyle);

HSSFCell cell1 =sheet_1_row_0.createCell((short)1);

cell1.setCellValue(newHSSFRichTextString("源单单号"));cell1.setCellStyle(headerStyle);

HSSFCell cell2 = sheet_1_row_0.createCell((short)2);

cell2.setCellValue(newHSSFRichTextString("单据日期"));cell2.setCellStyle(headerStyle);

HSSFCell cell3 =sheet_1_row_0.createCell((short)3);

cell3.setCellValue(newHSSFRichTextString("所属餐服长"));cell3.setCellStyle(headerStyle);

HSSFCell cell4 =sheet_1_row_0.createCell((short)4);

cell4.setCellValue(newHSSFRichTextString("销售出库单单号"));cell4.setCellStyle(headerStyle);

HSSFCell cell5 =sheet_1_row_0.createCell((short)5);

cell5.setCellValue(newHSSFRichTextString("发货仓库"));cell5.setCellStyle(headerStyle);

HSSFCell cell6 =sheet_1_row_0.createCell((short)6);

cell6.setCellValue(newHSSFRichTextString("客户名称"));cell6.setCellStyle(headerStyle);

HSSFCell cell7 =sheet_1_row_0.createCell((short)7);

cell7.setCellValue(newHSSFRichTextString("审核状态"));cell7.setCellStyle(headerStyle);

HSSFCell cell8 =sheet_1_row_0.createCell((short)8);

cell8.setCellValue(newHSSFRichTextString("单据状态"));cell8.setCellStyle(headerStyle);

//sheet2

HSSFCell cell12 =sheet_2_row_0.createCell((short)0);

cell12.setCellValue(new HSSFRichTextString("商品编码"));cell12.setCellStyle(headerStyle);

HSSFCell cell13 =sheet_2_row_0.createCell((short)1);

cell13.setCellValue(newHSSFRichTextString("商品名称"));cell13.setCellStyle(headerStyle);

HSSFCell cell14 =sheet_2_row_0.createCell((short)2);

cell14.setCellValue(newHSSFRichTextString("规格型号"));cell14.setCellStyle(headerStyle);

HSSFCell cell15 =sheet_2_row_0.createCell((short)3);

cell15.setCellValue(newHSSFRichTextString("商品品牌"));cell15.setCellStyle(headerStyle);

HSSFCell cell16 = sheet_2_row_0.createCell((short)4);

cell16.setCellValue(newHSSFRichTextString("计量单位"));cell16.setCellStyle(headerStyle);

HSSFCell cell17 =sheet_2_row_0.createCell((short)5);

cell17.setCellValue(newHSSFRichTextString("实际出库数量"));cell17.setCellStyle(headerStyle);

HSSFCell cell18 =sheet_2_row_0.createCell((short)6);

cell18.setCellValue(newHSSFRichTextString("实际出库成本价"));cell18.setCellStyle(headerStyle);

//开始配置list内容

int index = 1;

         for(int i = 1; i <= list.size(); i++) {

         SalesOutstoreAll  sAll= list.get(i-1);

         //重构第一行对象,循环赋值

         sheet_1_row_0= sheet_1.createRow(i);

         //设置样式

         HSSFCellStylecellStyle = setDataStyle(wb);

         cell0= sheet_1_row_0.createCell((short)0);

         cell0.setCellValue(newHSSFRichTextString("销售出库单"));cell0.setCellStyle(cellStyle);

         cell1= sheet_1_row_0.createCell((short)1);

         cell1.setCellValue(newHSSFRichTextString(sAll.getOriginOrderNo()));cell1.setCellStyle(cellStyle);

         cell2= sheet_1_row_0.createCell((short)2);

         cell2.setCellValue(newHSSFRichTextString(DateUtils.formatTimestampDate(sAll.getDocumentDate(),"yyyy-MM-dd")));cell2.setCellStyle(cellStyle);

         cell3= sheet_1_row_0.createCell((short)3);

         cell3.setCellValue(newHSSFRichTextString(mealsPlanner(sAll.getTeamManagerName(),sAll.getTeamManagerPhone())));cell3.setCellStyle(cellStyle);

                                     cell4= sheet_1_row_0.createCell((short)4);

                                     cell4.setCellValue(newHSSFRichTextString(sAll.getPurchaseOutstoreNo()));cell4.setCellStyle(cellStyle);

                                     cell5= sheet_1_row_0.createCell((short)5);

                                     cell5.setCellValue(newHSSFRichTextString(sAll.getOutstoreName()));cell5.setCellStyle(cellStyle);

                                     cell6= sheet_1_row_0.createCell((short)6);

                                     cell6.setCellValue(newHSSFRichTextString(sAll.getCustomerName()));cell6.setCellStyle(cellStyle);

                                     cell7= sheet_1_row_0.createCell((short)7);

                                     cell7.setCellValue(newHSSFRichTextString(applyStatus(sAll.getApplyStatus().toString())));cell7.setCellStyle(cellStyle);

                                     cell8= sheet_1_row_0.createCell((short)8);

                                     cell8.setCellValue(newHSSFRichTextString(documentStatus(sAll.getDocumentStatus().toString())));cell8.setCellStyle(cellStyle);

//第二个sheet开始

                                     List<SalesOutstoreGoodsForm>goods = sAll.getGoodsDatas();

                                     for(SalesOutstoreGoodsForm s : goods) {

                                               //重构第一行对象,循环赋值

                                               sheet_2_row_0= sheet_2.createRow(index);

                                               cell12= sheet_2_row_0.createCell((short)0);

                                               cell12.setCellValue(newHSSFRichTextString(s.getGoodsCode()));cell12.setCellStyle(cellStyle);

                                               cell13= sheet_2_row_0.createCell((short)1);

                                               cell13.setCellValue(newHSSFRichTextString(s.getGoodsName()));cell13.setCellStyle(cellStyle);

                                               cell14= sheet_2_row_0.createCell((short)2);

                                               cell14.setCellValue(newHSSFRichTextString(s.getGoodsSpecification()));cell14.setCellStyle(cellStyle);

                                               cell15= sheet_2_row_0.createCell((short)3);

                                               cell15.setCellValue(newHSSFRichTextString(s.getGoodsBrand()));cell15.setCellStyle(cellStyle);

                                               cell16= sheet_2_row_0.createCell((short)4);

                                               cell16.setCellValue(newHSSFRichTextString(s.getGoodsUnits()));cell16.setCellStyle(cellStyle);

                                               cell17= sheet_2_row_0.createCell((short)5);

                                               cell17.setCellValue(s.getOutNum());cell17.setCellStyle(cellStyle);

                                               cell18= sheet_2_row_0.createCell((short)6);

                                               cell18.setCellValue(s.getCostPrice());cell18.setCellStyle(cellStyle);

                                               index++;

                                     }

 

                            }

                            try{

                                     wb.write(outputStream);

                                     outputStream.flush();

                                     outputStream.close();

                                     //本地测试

                                     FileOutputStreamfout = new FileOutputStream("E:\\test.xls"); 

                     wb.write(fout);

                     fout.close();

                            }catch (IOException e) {

                                     e.printStackTrace();

                            }finally {

                                     try{

                                               outputStream.close();

                                     }catch (IOException e) {

                                               e.printStackTrace();

                                     }

                            }                

}

         //设置标题格式

         privatestatic HSSFCellStyle setTitleStyle(HSSFWorkbook wb) {

                   HSSFCellStyleheaderStyle  = wb.createCellStyle(); // 创建标题样式

                   headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框

                   headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框

                   headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框

                   headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

                   headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置居中

                   HSSFFontheaderFont = wb.createFont(); //创建字体样式 

                   headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 

                   headerFont.setFontName("仿宋_GB2312");

                   headerFont.setFontHeightInPoints((short)13);//字体大小

                   headerStyle.setFont(headerFont);    //为标题样式设置字体样式

                   returnheaderStyle;

         }

         //设置内容格式

         privatestatic HSSFCellStyle setDataStyle(HSSFWorkbook wb) {

                   HSSFCellStylecellStyle = wb.createCellStyle(); // 创建标题样式 

                   cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

                   cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框

                   cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框

                   cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框

                   cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

                   cellStyle.setWrapText(true);//自动换行

                   HSSFFontheaderFont = wb.createFont(); //创建字体样式 

                   headerFont.setFontName("仿宋_GB2312");

                   headerFont.setFontHeightInPoints((short)10);//字体大小

                   cellStyle.setFont(headerFont);    //为标题样式设置字体样式

                   returncellStyle;

         }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  教学 excel导出