合併單元格:
在POI中,合併單元格只需用到一個方法即可,即addMergedRegion(CellRangeAddress region),
此方法HSSFSheet的一個方法,即在工作薄對象下調用此方法
CellRangeAddress
的構造方法如下:
CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
如:sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
4個參數依次爲:開始行,結束行,開始列,結束列
樣式的處理:
POI中使用樣式的方式有幾種爲:
1.創建樣式對象 --> 在樣式對象中設置各種樣式 --> 把樣式對象賦予sheet對象或row對象或cell對象。
2.直接在sheet對象或row對象或cell對象中調用各種樣式方法。
如:
HSSFCellStyle style = wb.createCellStyle(); // 樣式對象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
Font font = wb.createFont();
font.setFontName("宋體");//字體類型
font.setFontHeightInPoints((short) 10);字體大小
style.setFont(font);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框類型
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框類型
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框類型
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框類型
sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
HSSFCell cellA3 = row3.createCell(0, Cell.CELL_TYPE_STRING);
cellA3.setCellValue("序號");
cellA3.setCellStyle(style);
sheet.setColumnWidth(0, 4*256); //設置列寬(第一列,4個字符長度)
HSSFRow row1 = sheet.createRow(0);
row1.setHeight((short) 400); //設置行高(第一行,高度爲400)
ps:更詳細的操作請查找相關API