有時候我們在做項目時,有些項目需要生成Microsoft Excel文件格式的報告。有時,甚至希望將Excel文件作爲輸入數據。這是我們需要用到Apache POI 。例如,一個公司開發的應用程序將財務部門需要所有輸出生成自己的Excel。
單元格對齊方式
1.編寫主方法
public class Demo8 {
public static void main(String[] args) throws Exception {
// 定義一個新的工作簿
Workbook wb = new HSSFWorkbook() ;
// 創建第一個Sheet頁
Sheet sheet = wb.createSheet("第一個Sheet頁");
// 創建一個行
Row row = sheet.createRow(2) ;
row.setHeightInPoints(30);
// 效果:位於底部,水平居中
createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM) ;
// 定義一個輸出流,並指定輸出路徑
FileOutputStream fileOut = new FileOutputStream("./excel/excel05.xls") ;
// 生成excel 文件
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) ;
}
}
2.查看效果
效果:位於底部,水平居中
單元格邊框處理
public class Demo9 {
public static void main(String[] args) throws Exception {
// 定義一個新的工作簿
Workbook wb = new HSSFWorkbook() ;
// 創建第一個Sheet頁
Sheet sheet = wb.createSheet("第一個Sheet頁");
// 創建一個行
Row row = sheet.createRow(1) ;
Cell cell = row.createCell(1) ;
cell.setCellValue("哈哈哈哈哈哈哈") ;
row.setHeightInPoints(30);
CellStyle cellStyle = wb.createCellStyle() ;
// 設置底部邊框:窄邊框
cellStyle.setBorderBottom(BorderStyle.THIN) ;
// 設置底部顏色
cellStyle.setBottomBorderColor(IndexedColors.GREEN.getIndex());
// 設置左邊框:點邊框
cellStyle.setBorderLeft(BorderStyle.DOTTED) ;
// 設置左邊框顏色
cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex());
// 設置右邊框:中等邊界
cellStyle.setBorderRight(BorderStyle.MEDIUM) ;
// 設置右邊框
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());
cell.setCellStyle(cellStyle) ;
// 定義一個輸出流,並指定輸出路徑
FileOutputStream fileOut = new FileOutputStream("./excel/excel06.xls") ;
// 生成excel 文件
wb.write(fileOut) ;
fileOut.close() ;
}
}
2.查看效果
單元格填充和顏色操作
1.編寫主方法
public class Demo10 {
public static void main(String[] args) throws Exception {
// 定義一個新的工作簿
Workbook wb = new HSSFWorkbook() ;
// 創建第一個Sheet頁
Sheet sheet = wb.createSheet("第一個Sheet頁");
// 創建一個行
Row row = sheet.createRow(1) ;
Cell cell = row.createCell(1) ;
cell.setCellValue("哈哈哈哈哈哈哈") ;
row.setHeightInPoints(30);
CellStyle cellStyle = wb.createCellStyle() ;
// 設置背景色
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()) ;
cellStyle.setFillPattern(CellStyle.BIG_SPOTS) ;
cell.setCellStyle(cellStyle) ;
Cell cell2 = row.createCell(3) ;
cell2.setCellValue("哦哦哦哦哦") ;
row.setHeightInPoints(30);
CellStyle cellStyle2 = wb.createCellStyle() ;
// 設置前景色
cellStyle2.setFillBackgroundColor(IndexedColors.RED.getIndex()) ;
cellStyle2.setFillPattern(CellStyle.THICK_VERT_BANDS) ;
cell2.setCellStyle(cellStyle2) ;
// 定義一個輸出流,並指定輸出路徑
FileOutputStream fileOut = new FileOutputStream("./excel/excel07.xls") ;
// 生成excel 文件
wb.write(fileOut) ;
fileOut.close() ;
}
}
2.查看效果
單元格合併
1.編寫主方法
public class Demo11 {
public static void main(String[] args) throws Exception {
// 定義一個新的工作簿
Workbook wb = new HSSFWorkbook() ;
// 創建第一個Sheet頁
Sheet sheet = wb.createSheet("第一個Sheet頁");
// 創建一個行
Row row = sheet.createRow(1) ;
Cell cell = row.createCell(1) ;
cell.setCellValue("哈哈哈哈哈哈哈") ;
row.setHeightInPoints(30);
// 合併單元格
sheet.addMergedRegion(new CellRangeAddress(
1, // 起始行
2, // 結束行
1, // 起始行
2 // 結束行
)) ;
// 定義一個輸出流,並指定輸出路徑
FileOutputStream fileOut = new FileOutputStream("./excel/excel08.xls") ;
// 生成excel 文件
wb.write(fileOut) ;
fileOut.close() ;
}
}
2.查看效果
有些方法不同是因爲poi架包版本不同,具體請查看api文檔。
本博主用的是:poi-3.15-beta2.jar