Excel導入工具類


項目需要從Excel導入數據,然后插入到數據庫對應表中。設計了一個導入工具類,導入數據和導入結果如下圖示:

 

 

 

 

 

poi jar版本采用的3.15

導入工具類實現如下:

package com.alphajuns.ssm.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 導入工具類
 */
public class ExcelPoiImportUtils {

    /**
     * 導入測試
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("F:\\樣機試制新的導入模板1.xlsx");
        List<List<String>> excelListList = importFromExcel(file);
        for (int i = 0; i < excelListList.size(); i++) {
            System.out.println(excelListList.get(i));
        }
    }

    /**
     * 導入
     * @param file
     * @return
     */
    public static List<List<String>> importFromExcel(File file) {
        // 用於保存讀取的Excel信息
        List<List<String>> excelListList = new ArrayList<List<String>>();
        // 創建工作簿
        Workbook workBook = null;
        // 獲取文件名
        String fileName = file.getName();
        // 判斷Excel類型,是Excel2003還是Excel2007,通過文件名后綴判斷
        try {
            if (fileName.endsWith("xls")) {
            workBook = new HSSFWorkbook(new FileInputStream(file));
            } else if (fileName.endsWith("xlsx")) {
                workBook = new XSSFWorkbook(new FileInputStream(file));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 獲得第一個sheet
        Sheet sheet = workBook.getSheetAt(0);
        // 獲得該sheet的所有行
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 0; i < rows; i++) {
            // 獲取一行
            Row row = sheet.getRow(i);
            // 第一行為標題行,跳過
            if (i == 0) {
                continue;
            }
            // 獲得列數
            int cellNums = row.getLastCellNum();
            // 用於保存每行數據
            List<String> excelList = new ArrayList<String>();
            // 一次保存列信息
            for (int j = 0; j < cellNums; j++) {
                Cell cell = row.getCell(j);
                String cellValue = getCellValue(cell).trim();
                excelList.add(cellValue);
            }
            excelListList.add(excelList);
        }

        return excelListList;
    }

    /**
     * 獲取單元格的值
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell) {
        String cellValue = null;
        if (cell == null) {
            cellValue = "";
        }
        // 獲取單元格類型
        int cellType = cell.getCellType();
        DecimalFormat decimalFormat = new DecimalFormat("0");
        switch (cellType) {
            case Cell.CELL_TYPE_BLANK:
                cellValue = "";
                break;
            case Cell.CELL_TYPE_NUMERIC:
                // 判斷是否為日期
                if (DateUtil.isCellDateFormatted(cell)) {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    cellValue = dateFormat.format(cell.getDateCellValue());
                } else {
                    String number = String.valueOf(cell.getNumericCellValue());
                    // 是否是浮點數
                    if (number.indexOf(".") != -1) {
                        decimalFormat = new DecimalFormat("#.###");
                    }
                    cellValue = decimalFormat.format(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                // 公式需要獲取其數值
                cell.setCellType(CellType.NUMERIC);
                cellValue = decimalFormat.format(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
            case Cell.CELL_TYPE_ERROR:
                cellValue = String.valueOf(cell.getErrorCellValue());
                break;
            default:
                cellValue = cell.getStringCellValue();
                break;
        }

        return cellValue;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM