引用:http://apps.hi.baidu.com/share/detail/17249059java
POI中可能會用到一些須要設置EXCEL單元格格式的操做小結:c++
先獲取工做薄對象:app
HSSFWorkbook wb = new HSSFWorkbook();字體
HSSFSheet sheet = wb.createSheet();url
HSSFCellStyle setBorder = wb.createCellStyle();spa
1、設置背景色:excel
setBorder.setFillForegroundColor((short) 13);// 設置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);code
2、設置邊框:對象
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框索引
3、設置居中:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
4、設置字體:
HSSFFont font = wb.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設置字體大小
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);
setBorder.setFont(font);//選擇須要用到的字體格式
5、設置列寬:
sheet.setColumnWidth(0, 3766); //第一個參數表明列id(從0開始),第2個參數表明寬度值 參考 :"2012-08-10"的寬度爲2500
6、設置自動換行:
setBorder.setWrapText(true);//設置自動換行
7、合併單元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//參數1:行號 參數2:起始列號 參數3:行號 參數4:終止列號
或者用
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
但應注意兩個構造方法的參數不是同樣的,具體使用哪一個取決於POI的不一樣版本。
sheet.addMergedRegion(region1);
目前用過的就這麼多,後續有新的會繼續添加。
導出例子:
List<StatMonthResp> statMonthRespList = tempResp.getResult();// 結果數據放入list if (null == statMonthRespList) { statMonthRespList = new ArrayList<StatMonthResp>(); } String url = "/file/統計.xls"; try { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle setBorder = workbook.createCellStyle(); HSSFCellStyle setTitleBorder = workbook.createCellStyle(); HSSFCellStyle setTitleBorder1 = workbook.createCellStyle(); /*****設置單元格格式*****/ setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN); setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN); setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN); setBorder.setBottomBorderColor(HSSFColor.BLACK.index); // 下邊框 setBorder.setLeftBorderColor(HSSFColor.BLACK.index);// 左邊框 setBorder.setTopBorderColor(HSSFColor.BLACK.index);// 上邊框 setBorder.setRightBorderColor(HSSFColor.BLACK.index);// 右邊框 setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 setBorder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font = workbook.createFont();// 設置字體大小 font.setFontName("黑體"); font.setFontHeightInPoints((short) 10); HSSFFont font2 = workbook.createFont(); font2.setFontName("黑體"); font2.setFontHeightInPoints((short) 20);// 設置標題字體大小 HSSFFont font3 = workbook.createFont(); font3.setFontName("黑體"); font3.setFontHeightInPoints((short) 14);// 設置小標題字體大小 setTitleBorder.setFont(font2); setTitleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); setBorder.setFont(font); setBorder.setWrapText(true); setTitleBorder1.setFont(font3); /*****設置單元格格式 end*****/ HSSFSheet sheet = workbook.createSheet(); // 設置列寬 int[] columnWidths = getColumnWidths(); for (int i = 0; i < columnWidths.length; i++) { sheet.setColumnWidth(i, columnWidths[i]); } /******合併單元格*****/ CellRangeAddress region1 = new CellRangeAddress(0, 0, (short) 0, (short) 17); CellRangeAddress region2 = new CellRangeAddress(1, 1, (short) 0, (short) 7); sheet.addMergedRegion(region1); sheet.addMergedRegion(region2); /******合併單元格 end*****/ /****** 第1行 ********/ HSSFRow titleRow = sheet.createRow(0); titleRow.setHeight((short) 800); HSSFCell fcell = titleRow.createCell(0); fcell.setCellValue(getMonth() + " 統計彙總表"); fcell.setCellStyle(setTitleBorder); /****** 第2行 ********/ HSSFRow titleRow1 = sheet.createRow(1); titleRow1.setHeight((short) 450); HSSFCell fcell1 = titleRow1.createCell(0); fcell1.setCellValue("填報單位:xxx"); fcell1.setCellStyle(setTitleBorder1); /****** 第3行 ********/ HSSFRow firstRow = sheet.createRow(2); firstRow.setHeight((short) 800); HSSFCell cell; int cellIndex = 0; String[] titles = excelTitles(); for (String field : titles) { // 在索引0的位置建立單元格(左上端) cell = firstRow.createCell(cellIndex); // 定義單元格爲字符串類型 cell.setCellStyle(setBorder); cell.setCellValue(field); cellIndex++; } /****** 第4行 ********/ int r = 3; for (StatMonthResp statMonthResp : statMonthRespList) { String[] datas = excelData(statMonthResp); HSSFRow secondRow = sheet.createRow(r); for (int i = 0; i < datas.length; i++) { cell = secondRow.createCell(i); cell.setCellStyle(setBorder); cell.setCellValue(datas[i]); } r++; } // 新建一輸出文件流 FileOutputStream fOut = new FileOutputStream(request.getRealPath("/") + url); // 把相應的Excel 工做簿存盤 workbook.write(fOut); fOut.flush(); // 操做結束,關閉文件 IOUtils.close(fOut); System.out.println("文件生成..."); } catch (Exception e) { } private int[] getColumnWidths() { int[] columnWidths = new int[4]; columnWidths[0] = 2000; columnWidths[1] = 2000; columnWidths[2] = 3000; columnWidths[3] = 2000; return columnWidths; } private String[] excelTitles() { String[] titles = new String[4]; titles[0] = "單位"; titles[1] = "車輛總數"; titles[2] = "其中已安裝GPS車輛數"; titles[3] = "GPS安裝率"; return titles; } private String[] excelData(StatMonthResp statMonthResp) { int c = 0; String[] datas = new String[4]; datas[c++] = ""; datas[c++] = String.valueOf(statMonthResp.getCars()); datas[c++] = String.valueOf(statMonthResp.getGpsCars()); datas[c++] = String.valueOf(statMonthResp.getGpsRate()); return datas; }