1 public ActionResult excelPrint() { 2 HSSFWorkbook workbook = new HSSFWorkbook();// 創建一個Excel文件 3 HSSFSheet sheet = workbook.createSheet();// 創建一個Excel的Sheet 4 sheet.createFreezePane(1, 3);// 凍結 5 // 設置列寬 6 sheet.setColumnWidth(0, 1000); 7 sheet.setColumnWidth(1, 3500); 8 sheet.setColumnWidth(2, 3500); 9 sheet.setColumnWidth(3, 6500); 10 sheet.setColumnWidth(4, 6500); 11 sheet.setColumnWidth(5, 6500); 12 sheet.setColumnWidth(6, 6500); 13 sheet.setColumnWidth(7, 2500); 14 // Sheet樣式 15 HSSFCellStyle sheetStyle = workbook.createCellStyle(); 16 // 背景色的設定 17 sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); 18 // 前景色的設定 19 sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); 20 // 填充模式 21 sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS); 22 // 設置列的樣式 23 for (int i = 0; i <= 14; i++) { 24 sheet.setDefaultColumnStyle((short) i, sheetStyle); 25 } 26 // 設置字體 27 HSSFFont headfont = workbook.createFont(); 28 headfont.setFontName("黑體"); 29 headfont.setFontHeightInPoints((short) 22);// 字體大小 30 headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 31 // 另一個樣式 32 HSSFCellStyle headstyle = workbook.createCellStyle(); 33 headstyle.setFont(headfont); 34 headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 35 headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 36 headstyle.setLocked(true); 37 headstyle.setWrapText(true);// 自動換行 38 // 另一個字體樣式 39 HSSFFont columnHeadFont = workbook.createFont(); 40 columnHeadFont.setFontName("宋體"); 41 columnHeadFont.setFontHeightInPoints((short) 10); 42 columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 43 // 列頭的樣式 44 HSSFCellStyle columnHeadStyle = workbook.createCellStyle(); 45 columnHeadStyle.setFont(columnHeadFont); 46 columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 47 columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 48 columnHeadStyle.setLocked(true); 49 columnHeadStyle.setWrapText(true); 50 columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左邊框的顏色 51 columnHeadStyle.setBorderLeft((short) 1);// 邊框的大小 52 columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右邊框的顏色 53 columnHeadStyle.setBorderRight((short) 1);// 邊框的大小 54 columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框為粗體 55 columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色 56 // 設置單元格的背景顏色(單元格的樣式會覆蓋列或行的樣式) 57 columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index); 58 59 HSSFFont font = workbook.createFont(); 60 font.setFontName("宋體"); 61 font.setFontHeightInPoints((short) 10); 62 // 普通單元格樣式 63 HSSFCellStyle style = workbook.createCellStyle(); 64 style.setFont(font); 65 style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中 66 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中 67 style.setWrapText(true); 68 style.setLeftBorderColor(HSSFColor.BLACK.index); 69 style.setBorderLeft((short) 1); 70 style.setRightBorderColor(HSSFColor.BLACK.index); 71 style.setBorderRight((short) 1); 72 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框為粗體 73 style.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色. 74 style.setFillForegroundColor(HSSFColor.WHITE.index);// 設置單元格的背景顏色. 75 // 另一個樣式 76 HSSFCellStyle centerstyle = workbook.createCellStyle(); 77 centerstyle.setFont(font); 78 centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 79 centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 80 centerstyle.setWrapText(true); 81 centerstyle.setLeftBorderColor(HSSFColor.BLACK.index); 82 centerstyle.setBorderLeft((short) 1); 83 centerstyle.setRightBorderColor(HSSFColor.BLACK.index); 84 centerstyle.setBorderRight((short) 1); 85 centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框為粗體 86 centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色. 87 centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 設置單元格的背景顏色. 88 89 try { 90 // 創建第一行 91 HSSFRow row0 = sheet.createRow(0); 92 // 設置行高 93 row0.setHeight((short) 900); 94 // 創建第一列 95 HSSFCell cell0 = row0.createCell(0); 96 cell0.setCellValue(new HSSFRichTextString("中非發展基金投資項目調度會工作落實情況對照表")); 97 cell0.setCellStyle(headstyle); 98 /** 99 * 合並單元格 100 * 第一個參數:第一個單元格的行數(從0開始) 101 * 第二個參數:第二個單元格的行數(從0開始) 102 * 第三個參數:第一個單元格的列數(從0開始) 103 * 第四個參數:第二個單元格的列數(從0開始) 104 */ 105 CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7); 106 sheet.addMergedRegion(range); 107 // 創建第二行 108 HSSFRow row1 = sheet.createRow(1); 109 HSSFCell cell1 = row1.createCell(0); 110 cell1.setCellValue(new HSSFRichTextString("本次會議時間:2009年8月31日 前次會議時間:2009年8月24日")); 111 cell1.setCellStyle(centerstyle); 112 // 合並單元格 113 range = new CellRangeAddress(1, 2, 0, 7); 114 sheet.addMergedRegion(range); 115 // 第三行 116 HSSFRow row2 = sheet.createRow(3); 117 row2.setHeight((short) 750); 118 HSSFCell cell = row2.createCell(0); 119 cell.setCellValue(new HSSFRichTextString("責任者")); 120 cell.setCellStyle(columnHeadStyle); 121 cell = row2.createCell(1); 122 cell.setCellValue(new HSSFRichTextString("成熟度排序")); 123 cell.setCellStyle(columnHeadStyle); 124 cell = row2.createCell(2); 125 cell.setCellValue(new HSSFRichTextString("事項")); 126 cell.setCellStyle(columnHeadStyle); 127 cell = row2.createCell(3); 128 cell.setCellValue(new HSSFRichTextString("前次會議要求/n/新項目的項目概要")); 129 cell.setCellStyle(columnHeadStyle); 130 cell = row2.createCell(4); 131 cell.setCellValue(new HSSFRichTextString("上周工作進展")); 132 cell.setCellStyle(columnHeadStyle); 133 cell = row2.createCell(5); 134 cell.setCellValue(new HSSFRichTextString("本周工作計划")); 135 cell.setCellStyle(columnHeadStyle); 136 cell = row2.createCell(6); 137 cell.setCellValue(new HSSFRichTextString("問題和建議")); 138 cell.setCellStyle(columnHeadStyle); 139 cell = row2.createCell(7); 140 cell.setCellValue(new HSSFRichTextString("備 注")); 141 cell.setCellStyle(columnHeadStyle); 142 // 訪問數據庫,得到數據集 143 List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList(); 144 int m = 4; 145 int k = 4; 146 for (int i = 0; i < deitelVOList.size(); i++) { 147 DeitelVO vo = deitelVOList.get(i); 148 String dname = vo.getDname(); 149 List<Workinfo> workList = vo.getWorkInfoList(); 150 HSSFRow row = sheet.createRow(m); 151 cell = row.createCell(0); 152 cell.setCellValue(new HSSFRichTextString(dname)); 153 cell.setCellStyle(centerstyle); 154 // 合並單元格 155 range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0); 156 sheet.addMergedRegion(range); 157 m = m + workList.size(); 158 159 for (int j = 0; j < workList.size(); j++) { 160 Workinfo w = workList.get(j); 161 // 遍歷數據集創建Excel的行 162 row = sheet.getRow(k + j); 163 if (null == row) { 164 row = sheet.createRow(k + j); 165 } 166 cell = row.createCell(1); 167 cell.setCellValue(w.getWnumber()); 168 cell.setCellStyle(centerstyle); 169 cell = row.createCell(2); 170 cell.setCellValue(new HSSFRichTextString(w.getWitem())); 171 cell.setCellStyle(style); 172 cell = row.createCell(3); 173 cell.setCellValue(new HSSFRichTextString(w.getWmeting())); 174 cell.setCellStyle(style); 175 cell = row.createCell(4); 176 cell.setCellValue(new HSSFRichTextString(w.getWbweek())); 177 cell.setCellStyle(style); 178 cell = row.createCell(5); 179 cell.setCellValue(new HSSFRichTextString(w.getWtweek())); 180 cell.setCellStyle(style); 181 cell = row.createCell(6); 182 cell.setCellValue(new HSSFRichTextString(w.getWproblem())); 183 cell.setCellStyle(style); 184 cell = row.createCell(7); 185 cell.setCellValue(new HSSFRichTextString(w.getWremark())); 186 cell.setCellStyle(style); 187 } 188 k = k + workList.size(); 189 } 190 // 列尾 191 int footRownumber = sheet.getLastRowNum(); 192 HSSFRow footRow = sheet.createRow(footRownumber + 1); 193 HSSFCell footRowcell = footRow.createCell(0); 194 footRowcell.setCellValue(new HSSFRichTextString(" 審 定:XXX 審 核:XXX 匯 總:XX")); 195 footRowcell.setCellStyle(centerstyle); 196 range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7); 197 sheet.addMergedRegion(range); 198 199 HttpServletResponse response = getResponse(); 200 HttpServletRequest request = getRequest(); 201 String filename = "未命名.xls";//設置下載時客戶端Excel的名稱 202 // 請見:http://zmx.javaeye.com/blog/622529 203 filename = Util.encodeFilename(filename, request); 204 response.setContentType("application/vnd.ms-excel"); 205 response.setHeader("Content-disposition", "attachment;filename=" + filename); 206 OutputStream ouputStream = response.getOutputStream(); 207 workbook.write(ouputStream); 208 ouputStream.flush(); 209 ouputStream.close(); 210 211 } catch (Exception e) { 212 e.printStackTrace(); 213 } 214 return null; 215 }