POI是Java对Office操作的常用工具包,网上资料有很多,但良莠不齐,临时便找边试容易耽误时间,故收集一些常用操作在这里以便查阅.
1.创建SheetSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "Sheet0");
2.设置列宽static final int[] COLUMN_WIDTHS = new int[] { 4*3500, 2*3500, 5*3500,
2*3500, 3*3500, 3*3500, 3500, 3*3500, 2*3800, 2*3500};
for (int colnum = 0; colnum < COLUMN_WIDTHS.length; colnum++) {
sheet.setColumnWidth(colnum, COLUMN_WIDTHS[colnum]);
}
3.设置缺省行高sheet.setDefaultRowHeight((short)360);
4.设置某一行行高Row rowFirst = sheet.createRow(0);
rowFirst.setHeightInPoints(20.0f);
5.给单元格设置字体及下边框Cell cell00 = rowFirst.createCell(0);
cell00.setCellValue("abc123");
HSSFFont fontAriel12Bold = workbook.createFont();
fontAriel12Bold.setFontName("Arial");
fontAriel12Bold.setFontHeightInPoints((short)12);
fontAriel12Bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontAriel12Bold.setColor(HSSFColor.RED.index);
HSSFCellStyle cell00FontStyle = workbook.createCellStyle();
cell00FontStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK);
cell00FontStyle.setFont(fontAriel12Bold);
cell00.setCellStyle(cell00FontStyle);
6.给单元格设置背景色
HSSFCellStyle blueStyle = workbook.createCellStyle();
blueStyle.setBorderTop(HSSFCellStyle.BORDER_THICK);
blueStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
blueStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
Cell cell=row9.createCell(0);
cell.setCellStyle(blueStyle);