博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POI技术处理Excel表 .xls ..xlsx两种格式的导入操作
阅读量:4186 次
发布时间:2019-05-26

本文共 2729 字,大约阅读时间需要 9 分钟。

一、说明

1、文章转载自:
原文标题====SpringMvc+POI 处理Excel的导入操作功能====
提到了ImportExcelUtil.java(Excel解析工具类)、UploadExcelControl.java (Spring控制器)、InfoVo.java(保存Excel数据对应的对象)、main.jsp(前端代码)以及配置文件web.xml、springmvc-servlet.xml(只做简单配置)、applicationContext-base.xml等。
2、本文只提Controller层、ImportExcelUtil工具类两部分,原文中这两部分导入功能可能会有一些小问题,具体可看原文网友评论。
3、我对原文导入部分代码进行略微修改后,导入功能已实际运用当中,没发现问题。
二、功能代码
首先先感谢下原文博主,然后上代码!!!!!
1、Controller层 

// 单号信息service@Autowiredpublic OrderService orderService ;		//服务层改为自己的	/**	 * 一键上传Excel表信息	 * 	 * @author Justin	 * 	 */	@RequestMapping("order_add.action")	public @ResponseBody List
uploadadd(MultipartFile myFile, HttpServletResponse res) throws IOException { List
errorList = new ArrayList
(); try { ImportExcelUtil util = new ImportExcelUtil(); InputStream input = null; List
> lists = null; if(myFile.isEmpty()) { log.error("文件不存在!"); }else { if (errorList.size() == 0) { String fileName = myFile.getOriginalFilename(); input = myFile.getInputStream(); lists = util.getBankListByExcel(input, fileName); input.close(); //循环将excel中的数据存入库 for(int i=1; i
list = lists.get(i); Order order= new Order(); //实体类,改为自己的 order.setOrderNumber(util.getFormat(String.valueOf(list.get(0)))); order.setAddress(util.getFormat(String.valueOf(list.get(1)))); order.setPhone(util.getFormat(String.valueOf(list.get(2)))); orderService.add(order); } } } } catch (Exception e) { errorList.add("导入单号数据错误"); e.printStackTrace(); log.error("系统错误", e.fillInStackTrace()); } return errorList; }

2、ImportExcelUtil工具类

package poi;import java.io.InputStream;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ImportExcel {	private final static String Excel_2003 =".xls";//2003版本的excel	private final static String Excel_2007 =".xlsx";//2007版本的excel		public List
> getBankListByExcel(InputStream in,String fileName) throws Exception{ List
> list = null; //得到一个Excel工作薄 Workbook work = this.getWorkbook(in, fileName); if(work==null) { throw new Exception("Excel工作薄为空"); } Sheet sheet = null; Row row = null; Cell cell = null; list = new ArrayList
>(); //遍历Excel中所有的sheet for(int i=0;i
li = new ArrayList
(); //int totalColumn = row.getLastCellNum(); for(int y=row.getFirstCellNum();y

 

你可能感兴趣的文章
JSP中常见TOMCAT错误代码原因
查看>>
MyEclipse中WEB项目加载mysql驱动方法
查看>>
常见编写JAVA报错总结
查看>>
org.gjt.mm.mysql.Driver和com.mysql.jdbc.Driver的区别
查看>>
UTF-8和GBK有什么区别
查看>>
增加MyEclipse分配内存的方法
查看>>
头痛与早餐
查看>>
[转]在ASP.NET 2.0中操作数据::创建一个数据访问层
查看>>
Linux命令之chmod详解
查看>>
【java小程序实战】小程序注销功能实现
查看>>
leetcode Unique Paths II
查看>>
几个大数据的问题
查看>>
CareerCup Pots of gold game:看谁拿的钱多
查看>>
CarreerCup Sort Height
查看>>
CareerCup Sort an array in a special way
查看>>
CareerCup Find lexicographic minimum in a circular array 循环数组字典序
查看>>
CareerCup Cost of cutting wood
查看>>
Find the number of subsets such that the sum of numbers in the subset is a prime number
查看>>
CareerCup Binary Tree the Maximum of 人
查看>>
CareerCup Divide n cakes to k different people
查看>>