/**建立HSSFWorkbook對象(excel的文檔對象)*/ HSSFWorkbook wkb = new HSSFWorkbook(); //用於設置單元格的顯示樣式 HSSFCellStyle cellstyle = (HSSFCellStyle) wkb.createCellStyle(); cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設置居中 HSSFFont bigTitleHeaderFont = (HSSFFont) wkb.createFont(); bigTitleHeaderFont.setFontHeightInPoints((short) 16); //設置字體大小 bigTitleHeaderFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字體加粗 cellstyle.setFont(bigTitleHeaderFont); HSSFCellStyle styleOne = wkb.createCellStyle(); HSSFFont fontOne = wkb.createFont(); fontOne.setColor(HSSFColor.GREEN.index);//綠色 styleOne.setFont(fontOne); 比較全面的POI Excel導出樣式設置 //創建新的sheet對象(excel的表單) 建立底部菜單 HSSFSheet sheet = wkb.createSheet(); sheet.setColumnWidth(1, 30 * 256); //設置第二列的寬度 wkb.setSheetName(0, "XXXXX "); /** new CellRangeAddress(a,b,c,d) 參數1:起始行 參數2:終止行 參數3:起始列 參數4:終止列 */ sheet.addMergedRegion(new CellRangeAddress(0,1,0,18));//從第1行到第二行結束,從第1列開始到19列結束(合併單元格,下標從0開始) HSSFRow row0 = sheet.createRow(0); //在sheet裏建立第一行(下標從0開始) HSSFCell cell2_0 = row0.createCell(0); cell2_0.setCellValue("XXXX"); //設置單元格內容 cell2_0.setCellStyle(cellstyle); //賦予樣式 /* row0.createCell(0).setCellStyle(cellstyle); 連着寫的話設置的樣式無效 **/ ........ /**a,b,c,d泛指數字*/ sheet1.addMergedRegion(new CellRangeAddress(a,b,c,d)); HSSFRow row10 = sheet1.createRow(a); //在sheet裏建立第(a+1)行 HSSFCell cell00_01 = row10.createCell(c); cell00_01.setCellValue("姓名: "); cell00_01.setCellStyle(oneCellstyle); sheet1.addMergedRegion(new CellRangeAddress(2,4,2,5)); HSSFCell cell01_02 = row10.createCell(2); cell01_02.setCellValue(""); cell01_02.setCellStyle(cellstyle); ...... //輸出Excel文件 try { OutputStream output = response.getOutputStream(); response.reset(); String filename = "XXXX"; response.setContentType("application/x-download");//處理亂碼問題 response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8") , "iso8859-1") + ".xls"); wkb.write(output); output.close(); } catch (Exception e) { }