JavaNote 4.8 使用POI創建Excel

一、Code

1、
package com.test;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Readxlsx {
        public static HSSFWorkbook workbook;
        public static FileOutputStream fileOutputStream;
        public static int RowNum = 10;
        public static int CellNum = 4;
        public static String filepath = "src/main/resources/Test.xls";
        public static void main(String args[]){
                Readxlsx readxlsx = new Readxlsx();
                readxlsx.createXlsx(filepath);
        }
        public String createXlsx(String filepath){
            File file = new File(filepath);
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                fileOutputStream = new FileOutputStream(file);
                workbook = new HSSFWorkbook();
                Sheet sheet = workbook.createSheet("Test");
                //凍結首行
                sheet.createFreezePane(0,1,0,1);
                /*
                * 凍結首列   sheet.createFreezePane(1,0,1,1);
                * createFreezePane的第一個參數表示列,從1開始,0表示不凍結列
                * 第二個參數表示行
                * 第三個參數表示未被凍結的可見列,從1開始,如果第一個參數爲0,此參數設置無效,如第一個參數爲1,則凍結1列,第三個參數爲2的話,第二列會不可見,需要拖動滾動條纔可見
                * 第四個參數表示行,使用與第三個參數一樣
                * */
                for(int i = 0;i<CellNum; i++){
                    //設置單元格寬度,第一個參數表示列,第二個參數表示寬度
                    sheet.setColumnWidth(i, 5000);
                }
                //創建workbook的樣式
                HSSFCellStyle style = workbook.createCellStyle();
                //水平居中
                style.setAlignment(HorizontalAlignment.CENTER);
                //垂直居中
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                //設置邊框顏色
                style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
                style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
                style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
                style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
                //設置邊框樣式
                style.setBorderBottom(BorderStyle.MEDIUM);
                style.setBorderLeft(BorderStyle.MEDIUM);
                style.setBorderRight(BorderStyle.MEDIUM);
                style.setBorderTop(BorderStyle.MEDIUM);
                //自動換行
                style.setWrapText(true);

                //創建字體
                Font font = workbook.createFont();
                //加粗
                font.setBold(true);
                //字體名稱
                font.setFontName("宋體");
                //字體顏色
                font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
                //字體大小
                font.setFontHeightInPoints((short) 14);
                //樣式中應用字體
                style.setFont(font);
                for(int i = 0;i < RowNum;i++){
                    //創建列
                    Row row = sheet.createRow(i);
                    //設置行高
                    row.setHeight((short)500);
                    for(int j = 0; j<CellNum ;j++){
                        //創建單元格
                        Cell cell = row.createCell(j);
                        //設置單元格格式
                       // cell.setCellType(CellType.STRING);
                        //應用單元格樣式
                        cell.setCellStyle(style);
                        if(i==0){
                            //將自定義Enum的值寫入標題行row等於0
                            switch (j){
                                case 0:
                                    cell.setCellValue(TestEnum.User.toString(j));
                                    break;
                                case 1:
                                    cell.setCellValue(TestEnum.User.toString(j));
                                    break;
                                case 2:
                                    cell.setCellValue(TestEnum.User.toString(j));
                                    break;
                                case 3:
                                    cell.setCellValue(TestEnum.User.toString(j));
                                    break;
                            }
                        }
                    }
                }
                workbook.write(fileOutputStream);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
              catch (IOException e){
                e.getStackTrace();
              }
              finally {
                try {
                    fileOutputStream.close();
                    System.out.println("Excel生成成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
              return file.getName();
        }

}
2、
package com.test;

public enum TestEnum {
    User("用戶", 0), Id("賬號", 1), Age("年齡", 2), Sex("性別", 3);
    private String name;
    private int index;
     TestEnum(String name, int index) {
        this.name = name;
        this.index = index;
    }
    public String toString(int index){
         for(TestEnum testEnum:TestEnum.values()){
             if(testEnum.index == index){
                 return testEnum.name;
             }
         }
         return null;
    }

}

二、效果展示

 

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