工作薄中不同類型的單元格
[java]
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(2);
row.createCell(0).setCellValue(1.1);//浮點
row.createCell(1).setCellValue(new Date());//日期
row.createCell(2).setCellValue(Calendar.getInstance());//日期
row.createCell(3).setCellValue("a string");//字符串
row.createCell(4).setCellValue(true);//Boolean
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);//錯誤
// 寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(2);
row.createCell(0).setCellValue(1.1);//浮點
row.createCell(1).setCellValue(new Date());//日期
row.createCell(2).setCellValue(Calendar.getInstance());//日期
row.createCell(3).setCellValue("a string");//字符串
row.createCell(4).setCellValue(true);//Boolean
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);//錯誤
// 寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
運行結果:
展示各種對其選項
[java]
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook(); //或者創建 HSSFWorkbook();對象
Sheet sheet = wb.createSheet();//創建工作薄
Row row = sheet.createRow((short) 2);//創建行
row.setHeightInPoints(30);//設置高度Point像素單位設置高度
createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("xssf-align.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* 創建一個單元格,並以一種特定的樣式對齊
*
* @param wb Workbook對象
* @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
cell.setCellValue("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 XSSFWorkbook(); //或者創建 HSSFWorkbook();對象
Sheet sheet = wb.createSheet();//創建工作薄
Row row = sheet.createRow((short) 2);//創建行
row.setHeightInPoints(30);//設置高度Point像素單位設置高度
createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("xssf-align.xls");
wb.write(fileOut); www.aiwalls.com
fileOut.close();
}
/**
* 創建一個單元格,並以一種特定的樣式對齊
*
* @param wb Workbook對象
* @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
cell.setCellValue("Align It");//設置值
CellStyle cellStyle = wb.createCellStyle();//創建樣式
cellStyle.setAlignment(halign);//設置水平對齊方式
cellStyle.setVerticalAlignment(valign);//設置垂直對齊方式
cell.setCellStyle(cellStyle);//給單元格添加樣式
}
運行結果:
作者:yhc13429826359