Java上传Excel并解析

Posted 听世界的喧闹。。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java上传Excel并解析相关的知识,希望对你有一定的参考价值。

1.上传:

public String uploadFile(CommonsMultipartFile file,String uploadPath,String realUploadPath){  
        InputStream is = null;  
        OutputStream os = null;  
        Calendar calendar = Calendar.getInstance();//获取时间  
        long excelName = calendar.getTime().getTime();  
          
        try {  
            is = file.getInputStream();  
            String des = realUploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();  
            os = new FileOutputStream(des);  
              
            byte[] buffer = new byte[1024];  
            int len = 0;  
              
            while((len = is.read(buffer))>0){  
                os.write(buffer);  
            }  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(is!=null){  
                try{  
                    is.close();  
                }catch (Exception e2){  
                    e2.printStackTrace();  
                }  
            }  
              
            if(os!=null){  
                try{  
                    os.close();  
                }catch (Exception e2){  
                    e2.printStackTrace();  
                }  
            }  
        }  
        //返回路径  
        return uploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();  
    }  

2.解析:

常用的Excel解析方式有两种JXL,POI

jxl用起来相对简单,但只支持Excel2003版本,也就是说jxl无法解析.xlsx的Excel文件,而POI会识别Excel版本进行解析,所以大部分人更倾向于POI

jxl使用时需要在项目中导入jxl.jar包

poi需要导入

 

poi-3.14-20160307.jar

 

poi-ooxml-3.14-20160307.jar

poi-ooxml-schemas-3.14-20160307.jar

commons-io-1.4.jar

commons-fileupload-1.2.1.jar

2.1   JXL解析代码:

 

    public String readExcel(CommonsMultipartFile file,HttpServletRequest request)throws IOException, WriteException{  
              
            StringBuffer sb = new StringBuffer();//将读取的内容存入StringBUffer中  
            try {  
                Workbook book = Workbook.getWorkbook(file.getInputStream());  
                try{  
                    Sheet sheet = book.getSheet(0);  
                    for(int i = 0 ; i < 3 ; i++){//i表示行数  
                        for(int j = 0 ; j < 4 ; j++){//j表示列数  
                            sb.append(sheet.getCell(j, i).getContents()+"\t");  
                        }  
                        sb.append("\n");  
                    }  
                    System.out.println(sb);  
                }finally{  
                    if(book != null){  
                        book.close();  
                    }  
                }  
            } catch (BiffException e) {  
                System.err.println(e+"");  
            } catch (IOException e) {  
                System.err.println(e+"文件读取错误");  
            }  
            return "";  
        }  

 

2.2   POI解析代码:

 private POIFSFileSystem fs;  
   private HSSFWorkbook wb;   
   private HSSFSheet sheet;   
   private HSSFRow row;  
   /*读取标题excel第一行内容*/  
public String[] readExcelTitle(InputStream is) {  
       try {  
           fs = new POIFSFileSystem(is);  
           wb = new HSSFWorkbook(fs);  
       } catch (IOException e) {  
           e.printStackTrace();  
       }  
       sheet = wb.getSheetAt(0);  
       row = sheet.getRow(0);  
       // 标题总列数  
       int colNum = row.getPhysicalNumberOfCells();  
       System.out.println("colNum:" + colNum);  
       String[] title = new String[colNum];  
       for (int i = 0; i < colNum; i++) {  
           //title[i] = getStringCellValue(row.getCell((short) i));  
           title[i] = getCellFormatValue(row.getCell((short) i));  
       }  
       return title;  
   }  
/*读取内容*/  
 public void readExcelContent(InputStream is) {  
       Map<Integer, ModelCourse> content = new HashMap<Integer, ModelCourse>();  
       ModelCourse model=new ModelCourse();  
       try {  
           fs = new POIFSFileSystem(is);  
           wb = new HSSFWorkbook(fs);  
       } catch (IOException e) {  
           e.printStackTrace();  
       }  
       sheet = wb.getSheetAt(0);  
       // 得到总行数  
       int rowNum = sheet.getLastRowNum();  
       row = sheet.getRow(0);  
       int colNum = row.getPhysicalNumberOfCells();  
         
       // 正文内容从第二行开始,第一行为表头的标题  
       for (int i = 1; i <=rowNum; i++) {  
           row = sheet.getRow(i);  
           int j = 0;  
            
            
           while (j < colNum) {  
                
            if(j==1){  
                model.setCourse_id(getCellFormatValue(row.getCell((short) j)));  
            }  
            else if(j==2){  
                model.setCourse_name(getCellFormatValue(row.getCell((short) j)));  
            }  
            else if(j==3){  
                model.setCourse_time(getCellFormatValue(row.getCell((short) j)));  
            }  
            else if(j==4){  
                model.setCourse_place(getCellFormatValue(row.getCell((short) j)));  
            }  
               j++;  
                
           }  
           content.put(i, model);  
           addCourse(model);             
       }     
   }  

 

以上是关于Java上传Excel并解析的主要内容,如果未能解决你的问题,请参考以下文章

NPOI 入门--上传excel文件并解析

Java 上传解压zip文件,并且解析文件里面的excel和图片

java上传excel以及解析

从excel表格读取数据用Java代码实现批量上传写入数据库

Java通过jxl解析Excel文件入库,及日期格式处理方式 (附源代码)

js实现上传excel文件并解析