如何使用 jxls 上传和更新集合 DB .xls 或 .xlsx 文件
How to upload and update set DB .xls or .xlsx files using jxls
我用过gxt2.5,java1.7,oracle 11g,MyBatis。
我想使用 jxsl 上传和更新到数据库中。
excel.xsl 或 excel.xlsx
http://i.stack.imgur.com/Bk5As.jpg
mapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<workbook>
<worksheet name="Sheet1">
<loop startRow="3" endRow="3" items="listFile" var="listFile"
varType="com.app.test.server.vo.listVO"
<section startRow="3" endRow="3">
<mapping row="3"col="0">listFile.a</mapping>
<mapping row="3" col="1">listFile.b</mapping>
<mapping row="3" col="2">listFile.c</mapping>
<mapping row="3" col="3">listFile.d</mapping>
<mapping row="3" col="4">listFile.e</mapping>
<mapping row="3" col="5">listFile.f</mapping>
<mapping row="3" col="6">listFile.g</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0">listFile.b</cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
我不知道循环中断条件。
我想在 listFile.b 为空时中断。因为,listFile.b是PK。
controller.java
private ClassPathResource uploadFileTemplate = new ClassPathResource("mapping.xml", getClass());
@RequestMapping(value = "/imp/XlsListImport.do")
public String handleFormUpload(@RequestParam("file") MultipartFile file,
@ModelAttribute("listVO") listVO listVo,
Model model, final HttpServletRequest request) throws InvalidFormatException, IOException,
SAXException {
listVo = new listVO();
model.addAttribute(listVo);
if (listExcelService == null) {
WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext(request.getSession().getServletContext());
listExcelService = (ListExcelService) wac.getBean("ListExcelService" );
}
final MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
final Map<String, MultipartFile> files = multiRequest.getFileMap();
InputStream fis = null;
String sResult = "";
Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
while (itr.hasNext()) {
Entry<String, MultipartFile> entry = itr.next();
file = entry.getValue();
if (!"".equals(file.getOriginalFilename())) {
if (file.getOriginalFilename().endsWith(".xls")
|| file.getOriginalFilename().endsWith(".XLS") ) {
try {
fis = file.getInputStream();
excelService.updateXls(fis);
sResult = "success";
} catch(Exception e) {
e.printStackTrace();
sResult = "error";
} finally {
if (fis != null)
fis.close();
}
} else if ( file.getOriginalFilename().endsWith(".xlsx")
|| file.getOriginalFilename().endsWith(".XLSX")) {
try {
fis = file.getInputStream();
excelService.updateXls(fis);
sResult = "success";
} catch(Exception e) {
e.printStackTrace();
sResult = "error";
} finally {
if (fis != null)
fis.close();
}
}else{
logger.info(" Only xls, xlsx... check file extension");
sResult = "success";
}
}
}
listExcelRead.read(uploadFileTemplate, file, model);
System.out.println(listVo);
System.out.println(listVo.getListVo());
return sResult;
}
listExcelRead.java
public XLSReadStatus read(Resource templateFile, MultipartFile file,
Model model) throws InvalidFormatException, IOException, SAXException {
return read(templateFile, file, model.asMap());
}
public XLSReadStatus read(Resource templateFile, MultipartFile file,
Map<?, ?> model) throws InvalidFormatException, IOException, SAXException {
InputStream inputXML = null;
InputStream inputXLS = null;
try {
inputXML = new BufferedInputStream(templateFile.getInputStream());
XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
inputXLS = new BufferedInputStream(file.getInputStream());
return mainReader.read(inputXLS, model);
} finally {
IOUtils.closeQuietly(inputXML);
IOUtils.closeQuietly(inputXLS);
}
}
尝试使用以下循环中断条件
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="1"/>
</rowcheck>
</loopbreakcondition>
因此,您指示检查 listFile.b 字段所在的 B 列(偏移量=1)中的单元格,并在该单元格中的值为空时中断循环。
我用过gxt2.5,java1.7,oracle 11g,MyBatis。 我想使用 jxsl 上传和更新到数据库中。
excel.xsl 或 excel.xlsx http://i.stack.imgur.com/Bk5As.jpg
mapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<workbook>
<worksheet name="Sheet1">
<loop startRow="3" endRow="3" items="listFile" var="listFile"
varType="com.app.test.server.vo.listVO"
<section startRow="3" endRow="3">
<mapping row="3"col="0">listFile.a</mapping>
<mapping row="3" col="1">listFile.b</mapping>
<mapping row="3" col="2">listFile.c</mapping>
<mapping row="3" col="3">listFile.d</mapping>
<mapping row="3" col="4">listFile.e</mapping>
<mapping row="3" col="5">listFile.f</mapping>
<mapping row="3" col="6">listFile.g</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0">listFile.b</cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
我不知道循环中断条件。 我想在 listFile.b 为空时中断。因为,listFile.b是PK。
controller.java
private ClassPathResource uploadFileTemplate = new ClassPathResource("mapping.xml", getClass());
@RequestMapping(value = "/imp/XlsListImport.do")
public String handleFormUpload(@RequestParam("file") MultipartFile file,
@ModelAttribute("listVO") listVO listVo,
Model model, final HttpServletRequest request) throws InvalidFormatException, IOException,
SAXException {
listVo = new listVO();
model.addAttribute(listVo);
if (listExcelService == null) {
WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext(request.getSession().getServletContext());
listExcelService = (ListExcelService) wac.getBean("ListExcelService" );
}
final MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
final Map<String, MultipartFile> files = multiRequest.getFileMap();
InputStream fis = null;
String sResult = "";
Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
while (itr.hasNext()) {
Entry<String, MultipartFile> entry = itr.next();
file = entry.getValue();
if (!"".equals(file.getOriginalFilename())) {
if (file.getOriginalFilename().endsWith(".xls")
|| file.getOriginalFilename().endsWith(".XLS") ) {
try {
fis = file.getInputStream();
excelService.updateXls(fis);
sResult = "success";
} catch(Exception e) {
e.printStackTrace();
sResult = "error";
} finally {
if (fis != null)
fis.close();
}
} else if ( file.getOriginalFilename().endsWith(".xlsx")
|| file.getOriginalFilename().endsWith(".XLSX")) {
try {
fis = file.getInputStream();
excelService.updateXls(fis);
sResult = "success";
} catch(Exception e) {
e.printStackTrace();
sResult = "error";
} finally {
if (fis != null)
fis.close();
}
}else{
logger.info(" Only xls, xlsx... check file extension");
sResult = "success";
}
}
}
listExcelRead.read(uploadFileTemplate, file, model);
System.out.println(listVo);
System.out.println(listVo.getListVo());
return sResult;
}
listExcelRead.java
public XLSReadStatus read(Resource templateFile, MultipartFile file,
Model model) throws InvalidFormatException, IOException, SAXException {
return read(templateFile, file, model.asMap());
}
public XLSReadStatus read(Resource templateFile, MultipartFile file,
Map<?, ?> model) throws InvalidFormatException, IOException, SAXException {
InputStream inputXML = null;
InputStream inputXLS = null;
try {
inputXML = new BufferedInputStream(templateFile.getInputStream());
XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
inputXLS = new BufferedInputStream(file.getInputStream());
return mainReader.read(inputXLS, model);
} finally {
IOUtils.closeQuietly(inputXML);
IOUtils.closeQuietly(inputXLS);
}
}
尝试使用以下循环中断条件
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="1"/>
</rowcheck>
</loopbreakcondition>
因此,您指示检查 listFile.b 字段所在的 B 列(偏移量=1)中的单元格,并在该单元格中的值为空时中断循环。