POI中可能會用到一些需要設置EXCEL單元格格式的操作小結:先獲取工作薄對象:HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet();HSSFCellStyle setBorder = wb.createCellStyle();一、設置背景色:setBorder.setFillForegroundColor((short) 13);// 設置背景色setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);二、設置邊框:setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框三、設置居中:setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中四、設置字體: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);//選擇需要用到的字體格式五、設置列寬:sheet.setColumnWidth(0, 3766); //第一個參數(shù)代表列id(從0開始),第2個參數(shù)代表寬度值六、設置自動換行:setBorder.setWrapText(true);//設置自動換行七、合并單元格:Region region1 = new Region(0, (short) 0, 0, (short) 6);//參數(shù)1:行號 參數(shù)2:起始列號 參數(shù)3:行號 參數(shù)4:終止列號sheet.addMergedRegion(region1);附一個完整的例子:package cn.com.util;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.util.CellRangeAddress;import org.apache.poi.hssf.util.Region;import org.apache.poi.ss.usermodel.CellStyle;import java.io.FileOutputStream;import javax.servlet.http.HttpServlet;public class CreateXL extends HttpServlet {/** Excel 文件要存放的位置,假定在D盤下 */public static String outputFile = "c:test.xls";private void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val) {HSSFCell cell = row.createCell(col);// cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue(val);HSSFCellStyle cellstyle = wb.createCellStyle();cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);cell.setCellStyle(cellstyle);}public static void main(String argv[]) {try {// 創(chuàng)建新的Excel 工作簿HSSFWorkbook workbook = new HSSFWorkbook();// 設置字體HSSFFont font = workbook.createFont();// font.setColor(HSSFFont.COLOR_RED);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);font.setFontHeightInPoints((short) 14);// HSSFFont font2 = workbook.createFont();// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// font.setFontHeightInPoints((short)14);// 設置樣式HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setFont(font);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// HSSFCellStyle cellStyle2= workbook.createCellStyle();// cellStyle.setFont(font2);// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 在Excel工作簿中建一工作表,其名為缺省值// 如要新建一名為"月報表"的工作表,其語句為:HSSFSheet sheet = workbook.createSheet("月報表");CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0,11);sheet.addMergedRegion(cellRangeAddress);//第一行// 在索引0的位置創(chuàng)建行(最頂端的行)HSSFRow row = sheet.createRow(0);// 在索引0的位置創(chuàng)建單元格(左上端)HSSFCell cell = row.createCell(0);// 定義單元格為字符串類型cell.setCellType(HSSFCell.CELL_TYPE_STRING);cell.setCellStyle(cellStyle);// 在單元格中輸入一些內容cell.setCellValue(new HSSFRichTextString("北京億卡聯(lián)科技發(fā)展有限公司小區(qū)門禁維修月報表"));//第二行cellRangeAddress = new CellRangeAddress(1, 1, 3, 6);sheet.addMergedRegion(cellRangeAddress);row = sheet.createRow(1);HSSFCell datecell = row.createCell(3);datecell.setCellType(HSSFCell.CELL_TYPE_STRING);datecell.setCellStyle(cellStyle);datecell.setCellValue("時間間隔xxxxx");cellRangeAddress = new CellRangeAddress(1, 1, 9,10);sheet.addMergedRegion(cellRangeAddress);row.createCell(9).setCellValue("單位:元");//第三行row=sheet.createRow(2);row.createCell(0).setCellValue("一、");row.createCell(1).setCellValue("基本資料");//第4行row=sheet.createRow(3);row.createCell(1).setCellValue("小區(qū)名稱:");cellRangeAddress=new CellRangeAddress(3,3,2,11);sheet.addMergedRegion(cellRangeAddress);row.createCell(2).setCellValue("xxxxx");//第5行row=sheet.createRow(4);row.createCell(1).setCellValue("座落地點:");cellRangeAddress=new CellRangeAddress(4,4,2,11);sheet.addMergedRegion(cellRangeAddress);row.createCell(2).setCellValue("xxxxx");//第6行row=sheet.createRow(5);row.createCell(1).setCellValue("建成年月:");cellRangeAddress=new CellRangeAddress(5,5,2,4);sheet.addMergedRegion(cellRangeAddress);row.createCell(2).setCellValue("年月日:xxxxx");row.createCell(5).setCellValue("聯(lián)系人");cellRangeAddress=new CellRangeAddress(5,5,6,8);sheet.addMergedRegion(cellRangeAddress);row.createCell(6).setCellValue("XXX");row.createCell(9).setCellValue("電話");cellRangeAddress=new CellRangeAddress(5,5,10,11);sheet.addMergedRegion(cellRangeAddress);row.createCell(10).setCellValue("XXX");//第7行row=sheet.createRow(6);row.createCell(1).setCellValue("住戶:");row.createCell(2).setCellValue("(XX)");row.createCell(3).setCellValue("(戶)");cellRangeAddress=new CellRangeAddress(6,6,4,5);sheet.addMergedRegion(cellRangeAddress);row.createCell(4).setCellValue("共計( )"); row.createCell(6).setCellValue("幢");cellRangeAddress=new CellRangeAddress(6,6,7,8);sheet.addMergedRegion(cellRangeAddress);row.createCell(7).setCellValue("發(fā)卡張數(shù)"); cellRangeAddress=new CellRangeAddress(6,6,9,10);sheet.addMergedRegion(cellRangeAddress);row.createCell(9).setCellValue("xxxx");//第9行row=sheet.createRow(8);row.createCell(0).setCellValue("二、");cellRangeAddress=new CellRangeAddress(8,8,1,2);sheet.addMergedRegion(cellRangeAddress);row.createCell(1).setCellValue("維修用材料臺賬");row.createCell(6).setCellValue("三、");cellRangeAddress=new CellRangeAddress(8,8,7,9);sheet.addMergedRegion(cellRangeAddress);row.createCell(7).setCellValue("維修工時記錄");//第10行row=sheet.createRow(9);row.createCell(0).setCellValue("日期");row.createCell(1).setCellValue("維修事項");row.createCell(2).setCellValue("材料清單");row.createCell(3).setCellValue("數(shù)量");row.createCell(4).setCellValue("單價");row.createCell(5).setCellValue("材料金額");row.createCell(7).setCellValue("日期");row.createCell(8).setCellValue("技工");row.createCell(9).setCellValue("工時數(shù)");row.createCell(10).setCellValue("單價");row.createCell(11).setCellValue("工時金額");//填充數(shù)據(jù)for (int i = 0; i < 10; i++) {row=sheet.createRow(9+i+1);row.createCell(0).setCellValue("日期");row.createCell(1).setCellValue("維修事項");row.createCell(2).setCellValue("材料清單");row.createCell(3).setCellValue("數(shù)量");row.createCell(4).setCellValue("單價");row.createCell(5).setCellValue("材料金額");row.createCell(7).setCellValue("日期");row.createCell(8).setCellValue("技工");row.createCell(9).setCellValue("工時數(shù)");row.createCell(10).setCellValue("單價");row.createCell(11).setCellValue("工時金額");}//第n+10行row=sheet.createRow(9+10+1);//cellRangeAddress=new CellRangeAddress(19,19,0,4);//sheet.addMergedRegion(cellRangeAddress);row.createCell(0).setCellValue("累計:");row.createCell(1).setCellValue("xxx");row.createCell(7).setCellValue("累計:");row.createCell(8).setCellValue("xxx");// 新建一輸出文件流FileOutputStream fOut = new FileOutputStream(outputFile);// 把相應的Excel 工作簿存盤workbook.write(fOut);fOut.flush();// 操作結束,關閉文件fOut.close();System.out.println("文件生成...");} catch (Exception e) {System.out.println("已運行 xlCreate() : " + e);}}}
新聞熱點
疑難解答
圖片精選