MultipartFileはcsvとexcelファイルをアップロードしてデータベースに保存します

4390 ワード

springMVC MultipartFile    :

CSVファイルのインポート:csv->json->list
    public List readCsv(MultipartFile file){
        List list = new ArrayList();

        if (!file.isEmpty()){
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                isr = new InputStreamReader(file.getInputStream());
                br = new BufferedReader(isr);
                String line = null;
                List> strs = new ArrayList>();
                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> 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 getExcelData(MultipartFile mfile, String fileName) {
        List> lists = new ArrayList>();
        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 list = new ArrayList();
                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;
    }