1. 程式人生 > >java使用poi自定義excel標題頭並導出(springmvc+poi)

java使用poi自定義excel標題頭並導出(springmvc+poi)

image borde osi 刪除線 日期格 idt lin top 使用

 項目使用的是jeecg開源框架(springmvc+spring+hibernate+。。。。。。等)此代碼僅供參考!如有更好的意見或建議可留言。
1
controller 層 2 3 /** 4 * excel自定義導出 5 * @param hAqscTieupsummary 6 * @param request 7 * @param response 8 * @param dataGrid 9 * @param modelMap 10 * @return 11 */
12 @SuppressWarnings("deprecation") 13 @RequestMapping(params = "exportEXL") 14 public String exportEXL(HAqscTieupsummaryEntity hAqscTieupsummary, 15 HttpServletRequest request, HttpServletResponse response, 16 DataGrid dataGrid, ModelMap modelMap) { 17 try
{ 18 String dateType = "yyyy"; 19 SimpleDateFormat df = new SimpleDateFormat(dateType);// 設置日期格式 20 SimpleDateFormat df1 = new SimpleDateFormat("yyyy.MM.dd");// 設置日期格式 21 // 創建HSSFWorkbook對象(excel的文檔對象) 22 HSSFWorkbook wb = new HSSFWorkbook();
23 HSSFRow row = null; 24 HSSFCell cell = null; 25 // 建立新的sheet對象(excel的表單) 並設置sheet名字 26 HSSFSheet sheet = wb.createSheet("占壓管線臺賬信息"); 27 sheet.setDefaultRowHeightInPoints(30);// 設置缺省列高sheet.setDefaultColumnWidth(20);//設置缺省列寬 28 //----------------標題樣式--------------------- 29 HSSFCellStyle titleStyle = wb.createCellStyle(); //標題樣式 30 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 31 titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 32 Font ztFont = wb.createFont(); 33 ztFont.setItalic(false); // 設置字體為斜體字 34 ztFont.setColor(Font.COLOR_NORMAL); // 將字體設置為“紅色” 35 ztFont.setFontHeightInPoints((short)16); // 將字體大小設置為18px 36 ztFont.setFontName("宋體"); // 將“宋體”字體應用到當前單元格上 37 ztFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗 38 // ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE單條下劃線/Font.U_DOUBLE雙條下劃線) 39 // ztFont.setStrikeout(true); // 是否添加刪除線 40 titleStyle.setFont(ztFont); 41 //------------------------------------------- 42 //----------------二級標題格樣式---------------------------------- 43 HSSFCellStyle titleStyle2 = wb.createCellStyle(); //表格樣式 44 titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); 45 titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 46 Font ztFont2 = wb.createFont(); 47 ztFont2.setItalic(false); // 設置字體為斜體字 48 ztFont2.setColor(Font.COLOR_NORMAL); // 將字體設置為“紅色” 49 ztFont2.setFontHeightInPoints((short)11); // 將字體大小設置為18px 50 ztFont2.setFontName("宋體"); // 字體應用到當前單元格上 51 ztFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗 52 // ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE單條下劃線/Font.U_DOUBLE雙條下劃線) 53 // ztFont.setStrikeout(true); // 是否添加刪除線 54 titleStyle2.setFont(ztFont2); 55 //---------------------------------------------------------- 56 //----------------單元格樣式---------------------------------- 57 HSSFCellStyle cellStyle = wb.createCellStyle(); //表格樣式 58 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 59 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 60 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框 61 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框 62 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框 63 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框 64 Font cellFont = wb.createFont(); 65 cellFont.setItalic(false); // 設置字體為斜體字 66 cellFont.setColor(Font.COLOR_NORMAL); // 將字體設置為“紅色” 67 cellFont.setFontHeightInPoints((short)10); // 將字體大小設置為18px 68 cellFont.setFontName("宋體"); // 字體應用到當前單元格上 69 // cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 70 cellStyle.setFont(cellFont); 71 cellStyle.setWrapText(true);//設置自動換行 72 //---------------------------------------------------------- 73 // ----------------------創建第一行--------------- 74 // 在sheet裏創建第一行,參數為行索引(excel的行),可以是0~65535之間的任何一個 75 row = sheet.createRow(0); 76 // 創建單元格(excel的單元格,參數為列索引,可以是0~255之間的任何一個 77 cell = row.createCell(0); 78 // 合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列 79 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 19)); 80 // 設置單元格內容 81 cell.setCellValue("占壓城市地下管線、輸油氣管道、化工產品管道違法違規建設匯總表"); 82 cell.setCellStyle(titleStyle); 83 // ---------------------------------------------- 84 85 // ------------------創建第二行(單位、填表日期)--------------------- 86 row = sheet.createRow(1); // 創建第二行 87 cell = row.createCell(0); 88 cell.setCellValue("填報單位名稱(蓋章): "); 89 cell.setCellStyle(titleStyle2); 90 sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 3)); 91 cell = row.createCell(4); 92 sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5)); 93 TSBaseUser tb = ResourceUtil.getSessionUserName(); //獲取當前登錄用戶信息 94 String uid = tb.getId(); 95 String deptId = userDao.getDeptId(uid); 96 String deptName = userDao.getDeptName(deptId); 97 cell.setCellValue(deptName); 98 // cell.setCellValue("*****"); 99 cell.setCellStyle(titleStyle2); 100 cell = row.createCell(13); // 填表時間 101 sheet.addMergedRegion(new CellRangeAddress(1, 1, 13, 16)); 102 cell.setCellValue("填表時間:"+df1.format(new Date())); 103 cell.setCellStyle(titleStyle2); 104 // HSSFCell cell14 = row.createCell(15); // 填表時間 105 // cell14.setCellValue(); 106 // cell14.setCellValue("2017.11.30"); 107 // cell14.setCellStyle(titleStyle2); 108 // ---------------------------------------------- 109 110 // ------------------創建表頭start--------------------- 111 row = sheet.createRow(2); // 創建第三行 112 sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0)); 113 cell = row.createCell(0); 114 cell.setCellValue("序號"); 115 cell.setCellStyle(cellStyle); 116 117 sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1)); 118 cell = row.createCell(1); 119 cell.setCellValue("隱患等級評定"); 120 cell.setCellStyle(cellStyle); 121 122 sheet.addMergedRegion(new CellRangeAddress(2, 3, 2, 2)); 123 cell = row.createCell(2); 124 cell.setCellValue("隱患名稱"); 125 cell.setCellStyle(cellStyle); 126 127 sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 5)); 128 cell = row.createCell(3); 129 cell.setCellValue("位置描述"); 130 cell.setCellStyle(cellStyle); 131 132 cell = row.createCell(4); 133 cell.setCellStyle(cellStyle); 134 cell = row.createCell(5); 135 cell.setCellStyle(cellStyle); 136 137 sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 10)); 138 cell = row.createCell(6); 139 cell.setCellValue("管線情況"); 140 cell.setCellStyle(cellStyle); 141 142 cell = row.createCell(7); 143 cell.setCellStyle(cellStyle); 144 cell = row.createCell(8); 145 cell.setCellStyle(cellStyle); 146 cell = row.createCell(9); 147 cell.setCellStyle(cellStyle); 148 cell = row.createCell(10); 149 cell.setCellStyle(cellStyle); 150 151 sheet.addMergedRegion(new CellRangeAddress(2, 2, 11, 13)); 152 cell = row.createCell(11); 153 cell.setCellValue("占壓物情況"); 154 cell.setCellStyle(cellStyle); 155 156 cell = row.createCell(12); 157 cell.setCellStyle(cellStyle); 158 cell = row.createCell(13); 159 cell.setCellStyle(cellStyle); 160 161 sheet.addMergedRegion(new CellRangeAddress(2, 2, 14, 14)); 162 cell = row.createCell(14); 163 cell.setCellValue("占壓物用途"); 164 cell.setCellStyle(cellStyle); 165 166 sheet.addMergedRegion(new CellRangeAddress(2, 3, 15, 15)); 167 cell = row.createCell(15); 168 cell.setCellValue("已采用的安全防護措施"); 169 cell.setCellStyle(cellStyle); 170 171 sheet.addMergedRegion(new CellRangeAddress(2, 3, 16, 16)); 172 cell = row.createCell(16); 173 cell.setCellValue("備註"); 174 cell.setCellStyle(cellStyle); 175 176 sheet.addMergedRegion(new CellRangeAddress(2, 3, 17, 17)); 177 cell = row.createCell(17); 178 cell.setCellValue("聯系人電話"); 179 cell.setCellStyle(cellStyle); 180 181 sheet.addMergedRegion(new CellRangeAddress(2, 3, 18, 18)); 182 cell = row.createCell(18); 183 cell.setCellValue("是否已和區管委和供熱辦聯系"); 184 cell.setCellStyle(cellStyle); 185 186 sheet.addMergedRegion(new CellRangeAddress(2, 3, 19, 19)); 187 cell = row.createCell(19); 188 cell.setCellValue("是否采取防範措施"); 189 cell.setCellStyle(cellStyle); 190 191 //--------------------------- 創建第四行-------------------- 192 row = sheet.createRow(3); 193 sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 3)); 194 cell = row.createCell(3); 195 cell.setCellValue("所在區縣"); 196 cell.setCellStyle(cellStyle); 197 198 cell = row.createCell(0); 199 cell.setCellStyle(cellStyle); 200 201 sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 4)); 202 cell = row.createCell(4); 203 cell.setCellValue("所在街道"); 204 cell.setCellStyle(cellStyle); 205 206 cell = row.createCell(1); 207 cell.setCellStyle(cellStyle); 208 209 sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 5)); 210 cell = row.createCell(5); 211 cell.setCellValue("詳細地址"); 212 cell.setCellStyle(cellStyle); 213 214 sheet.addMergedRegion(new CellRangeAddress(3, 3, 6, 6)); 215 cell = row.createCell(6); 216 cell.setCellValue("管線建成時間"); 217 cell.setCellStyle(cellStyle); 218 219 sheet.addMergedRegion(new CellRangeAddress(3, 3, 7, 7)); 220 cell = row.createCell(7); 221 cell.setCellValue("管線埋深"); 222 cell.setCellStyle(cellStyle); 223 224 sheet.addMergedRegion(new CellRangeAddress(3, 3, 8, 8)); 225 cell = row.createCell(8); 226 cell.setCellValue("管徑"); 227 cell.setCellStyle(cellStyle); 228 229 sheet.addMergedRegion(new CellRangeAddress(3, 3, 9, 9)); 230 cell = row.createCell(9); 231 cell.setCellValue("管線壓力等級"); 232 cell.setCellStyle(cellStyle); 233 234 sheet.addMergedRegion(new CellRangeAddress(3, 3, 10, 10)); 235 cell = row.createCell(10); 236 cell.setCellValue("占壓管線長度"); 237 cell.setCellStyle(cellStyle); 238 239 sheet.addMergedRegion(new CellRangeAddress(3, 3, 11, 11)); 240 cell = row.createCell(11); 241 cell.setCellValue("占壓單位(個人)名稱"); 242 cell.setCellStyle(cellStyle); 243 244 sheet.addMergedRegion(new CellRangeAddress(3, 3, 12, 12)); 245 cell = row.createCell(12); 246 cell.setCellValue("占壓物建成時間"); 247 cell.setCellStyle(cellStyle); 248 249 sheet.addMergedRegion(new CellRangeAddress(3, 3, 13, 13)); 250 cell = row.createCell(13); 251 cell.setCellValue("占壓物面積(平方米)"); 252 cell.setCellStyle(cellStyle); 253 254 sheet.addMergedRegion(new CellRangeAddress(3, 3, 14, 14)); 255 cell = row.createCell(14); 256 cell.setCellValue("經營、出租、自用、居住"); 257 cell.setCellStyle(cellStyle); 258 259 cell = row.createCell(15); 260 cell.setCellStyle(cellStyle); 261 cell = row.createCell(16); 262 cell.setCellStyle(cellStyle); 263 cell = row.createCell(17); 264 cell.setCellStyle(cellStyle); 265 cell = row.createCell(18); 266 cell.setCellStyle(cellStyle); 267 cell = row.createCell(19); 268 cell.setCellStyle(cellStyle); 269 //-------------------------表頭end--------------------- 270 CriteriaQuery cq = new CriteriaQuery(HAqscTieupsummaryEntity.class, 271 dataGrid); 272 org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, 273 hAqscTieupsummary, request.getParameterMap()); 274 List<HAqscTieupsummaryEntity> hAqscTieupsummarys = this.hAqscTieupsummaryService 275 .getListByCriteriaQuery(cq, false); 276 for (int i = 0; i < hAqscTieupsummarys.size(); i++) { //向表格插入數據 277 List<Object> data = new ArrayList<>(); //將前臺傳來的數據存入到list中 278 // System.out.println(hAqscTieupsummarys.get(i).getSeqNum()); 279 HAqscTieupsummaryEntity entity = hAqscTieupsummarys.get(i); 280 data.add(entity.getSeqNum()); 281 String yhjb = entity.getYhDjpd(); 282 String hyjb = dao.getHyjb(yhjb); 283 data.add(hyjb); //隱患級別 284 data.add(entity.getYhName()); 285 String countryName = dao.getCountryByCode(entity.getAtcounty()); //區縣 286 data.add(countryName); 287 String code = entity.getAtdistrict(); 288 String streetName = dao.getStreetByCode(code); //街道 289 data.add(streetName); 290 data.add(entity.getAddress()); 291 Date buildtime = entity.getPipelineBuildtime(); 292 if (buildtime!=null) { 293 String format = df.format(buildtime); 294 data.add(format); 295 }else{ 296 data.add(""); 297 } 298 data.add(entity.getPipelineDepth()); 299 data.add(entity.getPipeSize()); 300 data.add(entity.getPipelinePr()); 301 data.add(entity.getTppipelineLength()); 302 data.add(entity.getTieupName()); 303 Date goodsBuildtime = entity.getTieupgoodsBuildtime(); 304 if (buildtime!=null) { 305 String format = df.format(goodsBuildtime); 306 data.add(format); 307 }else{ 308 data.add(""); 309 } 310 data.add(entity.getTieupgoodsArea()); 311 String useType = entity.getTieupgoodsUse(); 312 data.add(dao.getUseType(useType)); 313 data.add(entity.getUseSecuritymeasures()); 314 data.add(entity.getRemark()); 315 data.add(entity.getTelephone()); 316 data.add(dao.getIsContact(entity.getIsContact())); 317 data.add(entity.getIsUsesecuritymeasures()); 318 int rowNum = 4+i; //從第四行開始 319 row = sheet.createRow(rowNum); 320 for (int j = 0; j < data.size(); j++) { //將數據添加到單元格中 321 // System.out.println(data.get(j)); 322 sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, j, j)); 323 cell = row.createCell(j); 324 cell.setCellValue(""+data.get(j)+""); 325 cell.setCellStyle(cellStyle); 326 } 327 } 328 329 // 輸出Excel文件 330 OutputStream output = response.getOutputStream(); 331 response.reset(); 332 response.setHeader("Content-disposition", 333 "attachment; filename=details.xls"); //filename = 文件名 334 response.setContentType("application/msexcel"); 335 wb.write(output); 336 output.close(); 337 } catch (IOException e) { 338 // TODO Auto-generated catch block 339 e.printStackTrace(); 340 } 341 return null; 342 }
技術分享圖片
1 前臺請求地址:
2 <t:dgToolBar title="導出" icon="icon-putout" funname="ExportXls"></t:dgToolBar>
3 //導出
4 function ExportXls() {
5     JeecgExcelExport("hAqscTieupsummaryController.do?exportEXL","hAqscTieupsummaryList");
6 }
View Code

excel導出模版如下:

技術分享圖片

java使用poi自定義excel標題頭並導出(springmvc+poi)