【问题标题】:Upload Excel file into database using Apache POI and Spring framework使用 Apache POI 和 Spring 框架将 Excel 文件上传到数据库
【发布时间】:2013-02-06 23:15:31
【问题描述】:

在互联网上找不到可以帮助我解决此问题的好帖子。

我的要求是从电子表格中读取每一行,并使用单元格中的值生成一个 sql 语句,并在读取电子表格完成后进行批量上传。

我正在使用 Apache POI、Spring 框架和 JDBC。

我应该如何从excel生成sqls?

  1. 是否有带参数 (?) 的 sql 语句和单元格内容的格式?

  1. 通过连接单元格内容准备 sql?

最好的方法是什么??

【问题讨论】:

标签: java spring apache apache-poi


【解决方案1】:

几周前我打算做同样的事情,最终为您问题的 excel 部分提供了以下解决方案。该解决方案支持新的和 97-2007 表格式。我正在使用弹簧和 POI。如果没有更多信息,我认为不可能回答您的其余问题。

用户上传文件的jsp站点:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>import</title>
</head>
<body>
    <form:form modelAttribute="fileBean" method="post" enctype="multipart/form-data">
        <form:label for="fileData" path="fileData">Select file</form:label><br/><br/>
        <form:input path="fileData" type="file"/>
        <input type="submit" />
    </form:form>
</body>
</html>

提交时将触发的控制器

@Controller
@RequestMapping("/upload")
public class ExcelImporterController {

    @RequestMapping(method = RequestMethod.POST)
    public String upload(FileBean uploadItem, BindingResult result) {
        importService.import(uploadItem);

        return "import/importDone";
    }

}

界面..

public interface importService {

    public void import(FileBean fileBean);
}

用import方法实现接口..

@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<Cell> 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();
        }
    }

在 FileBean 中将用于 spring 上传的 bean 的配置..

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"/>
</bean>

文件 bean

public class FileBean {

  private CommonsMultipartFile fileData;

  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }

  public void setFileData(CommonsMultipartFile fileData)
  {
    this.fileData = fileData;
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2018-05-16
    • 2013-06-06
    相关资源
    最近更新 更多