package com.opensource.util.excel; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.util.Region; import org.apache.struts.action.Action; public class CreateSimpleExcelToDisk extends Action { public static void ExportExcel(List<List> allList,String inWeek, HttpServletResponse response) throws Exception { // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow rowT = sheet.createRow(0); rowT.setHeight((short)450); // 第四步,创建单元格,并设置值表头 设置表头居中 //style表格主体使用 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //style1日期星期列使用中中对其 HSSFCellStyle style1 = wb.createCellStyle(); style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //标题专用style2 HSSFCellStyle style2 = wb.createCellStyle(); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); //列标题用style3 HSSFCellStyle style3 = wb.createCellStyle(); style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //第几周用style HSSFCellStyle style4 = wb.createCellStyle(); style4.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //添加边框 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 // style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style3.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style3.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 /**字体start*/ //生成一个字体 HSSFFont font=wb.createFont(); font.setFontName("仿宋_GB2312"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色 font.setFontHeightInPoints((short)12); //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗 //把字体应用到当前的样式 style.setFont(font); /**字体end*/ /**字体start*/ //生成一个字体日期星期用 HSSFFont font1=wb.createFont(); font1.setFontName("仿宋"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色 font1.setFontHeightInPoints((short)12); font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗 //把字体应用到当前的样式 style1.setFont(font1); /**字体end*/ /**字体start*/ //生成一个字体标题用 HSSFFont font2=wb.createFont(); font2.setFontName("仿宋"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色 font2.setFontHeightInPoints((short)16); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗 //把字体应用到当前的样式 style2.setFont(font2); /**字体end*/ /**字体start*/ //生成一个字体列标题 HSSFFont font3=wb.createFont(); font3.setFontName("仿宋"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色 font3.setFontHeightInPoints((short)14); font3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗 //把字体应用到当前的样式 style3.setFont(font3); /**字体end*/ /**字体start*/ //生成一个字体列标题 HSSFFont font4=wb.createFont(); font4.setFontName("仿宋"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色 font4.setFontHeightInPoints((short)11); // font4.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗 //把字体应用到当前的样式 style4.setFont(font4); /**字体end*/ //合并单元格 ,四个参数分别是:起始行,起始列,结束行,结束列 sheet.addMergedRegion(new Region(0,(short)0,0,(short)5)); HSSFCell cellT = rowT.createCell((short)0); cellT.setCellValue(" 公 司 领 导 周 安 排"); cellT.setCellStyle(style2); HSSFRow row2 = sheet.createRow((int) 1); row2.setHeight((short)435); sheet.addMergedRegion(new Region(1,(short)0,1,(short)5)); HSSFCell cell2 = row2.createCell((short)0);//参数是此行的第一列 cell2.setCellValue(inWeek); cell2.setCellStyle(style4); HSSFRow row3 = sheet.createRow((int) 2); //自动调整列宽 // sheet.autoSizeColumn((short)0); //调整第一列宽度 // sheet.autoSizeColumn((short)1); //调整第二列宽度 // sheet.autoSizeColumn((short)2); //调整第三列宽度 // sheet.autoSizeColumn((short)3); //调整第四列宽度 // sheet.autoSizeColumn((short)4); //调整第五列宽度 // sheet.autoSizeColumn((short)5); //调整第六列宽度 HSSFCell cell = row3.createCell((short) 0); row3.setHeight((short)500); sheet.setColumnWidth((short) 0, (short) 3500);//设置宽度 cell.setCellValue("日期"); cell.setCellStyle(style3); cell = row3.createCell((short) 1); cell.setCellValue("星期"); cell.setCellStyle(style3); cell = row3.createCell((short) 2); sheet.setColumnWidth((short) 2, (short) 3500);//设置宽度 cell.setCellValue("时间"); cell.setCellStyle(style3); cell = row3.createCell((short) 3); cell.setCellValue("地点"); cell.setCellStyle(style3); cell = row3.createCell((short) 4); sheet.setColumnWidth((short) 4, (short) 23000);//设置宽度 cell.setCellValue("参加领导"); cell.setCellStyle(style3); cell = row3.createCell((short) 5); sheet.setColumnWidth((short) 5, (short) 5000);//设置宽度 cell.setCellValue("工作事项"); cell.setCellStyle(style3); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, int lineNum = 3; for(List list : allList) { for (int j = 0; j < list.size(); j++) { sheet.addMergedRegion(new Region(lineNum,(short)0, lineNum+(list.size()-1),(short)0)); sheet.addMergedRegion(new Region(lineNum,(short)1, lineNum+(list.size()-1),(short)1)); row3 = sheet.createRow(lineNum+j); //从list中获取对象 row3.setHeight((short) 420); Map map = (Map)list.get(j); // 第四步,创建单元格,并设置值 HSSFCell cell4 =row3.createCell((short) 0); cell4.setCellValue(map.get("riqi").toString()); cell4.setCellStyle(style1); cell4=row3.createCell((short) 1); cell4.setCellValue(dayForWeek(map.get("riqi").toString())); cell4.setCellStyle(style1); cell4=row3.createCell((short) 2); cell4.setCellValue( map.get("shijian").toString()); cell4.setCellStyle(style); cell4= row3.createCell((short) 3); cell4.setCellValue( map.get("didian").toString()); cell4.setCellStyle(style); cell4=row3.createCell((short) 4); cell4.setCellValue( map.get("renyuan").toString()); cell4.setCellStyle(style); cell4=row3.createCell((short) 5); cell4.setCellValue(map.get("neirong").toString()); cell4.setCellStyle(style); } lineNum = lineNum + list.size(); } // 第六步,将文件存到指定位置 try { // FileOutputStream fout = new FileOutputStream("E:/students.xls"); // wb.write(fout); // fout.close(); ByteArrayOutputStream os = new ByteArrayOutputStream(); wb.write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(("京投公司领导周工作安排"+inWeek+".xls") .getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } } catch (Exception e) { e.printStackTrace(); } } //根据日期判断星期几 public static String dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if (c.get(Calendar.DAY_OF_WEEK) == 1) { dayForWeek = 7; } else { dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } String week = "星期一"; switch(dayForWeek) { case 1: week = "星期一"; break; case 2: week = "星期二"; break; case 3: week = "星期三"; break; case 4: week = "星期四"; break; case 5: week = "星期五"; break; case 6: week = "星期六"; break; default: week = "星期日"; break; } return week; } }
以excel形式导出,java实现代码
猜你喜欢
转载自blog.csdn.net/java001122/article/details/80229089
今日推荐
周排行