Hutool using simple tools excel import and export tools

public  class ExcelUtils { 

    Private  static  Final Logger Logger = LoggerFactory.getLogger (ExcelUtils. class ); 

    Private  static List <List <Object >> lineList = new new the ArrayList <> (); 

    / ** 
     * Excel export tools 
     * 
     * @param Response 
     * @param fileName file name 
     * @param the Projects collection of objects 
     * @param column names columnNames exported excel in 
     * @param Keys corresponds to the field name object 
     * @throws IOException
     * / 
    Public  static  void Export (the HttpServletResponse Response, String fileName, List Projects, String [] the columnNames, String [] Keys <?>) Throws IOException { 

        ExcelWriter bigWriter = ExcelUtil.getBigWriter (); 

        for ( int I = 0; I < columnNames.length; I ++ ) { 
            bigWriter.addHeaderAlias (the columnNames [I], Keys [I]); 
            bigWriter.setColumnWidth (I, 20 is ); 
        } 
        // disposable on writing, the default style, forced output header 
        bigWriter.write (the Projects, to true );
         // the Response to HttpServletResponse object
        response.setContentType ( "the Application / vnd.ms-Excel; charset = UTF-8" );
         // Test.xls is a pop-up dialog box to download the file name, can not be Chinese, Chinese make their own coding 
        response.setHeader ( "Content- Disposition "," Attachment; filename = "+ new new String ((fileName +" .xlsx ") the getBytes (),." ISO-8859-1 " )); 
        the ServletOutputStream OUT = response.getOutputStream (); 
        bigWriter.flush (OUT , to true );
         // Close writer, the release of memory 
        bigWriter.close ();
         // remember to turn off the output stream Servlet herein 
        IoUtil.close (OUT); 
    } 


    / ** 
     * Excel import tools 
     * 
     * @paramfile file 
     * @param field name corresponding to a column columNames 
     * @return return data set 
     * @throws OperationException 
     * @throws IOException
      * / 
    public  static List <the Map <String, Object >> leading (a MultipartFile file, String [] columNames) throws OperationException , IOException { 
        String fileName = file.getOriginalFilename ();
         // uploaded file is empty 
        IF (StringUtils.isEmpty (fileName)) {
             the throw  new new OperationException (ReturnCodeEnum.OPERATION_EXCEL_ERROR, "no import file" );
        } 
        // upload file size to 1000 data 
        IF (file.getSize ()> 1024 * 1024 * 10 ) { 
            logger.error ( "the Upload | Upload failed: file size exceeds 10M, file size: {}" , File. getSize ());
             the throw  new new OperationException (ReturnCodeEnum.OPERATION_EXCEL_ERROR, "upload failed:! file size can not exceed 10M" ;) 
        } 
        // upload the file name format 
        IF "." (fileName.lastIndexOf ()! = -1 && !. "XLSX" .equals (fileName.substring (fileName.lastIndexOf ( "." )))) {
             the throw  new new OperationException (ReturnCodeEnum.OPERATION_EXCEL_ERROR, "file name format is incorrect, please use the file name suffix .XLSX" ) ;
        } 

        // read data
        ExcelUtil.read07BySax(file.getInputStream(), 0, createRowHandler());
        //去除excel中的第一行数据
        lineList.remove(0);

        //将数据封装到list<Map>中
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 0; i < lineList.size(); i++) {
            if (null != lineList.get(i)) {
                Map<String, Object> hashMap = new HashMap<>();
                for (int j = 0; j < columNames.length; j++) {
                    Property Object = lineList.get (I) .get (J); 
                    hashMap.put (columNames [J], Property); 
                } 
                dataList.add (hashMap '); 
            } the else {
                 BREAK ; 
            } 
        } 
        return dataList; 
    } 

    / ** 
     * we should write operation for each row of data by implementing the method handle 
     * / 
    Private  static RowHandler createRowHandler () {
         // empty at the data collection 
        lineList.removeAll (lineList);
         return  new new RowHandler () { 
            @Override
            public  void handle ( int sheetIndex, int rowIndex, List rowlist) {
                 // the read data into each row of the list to set 
                the JSONArray jsonObject = new new the JSONArray (rowlist); 
                . lineList.add (jsonObject.toList (Object class )); 
            } 
        }; 
    } 
}

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/12128411.html