1. 程式人生 > >MultipartFile上傳csv和excel檔案儲存到資料庫中

MultipartFile上傳csv和excel檔案儲存到資料庫中

springMVC中MultipartFile接收檔案:

匯入CSV檔案:csv->json->list<類>

    public List<T> readCsv(MultipartFile file){
        List<T> list = new ArrayList<T>();

        if (!file.isEmpty()){
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                isr = new InputStreamReader(file.getInputStream());
                br = new BufferedReader(isr);
                String line = null;
                List<List<String>> strs = new ArrayList<List<String>>();
                while ((line = br.readLine()) != null){
                    strs.add(Arrays.asList(line.split(",")));
                }
                JSONArray array = toJsonArray(strs);
                list = array.toJavaList(T.class);
            } catch (IOException e) {
               //
            }finally {
                try {
                    if (br != null){
                        br.close();
                    }
                    if (isr != null){
                        isr.close();
                    }
                } catch (IOException e) {
                    //
                }
            }
        }else {
            //
        }
        return list;
    }

    //fastjson
    private JSONArray toJsonArray(List<List<String>> strs){
        JSONArray array = new JSONArray();
        for (int i = 1; i < strs.size(); i++) {
            JSONObject object = new JSONObject();
            for (int j = 0; j < strs.get(0).size(); j++) {
                object.put(strs.get(0).get(j),strs.get(i).get(j));
            }
            array.add(object);
        }
        return array;
    }

匯出excel檔案:

public class ExportExcelFile {
    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

   
    //判斷版本獲取Wordboook
    private static Workbook getWorkbook(InputStream in, String fileName) throws IOException {
        Workbook wbook = null;
        if (fileName.endsWith(EXCEL_XLS)) {
            wbook = new HSSFWorkbook(in);
        } else if (fileName.endsWith(EXCEL_XLSX)) {
            wbook = new XSSFWorkbook(in);
        }
        return wbook;
    }

  
    //解析excel檔案問物件集合
    public static List<T> getExcelData(MultipartFile mfile, String fileName) {
        List<List<String>> lists = new ArrayList<List<String>>();
        try {
            Workbook workbook = getWorkbook(mfile.getInputStream(), fileName);
            //獲取Sheet的數量
            int sheetCount = workbook.getNumberOfSheets();
            // 第一個Sheet
            Sheet sheet = workbook.getSheetAt(0);
            //表頭
            Row rowHead = sheet.getRow(0);
            //總列數
            int columns = rowHead.getPhysicalNumberOfCells();
            //總行數
            int lines = sheet.getPhysicalNumberOfRows();
            //迴圈獲取每行資料
            for (int i = 0; i < lines; i++) {
                //迴圈獲取每列
                List<String> list = new ArrayList<String>();
                for (int j = 0; j < columns; j++) {
                    if (sheet.getRow(i).getCell(j) != null){
                        Object obj = getValue(sheet.getRow(i).getCell(j));
                        list.add(String.valueOf(obj));
                    }else {
                        list.add(null);
                    }
                }
                lists.add(list);

            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONArray array = toJsonArray(lists);
        return array.toJavaList(User.class);
    }

     //獲取對應的列舉型別資料
    private static Object getValue(Cell cell) {
        Object obj = null;
        switch (cell.getCellTypeEnum()) {
            case BOOLEAN:
                obj = cell.getBooleanCellValue();
                break;
            case ERROR:
                obj = cell.getErrorCellValue();
                break;
            case NUMERIC:
                obj = cell.getNumericCellValue();
                break;
            case STRING:
                obj = cell.getStringCellValue();
                break;
            default:
                break;
        }
        return obj;
    }