首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache和Spring框架将Excel文件上载到数据库

使用Apache和Spring框架将Excel文件上载到数据库
EN

Stack Overflow用户
提问于 2013-02-06 23:15:31
回答 1查看 30.9K关注 0票数 2

在网上找不到一个很好的帖子来帮我这个忙。

我的要求是从电子表格中读取每一行,生成带有单元格值的sql语句,并在读取电子表格时进行批量上载。

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

如何从excel生成sql?

  1. 有一个带有args (?)的sql语句用单元格内容格式化?

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

做这个最好的方法是什么??

EN

回答 1

Stack Overflow用户

发布于 2013-06-25 11:39:52

几周前,我也打算做同样的事情,最后得到了你问题中excel部分的解决方案。此解决方案支持新的和97-2007页格式。我用的是弹簧和POI。我认为在没有更多信息的情况下回答剩下的问题是不可能的。

用户上传文件的jsp站点:

代码语言:javascript
复制
<%@ 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>

在提交时将触发的控制器。

代码语言:javascript
复制
@Controller
@RequestMapping("/upload")
public class ExcelImporterController {

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

        return "import/importDone";
    }

}

接口.。

代码语言:javascript
复制
public interface importService {

    public void import(FileBean fileBean);
}

用导入方法实现接口。

代码语言:javascript
复制
@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中春季上传的bean的配置。

代码语言:javascript
复制
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"/>
</bean>

文件bean

代码语言:javascript
复制
public class FileBean {

  private CommonsMultipartFile fileData;

  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }

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

}

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14740727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档