Java POI 讀寫Excel 文件簡單實現

整理FileUtils的一些方法,只是一些初步實現,剛寫完就掛上來了…

友情提示:**過於結構化,沒太多潤色....碼的不好還請諸位海涵並多提意見**

關聯的類型

資源 類型 說明
Workbook 接口 Excel工作簿的高級表示。這是大多數用戶在閱讀或編寫工作簿時所構建的第一個對象。它也是創建新表/等的頂級對象
WorkbookFactory 創建合適的工作簿(HSSFWorkbook或XSSFWorkbook),從提供的輸入中自動檢測。
HSSFWorkbook .xls (97版和2003版的格式)
XSSFWorkbook .xlsx (2007之後版本格式)
Sheet 接口 Excel工作表的高級表示。表是工作簿中的中心結構,並且是用戶在其電子表格中工作的地方。最常見的表類型是工作表,它表示爲單元格網格。工作表單元格可以包含文本、數字、日期和公式。單元格也可以被格式化。
Row 接口
Cell 接口 單元格

如果有其他需求可參考官方文檔:http://poi.apache.org/apidocs/index.html?overview-summary.html

jar包預覽:這裏寫圖片描述


下載地址:http://download.csdn.net/detail/u013591605/9898273


概述

  1. 讀取:
    利用WorkbookFactory 構建一個Workbook對象,按次生成Sheet[1…255]03版,[1…n]07版.根據數據源容器(size)創建對應行(row)的個數rowNumber = size+1(保留一個title),可以理解爲一個row代表一個Object,而對應行(row)所包含的單元格(Cell)就是Object的成員屬性.
  2. 寫入:
    與讀取方法不同的事,寫入根據實際需求決定是哪種文件格式,另外Cell.setCellStyle()可以定義單元格的外觀,換句話說,寫入的時候如果將寫入的數據只是作爲一般存儲,也沒必要糾結於樣式.
  3. Excel 提供的6中參數類型:
Tag 說明
Cell.CELL_TYPE_STRING 代表文本
Cell.CELL_TYPE_BLANK 空白格
Cell.CELL_TYPE_BOOLEAN: 布爾型
Cell.CELL_TYPE_NUMERIC 數字
Cell.CELL_TYPE_ERROR 錯誤
Cell.CELL_TYPE_FORMULA 公式

代碼部分

讀取Excel文件

