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); //第一個參數代表列id(從0開始),第2個參數代表寬度值
六、設置自動換行:
setBorder.setWrapText(true);//設置自動換行
七、合並單元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//參數1:行號 參數2:起始列號 參數3:行號 參數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 {
// 創建新的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的位置創建行(最頂端的行)
HSSFRow row = sheet.createRow(0);
// 在索引0的位置創建單元格(左上端)
HSSFCell cell = row.createCell(0);
// 定義單元格為字符串類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
// 在單元格中輸入一些內容
cell.setCellValue(new HSSFRichTextString("北京億卡聯科技發展有限公司小區門禁維修月報表"));
//第二行
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("小區名稱:");
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("聯系人");
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("