Java 使用ApachePOI和Spring框架将Excel文件上载到数据库中
在互联网上找不到好的帖子来帮助我 我的要求是从电子表格中读取每一行,并使用单元格中的值生成一条sql语句,并在读取电子表格时进行批量上载 我正在使用ApachePOI、Spring框架和JDBC 如何从excel生成SQLJava 使用ApachePOI和Spring框架将Excel文件上载到数据库中,java,spring,apache,apache-poi,Java,Spring,Apache,Apache Poi,在互联网上找不到好的帖子来帮助我 我的要求是从电子表格中读取每一行,并使用单元格中的值生成一条sql语句,并在读取电子表格时进行批量上载 我正在使用ApachePOI、Spring框架和JDBC 如何从excel生成SQL 是否有一个带有args(?)的sql语句,并使用单元格内容格式化 或 通过连接单元格内容准备sql 做这件事的最好方法是什么???几周前我也打算做同样的事情,最后你的问题的excel部分得到了以下解决方案。此解决方案支持新的和97-2007图纸格式。我正在使用spring和P
做这件事的最好方法是什么???几周前我也打算做同样的事情,最后你的问题的excel部分得到了以下解决方案。此解决方案支持新的和97-2007图纸格式。我正在使用spring和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>
接口
public interface importService {
public void import(FileBean fileBean);
}
使用导入方法实现接口
@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();
}
}
}您的表格结构是什么?3列数字数据
@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();
}
}
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
public class FileBean {
private CommonsMultipartFile fileData;
public CommonsMultipartFile getFileData()
{
return fileData;
}
public void setFileData(CommonsMultipartFile fileData)
{
this.fileData = fileData;
}