springMVC 读取excel内容并写入数据库
2015-03-11 16:35
369 查看
1,jsp页面
<form id="fileUpload" action="${path}/break/uploadGoodsStore.jhtml" enctype="multipart/form-data" method="post">
<input id="excelFile" name="file" type="file"/>
<input type="button" value="提交" onclick="submitExcel()"
style="background:#faaa70; padding:2px 8px; color:white;margin-top:10px; text-align:center; border-radius:2px;border:1px #fa7b1e solid;"
/>
</form>
2,js代码
<script>
function submitExcel(){
var excelFile = $("#excelFile").val();
if(excelFile=='') {alert("请选择需上传的文件!");return false;}
if(excelFile.indexOf('.xls')==-1){alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)!");return false;}
$("#fileUpload").submit();
}
</script>
3,后台代码
@RequestMapping("uploadGoodsStore.jhtml")
public String uploadGoodsStore( @RequestParam MultipartFile file,HttpServletRequest request) throws BiffException, IOException{
logger.info("进入上传excel方法【uploadGoodsStore】");
jxl.Workbook excel = null;
// 创建一个excel文件对象
try {
excel = jxl.Workbook.getWorkbook(file.getInputStream());
// 获得第一个excel文件的sheet
jxl.Sheet sheet = excel.getSheet(0);
// 获取sheet单元的总行数
int rows = sheet.getRows();
// 获得sheet单元的总列数
int columns = sheet.getColumns();
List<GoodsStoreVo> goodsStroeErrorList=new ArrayList<GoodsStoreVo>();
List<GoodsStoreVo> goodsStroeRightList=new ArrayList<GoodsStoreVo>();
// 读取数据
for (int r = 2; r < rows; r++) {
// 每行数据
GoodsStoreVo goodsStoreVo=new GoodsStoreVo();
//for (int c =0; c < columns; c++) {
// jxl.Cell cell = sheet.getCell(c, r);
// String cellValue = cell.getContents();
// System.out.println(cellValue);
//}
String productCode=sheet.getCell(0,r).getContents();
String productName=sheet.getCell(1,r).getContents();
String unit=sheet.getCell(2,r).getContents();
String specifications=sheet.getCell(3,r).getContents();
String brand=sheet.getCell(4,r).getContents();
int amount=0;
if(StringUtils.isNotEmpty(sheet.getCell(5,r).getContents())){
amount=Integer.parseInt(sheet.getCell(5,r).getContents());
}
goodsStoreVo.setProductCode(productCode);
goodsStoreVo.setProductName(productName);
goodsStoreVo.setUnit(unit);
goodsStoreVo.setSpecifications(specifications);
goodsStoreVo.setBrand(brand);
goodsStoreVo.setAmount(amount);
goodsStoreVo.setLineNumber(r+1);
if(StringUtils.isNotEmpty(sheet.getCell(6,r).getContents())){
goodsStoreVo.setPrice(new BigDecimal(sheet.getCell(6,r).getContents()));
}else{
goodsStoreVo.setPrice(null);
}
goodsStoreVo.setStoreLoc(sheet.getCell(7,r).getContents());
goodsStoreVo.setRemark(sheet.getCell(8,r).getContents());
if(StringUtils.isEmpty(productCode)||StringUtils.isEmpty(productName)||StringUtils.isEmpty(unit)||StringUtils.isEmpty(specifications)||StringUtils.isEmpty(brand)||amount==0){
goodsStroeErrorList.add(goodsStoreVo);
}else{
goodsStroeRightList.add(goodsStoreVo);
}
}
// 关闭excel对象
excel.close();
if(goodsStroeErrorList.size()>0){
request.setAttribute("goodsStroeErrorList", goodsStroeErrorList);
return "goods/outInfo/goodsStoreTemplateImport";
}else {
for(int i=0;i<goodsStroeRightList.size();i++){
GoodsStoreVo goodsStoreVo=goodsStroeRightList.get(i);
goodsStoreVo.setProjectteamId(getCurrentUser(request).getCurrentPtid());
int m=overflowinfoService.updateGoodByImport(goodsStoreVo);
if(m==0){
goodsStoreVo.setGoodId(IdGenerator.genUUid("g"));
goodsStoreVo.setCreateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setCreateTime(new Date());
goodsStoreVo.setUpdateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setUpdateTime(new Date());
goodsService.addGoods(goodsStoreVo);
}
}
request.getSession().setAttribute("suc","保存数据成功!");
return "redirect:../jsp/goods/outInfo/goodsStoreTemplateImport.jsp";
}
} catch (Exception e) {
e.printStackTrace();
}
return "goods/outInfo/goodsStoreTemplateImport";
}
<form id="fileUpload" action="${path}/break/uploadGoodsStore.jhtml" enctype="multipart/form-data" method="post">
<input id="excelFile" name="file" type="file"/>
<input type="button" value="提交" onclick="submitExcel()"
style="background:#faaa70; padding:2px 8px; color:white;margin-top:10px; text-align:center; border-radius:2px;border:1px #fa7b1e solid;"
/>
</form>
2,js代码
<script>
function submitExcel(){
var excelFile = $("#excelFile").val();
if(excelFile=='') {alert("请选择需上传的文件!");return false;}
if(excelFile.indexOf('.xls')==-1){alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)!");return false;}
$("#fileUpload").submit();
}
</script>
3,后台代码
@RequestMapping("uploadGoodsStore.jhtml")
public String uploadGoodsStore( @RequestParam MultipartFile file,HttpServletRequest request) throws BiffException, IOException{
logger.info("进入上传excel方法【uploadGoodsStore】");
jxl.Workbook excel = null;
// 创建一个excel文件对象
try {
excel = jxl.Workbook.getWorkbook(file.getInputStream());
// 获得第一个excel文件的sheet
jxl.Sheet sheet = excel.getSheet(0);
// 获取sheet单元的总行数
int rows = sheet.getRows();
// 获得sheet单元的总列数
int columns = sheet.getColumns();
List<GoodsStoreVo> goodsStroeErrorList=new ArrayList<GoodsStoreVo>();
List<GoodsStoreVo> goodsStroeRightList=new ArrayList<GoodsStoreVo>();
// 读取数据
for (int r = 2; r < rows; r++) {
// 每行数据
GoodsStoreVo goodsStoreVo=new GoodsStoreVo();
//for (int c =0; c < columns; c++) {
// jxl.Cell cell = sheet.getCell(c, r);
// String cellValue = cell.getContents();
// System.out.println(cellValue);
//}
String productCode=sheet.getCell(0,r).getContents();
String productName=sheet.getCell(1,r).getContents();
String unit=sheet.getCell(2,r).getContents();
String specifications=sheet.getCell(3,r).getContents();
String brand=sheet.getCell(4,r).getContents();
int amount=0;
if(StringUtils.isNotEmpty(sheet.getCell(5,r).getContents())){
amount=Integer.parseInt(sheet.getCell(5,r).getContents());
}
goodsStoreVo.setProductCode(productCode);
goodsStoreVo.setProductName(productName);
goodsStoreVo.setUnit(unit);
goodsStoreVo.setSpecifications(specifications);
goodsStoreVo.setBrand(brand);
goodsStoreVo.setAmount(amount);
goodsStoreVo.setLineNumber(r+1);
if(StringUtils.isNotEmpty(sheet.getCell(6,r).getContents())){
goodsStoreVo.setPrice(new BigDecimal(sheet.getCell(6,r).getContents()));
}else{
goodsStoreVo.setPrice(null);
}
goodsStoreVo.setStoreLoc(sheet.getCell(7,r).getContents());
goodsStoreVo.setRemark(sheet.getCell(8,r).getContents());
if(StringUtils.isEmpty(productCode)||StringUtils.isEmpty(productName)||StringUtils.isEmpty(unit)||StringUtils.isEmpty(specifications)||StringUtils.isEmpty(brand)||amount==0){
goodsStroeErrorList.add(goodsStoreVo);
}else{
goodsStroeRightList.add(goodsStoreVo);
}
}
// 关闭excel对象
excel.close();
if(goodsStroeErrorList.size()>0){
request.setAttribute("goodsStroeErrorList", goodsStroeErrorList);
return "goods/outInfo/goodsStoreTemplateImport";
}else {
for(int i=0;i<goodsStroeRightList.size();i++){
GoodsStoreVo goodsStoreVo=goodsStroeRightList.get(i);
goodsStoreVo.setProjectteamId(getCurrentUser(request).getCurrentPtid());
int m=overflowinfoService.updateGoodByImport(goodsStoreVo);
if(m==0){
goodsStoreVo.setGoodId(IdGenerator.genUUid("g"));
goodsStoreVo.setCreateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setCreateTime(new Date());
goodsStoreVo.setUpdateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setUpdateTime(new Date());
goodsService.addGoods(goodsStoreVo);
}
}
request.getSession().setAttribute("suc","保存数据成功!");
return "redirect:../jsp/goods/outInfo/goodsStoreTemplateImport.jsp";
}
} catch (Exception e) {
e.printStackTrace();
}
return "goods/outInfo/goodsStoreTemplateImport";
}
相关文章推荐
- java SpringMVC中 POI读取数据库数据并写入Excel表格中,并实现下载功能
- yii2从excel文件中读取表格内容,并批量写入数据库
- 【原创】上传EXCEL并把内容批量写入指定数据库
- java连接Excel数据库读取,写入,操纵Excel表格
- java 对excel操作 读取、写入、修改数据;导出数据库数据到excel
- 将Excel文件内容写入到数据库-温柔一刀
- phpexcel 读取含多个sheet的excel,将从数据库中检索出的数据分别写入该excel不同的sheet中 并导出该excel
- [GridView]读取本地EXCEL 并写入数据库
- PHP读取Excel文件的内容并写入Mysql数据库
- php excel reader读取excel内容存入数据库实现代码
- php管理后台内容写入数据库,前台数据读取于数据库
- php-excel-reader读取excel内容存入数据库
- 将EXCEL的数据读取出来写入数据库的实例方法
- java读取文件中的内容写入excel中
- Java读取Excel文件并写入数据库
- 将Excel上传到服务器,读取Excel数据写入到数据库
- 从数据库读取数据写入excel并调用Winrar加密压缩输出
- 将数据库中读取的文件写入Excel文件
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- VBS 读取excel内容并写入到文本文件