Upload Excel file into database using Apache POI and Spring framework

后端 未结 1 2020
傲寒
傲寒 2021-02-04 22:23

Couldn\'t find a good post on the internet to help me with this.

My requirement is to read each row from a spreadsheet, and generate a sql statement with values from the

1条回答
  •  温柔的废话
    2021-02-04 23:02

    I was going to do the same thing some weeks ago, ended up with the following solution for the excel part of your question. This solution support both the new and the 97-2007 sheet format. I am using spring and POI. I dont think it is posible to answer the rest of your question without more information.

    the jsp site where the user upload the file:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        import
    
    
        
            Select file

    the controler that will be triggered on submit

    @Controller
    @RequestMapping("/upload")
    public class ExcelImporterController {
    
        @RequestMapping(method = RequestMethod.POST)
        public String upload(FileBean uploadItem, BindingResult result) {
            importService.import(uploadItem);
    
            return "import/importDone";
        }
    
    }
    

    interface..

    public interface importService {
    
        public void import(FileBean fileBean);
    }
    

    implementation of the interface with the import method..

    @Override
        public void import(FileBean fileBean) {
    
            ByteArrayInputStream bis = new ByteArrayInputStream(filedBean.getFileData().getBytes());
            Workbook workbook;
            try {
                if (fileBean.getFileData().getOriginalFilename().endsWith("xls")) {
                    workbook = new HSSFWorkbook(bis);
                } else if (fileBean.getFileData().getOriginalFilename().endsWith("xlsx")) {
                    workbook = new XSSFWorkbook(bis);
                } else {
                    throw new IllegalArgumentException("Received file does not have a standard excel extension.");
                }
    
                for (Row row : sheet) {
                   if (row.getRowNum() == 0) {
                      Iterator cellIterator = row.cellIterator();
                      while (cellIterator.hasNext()) {
                          Cell cell = cellIterator.next();
                          //go from cell to cell and do create sql based on the content
                      }
                   }
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    the configuration of the bean that will be used for the spring upload in the FileBean..

    
        
    
    

    the file bean

    public class FileBean {
    
      private CommonsMultipartFile fileData;
    
      public CommonsMultipartFile getFileData()
      {
        return fileData;
      }
    
      public void setFileData(CommonsMultipartFile fileData)
      {
        this.fileData = fileData;
      }
    

    }

    0 讨论(0)
提交回复
热议问题