POI:單元格處理(對齊方式、邊框、填充色、合併)
Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
Sheet sheet=wb.createSheet("第一個Sheet頁"); // 建立第一個Sheet頁
Row row=sheet.createRow(2); // 建立一個行
row.setHeightInPoints(30);
createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* 建立一個單元格併為其設定指定的對其方式
* @param wb 工作簿
* @param row 行
* @param column 列
* @param halign 水平方向對其方式
* @param valign 垂直方向對其方式
*/
private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
Cell cell=row.createCell(column); // 建立單元格
cell.setCellValue(new HSSFRichTextString("Align It")); // 設定值
CellStyle cellStyle=wb.createCellStyle(); // 建立單元格樣式
cellStyle.setAlignment(halign); // 設定單元格水平方向對其方式
cellStyle.setVerticalAlignment(valign); // 設定單元格垂直方向對其方式
cell.setCellStyle(cellStyle); // 設定單元格樣式
}
public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
Sheet sheet=wb.createSheet("第一個Sheet頁"); // 建立第一個Sheet頁
Row row=sheet.createRow(1); // 建立一個行
Cell cell=row.createCell(1); // 建立一個單元格
cell.setCellValue(4);
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部邊框
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部邊框顏色
cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左邊邊框
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); // 左邊邊框顏色
cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右邊邊框
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右邊邊框顏色
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上邊邊框
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上邊邊框顏色
cell.setCellStyle(cellStyle);
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}
public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
Sheet sheet=wb.createSheet("第一個Sheet頁"); // 建立第一個Sheet頁
Row row=sheet.createRow(1); // 建立一個行
Cell cell=row.createCell(1);
cell.setCellValue("XX");
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
cell.setCellStyle(cellStyle);
Cell cell2=row.createCell(2);
cell2.setCellValue("YYY");
CellStyle cellStyle2=wb.createCellStyle();
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell2.setCellStyle(cellStyle2);
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}
public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
Sheet sheet=wb.createSheet("第一個Sheet頁"); // 建立第一個Sheet頁
Row row=sheet.createRow(1); // 建立一個行
Cell cell=row.createCell(1);
cell.setCellValue("單元格合併測試");
sheet.addMergedRegion(new CellRangeAddress(
1, // 起始行
2, // 結束行
1, // 其實列
2 // 結束列
));
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}