Java使用poi對(duì)Execl簡(jiǎn)單寫操作

public class WriteExecl {

    public void writeExeclTest() throws Exception{
        OutputStream os = new FileOutputStream("F:/execl/writeTest2.xlsx");
        //Workbook wb = new HSSFWorkbook(); // 創(chuàng)建一個(gè) 2003 版本的Execl
        Workbook wb = new XSSFWorkbook(); // 創(chuàng)建一個(gè) 2007 以上版本的Execl
         
        CellStyle cellStyle = wb.createCellStyle(); // 創(chuàng)建一個(gè)樣式
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 單元格邊框粗細(xì)
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);// 單元格邊框粗細(xì)
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);// 單元格邊框假粗細(xì)
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);// 單元格邊框粗細(xì)
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中 
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中
        cellStyle.setBottomBorderColor(IndexedColors.RED.getIndex()); // 設(shè)置底邊框?yàn)榧t色
        cellStyle.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex()); // 設(shè)置前景色
        cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 設(shè)置單元格填充顏色
         
        Sheet sheet = wb.createSheet("第一個(gè)sheet"); // 創(chuàng)建一個(gè)名字的sheet
        sheet.setDefaultColumnWidth(1000); // 設(shè)置sheet中的默認(rèn)寬度
        sheet.setDefaultRowHeight((short) 800); // 設(shè)置sheet中的默認(rèn)高度
         
        Row row = sheet.createRow(0); // 創(chuàng)建一行
        row.createCell(0, Cell.CELL_TYPE_BLANK); // 創(chuàng)建一個(gè)blank的單元格
        row.createCell(1, Cell.CELL_TYPE_BOOLEAN).setCellValue(true); // 創(chuàng)建一個(gè)boolean的單元格
        row.createCell(2, Cell.CELL_TYPE_FORMULA).setCellValue(3.141592653); // 創(chuàng)建一個(gè)formula的單元格
        row.createCell(3, Cell.CELL_TYPE_NUMERIC).setCellValue(0); // 創(chuàng)建一個(gè)numeric的單元格
        row.createCell(4, Cell.CELL_TYPE_STRING).setCellValue("String..."); // 創(chuàng)建一個(gè)string的單元格
        row.setRowStyle(cellStyle); // 設(shè)置行的樣式
        wb.write(os); // 向內(nèi)存中的Execl寫出orkbook工作簿
        os.close(); // 記得關(guān)閉流,釋放資源
    }
}