public static <T> Map<String, List<? extends T>> readExcel(String path, Class clzz) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<T> list = new LinkedList<T>(); 
        Map<String, List<? extends T>> map = new HashMap<String, List<? extends T>>();
        File file = new File(path);
        FileInputStream fis = null;
        Workbook workBook = null;
        if (file.exists()) {
            try {
                fis = new FileInputStream(file);
                workBook = WorkbookFactory.create(fis);
                int numberOfSheets = workBook.getNumberOfSheets();
                for (int s = 0; s < numberOfSheets; s++) { // sheet工作表
                    Sheet sheetAt = workBook.getSheetAt(s);
//                  String sheetName = sheetAt.getSheetName(); //獲取工作表名稱
                    int rowsOfSheet = sheetAt.getPhysicalNumberOfRows(); // 獲取當前Sheet的總列數
                    System.out.println("當前表格的總行數:" + rowsOfSheet);
                    for (int r = 0; r < rowsOfSheet; r++) { // 總行
                        Row row = sheetAt.getRow(r);
                        if (row == null) {
                            continue;
                        } else {
                            int rowNum = row.getRowNum();
                            System.out.println("當前行:" + rowNum);
                            int numberOfCells = row.getPhysicalNumberOfCells();
                            for (int c = 0; c < numberOfCells; c++) { // 總列(格)
                                Cell cell = row.getCell(c);
                                if (cell == null) {
                                    continue;
                                } else {
                                    int cellType = cell.getCellType();
                                    switch (cellType) {
                                    case Cell.CELL_TYPE_STRING: // 代表文本
                                        String stringCellValue = cell.getStringCellValue();
                                        System.out.print(stringCellValue + "\t");
                                        break;
                                    case Cell.CELL_TYPE_BLANK: // 空白格
                                        String stringCellBlankValue = cell.getStringCellValue();
                                        System.out.print(stringCellBlankValue + "\t");
                                        break;
                                    case Cell.CELL_TYPE_BOOLEAN: // 布爾型
                                        boolean booleanCellValue = cell.getBooleanCellValue();
                                        System.out.print(booleanCellValue + "\t");
                                        break;
                                    case Cell.CELL_TYPE_NUMERIC: // 數字||日期
                                        boolean cellDateFormatted = DateUtil.isCellDateFormatted(cell);
                                        if (cellDateFormatted) {
                                            Date dateCellValue = cell.getDateCellValue();
                                            System.out.print(sdf.format(dateCellValue) + "\t");
                                        } else {
                                            double numericCellValue = cell.getNumericCellValue();
                                            System.out.print(numericCellValue + "\t");
                                        }
                                        break;
                                    case Cell.CELL_TYPE_ERROR: // 錯誤
                                        byte errorCellValue = cell.getErrorCellValue();
                                        System.out.print(errorCellValue + "\t");
                                        break;
                                    case Cell.CELL_TYPE_FORMULA: // 公式
                                        int cachedFormulaResultType = cell.getCachedFormulaResultType();
                                        System.out.print(cachedFormulaResultType + "\t");
                                        break;
                                    }
                                }
                            }
                            System.out.println(" \t ");
                        }
                        System.out.println("");
                    }
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("文件不存在!");
        }
        return map;
    }

寫入Excel

@SuppressWarnings("resource")
    public static <T> void writeExcel(String path, List<T> list, Class<T> clzz) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Field[] declaredFields = clzz.getDeclaredFields();
        File file = new File(path);
        FileOutputStream fos = null;
        Workbook workbook = null;
        try {
            if (file.exists()) {
                fos = new FileOutputStream(file);
                String suffix = getSuffix(path);
                if (suffix.equalsIgnoreCase("XLSX")) {
                    workbook = new XSSFWorkbook();
                } else if (suffix.equalsIgnoreCase("XLS")) {
                    workbook = new HSSFWorkbook();
                } else {
                    throw new Exception("當前文件不是excel文件");
                }
                Sheet sheet = workbook.createSheet(); // 生成工作表
                Row row = sheet.createRow(0);
                for (int m = 0; m < declaredFields.length; m++) { // 設置title
                    Field field = declaredFields[m];
                    field.setAccessible(true);
                    Cell cell = row.createCell(m);
                    String name = field.getName();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue(name);
                }
                for (int i = 0; i < list.size(); i++) { // 數據
                    T instance = list.get(i);
                    row = sheet.createRow(i + 1);
                    for (int j = 0; j < declaredFields.length; j++) {
                        Field field = declaredFields[j];
                        Object value = field.get(instance);
                        String fieldTypeName = field.getGenericType().getTypeName();
                        field.setAccessible(true);
                        Cell cell = row.createCell(j);
                        switch (fieldTypeName) {// content
                        case "long":
                            double d = Double.valueOf(value.toString());
                            cell.setCellValue(d);
                            break;
                        case "float":
                            CellStyle floatStyle = workbook.createCellStyle();
                            short format = workbook.createDataFormat().getFormat(".00");// 保留2位精度
                            floatStyle.setDataFormat(format);
                            double d1 = Double.parseDouble(String.valueOf(value));
                            cell.setCellStyle(floatStyle);
                            cell.setCellValue(d1);
                            break;
                        case "int":
                            double d2 = Double.parseDouble(String.valueOf(value));
                            cell.setCellValue(d2);
                            break;
                        case "java.util.Date":
                            CellStyle dateStyle = workbook.createCellStyle();
                            short df = workbook.createDataFormat().getFormat("yyyy-mm-dd");
                            dateStyle.setDataFormat(df);
                            cell.setCellStyle(dateStyle);
                            String format2 = sdf.format(value);
                            Date date = sdf.parse(format2);
                            cell.setCellValue(date);
                            break;
                        case "java.lang.String":
                            cell.setCellValue(value.toString());
                            break;
                        }
                    }
                    workbook.write(fos);
                }
            } else {
                if (file.createNewFile()) {
                    writeExcel(path, list, clzz);
                } else {
                    System.out.println("創建Excel表格失敗!");
                }
            }
            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

截取文件後綴

public static String getSuffix(String path) {
        String substring = path.substring(path.lastIndexOf(".") + 1);
        return substring;
    }

初步實現大概就是這樣,還有很多不完善的地方:

  1. 比如數據類型對應(本人用的校驗不完善);
  2. 還有就是讀寫效率與性能優化;
  3. 數據同步,避免髒讀髒寫;
  4. 其它的以後補充…

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