POI的基本應用

POI可以生成excel文件,基本用法較簡單,使用如下:

public class App {

   

    public static void main(String[] args) {

       try {

           HSSFWorkbook wb = new HSSFWorkbook();

           //創建sheet

           HSSFSheet sheet = wb.createSheet("first sheet");

           wb.createSheet("second sheet");

           //創建row

           HSSFRow row = sheet.createRow(0);

           //創建單元格並設置單元格值

           row.createCell(0).setCellValue(false);// boolean

           row.createCell(1).setCellValue(Calendar.getInstance());//日曆

           row.createCell(2).setCellValue(new Date());//date

           row.createCell(3).setCellValue(123456789.9876543);//double

           //string

           row.createCell(4).setCellValue(new HSSFRichTextString("文本"));

          

           //對數據進行格式化顯示

           HSSFCell cell = row.getCell(1);

           //通過workbook創建數據格式對象

           HSSFDataFormat format = wb.createDataFormat();

           //通過workbook創建單元格樣式對象

           HSSFCellStyle style = wb.createCellStyle();

           //設置數據格式

           style.setDataFormat(format.getFormat("yyyy-MM-dd hh:mm:ss"));

           //設置單元格的樣式對象

           cell.setCellStyle(style);

           row.getCell(2).setCellStyle(style);

          

           style = wb.createCellStyle();

           style.setDataFormat(format.getFormat("#,###.0000"));

           row.getCell(3).setCellStyle(style);

          

           //設置列寬 1 = 1/20 ()

           sheet.setColumnWidth(1, 6000);

           sheet.autoSizeColumn((short)2);//設置自動列寬

          

           //操縱文本

           row = sheet.createRow(1);

           row.createCell(0).setCellValue(new HSSFRichTextString("左下"));

           row.createCell(1).setCellValue(new HSSFRichTextString("中中"));

           row.createCell(2).setCellValue(new HSSFRichTextString("右上"));

          

           //設置文本的對齊方式

           cell = row.getCell(0);

           style = wb.createCellStyle();

           style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左對齊

           style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);//下對齊

           cell.setCellStyle(style);

          

           cell = row.getCell(1);

           style = wb.createCellStyle();

           style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

           style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//下對齊

           cell.setCellStyle(style);

          

           cell = row.getCell(2);

           style = wb.createCellStyle();

           style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//左對齊

           style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//下對齊

           cell.setCellStyle(style);

          

           row.setHeightInPoints(50);//設置行高

          

           //設置文本旋轉

           cell = row.getCell(0);

           style = cell.getCellStyle();

           style.setRotation((short)30);

          

           //設置字體和顏色

           HSSFFont font = wb.createFont();

           font.setFontName("宋體");

           font.setFontHeight((short)300);

           font.setColor(HSSFColor.RED.index);

           style.setFont(font);

          

           //設置文本回繞

           String str =

               "dddddddddsdfasfdoiuretjijrtlwjertlkwej;lkouifdiudugsfd" ;

           cell = row.createCell(3);

           cell.setCellValue(new HSSFRichTextString(str));

           style = wb.createCellStyle();

           style.setWrapText(true);

           cell.setCellStyle(style);

          

           //設置邊框的特效

           row = sheet.createRow(2);

           cell = row.createCell(0);

           style = wb.createCellStyle();

           style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);

           style.setTopBorderColor(HSSFColor.GREEN.index);

           cell.setCellStyle(style);

          

           cell = row.createCell(1);

           style = wb.createCellStyle();

           style.setRightBorderColor(HSSFColor.RED.index);

           style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);

           FileOutputStream fos = new FileOutputStream("d:/pio.xls");

           cell.setCellStyle(style);

          

           //創建計算列

           row = sheet.createRow(3);

           row.createCell(0).setCellValue(13);

           row.createCell(1).setCellValue(15);

           row.createCell(2).setCellValue(17);

          

           cell = row.createCell(3);

           cell.setCellFormula("average(A4:C4)");

          

           //移動行

           //sheet.shiftRows(1, 3, 2);

          

           //拆分窗口

           //1000:x軸拆分距離 2000:y軸拆分距離 2:右側窗格從第幾列開始顯示

           // 3:下端窗格從第幾行開始顯示 1:激活的窗格

           //sheet.createSplitPane(1000, 2000, 2, 3, 1);

          

           //凍結窗口

           //1:列的凍結數量 2:行的凍結數量 3:右側窗格從第幾列開始顯示

           //4:下端窗格從第幾行開始顯示

           sheet.createFreezePane(1, 2, 3, 4);

           wb.write(fos);

           fos.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章