环境:导入POI对应的包

环境:

Spring+SpringMVC+Mybatis

POI对应的包

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>

ExcelBean数据封装

ExcelBean.java:


/**
* Created by LT on 2017-08-23.
*/
public class ExcelBean implements java.io.Serializable{
private String headTextName; //列头(标题)名
private String propertyName; //对应字段名
private Integer cols; //合并单元格数
private XSSFCellStyle cellStyle;
public ExcelBean(){
}
public ExcelBean(String headTextName, String propertyName){
this.headTextName = headTextName;
this.propertyName = propertyName;
}
public ExcelBean(String headTextName, String propertyName, Integer cols) {
super();
this.headTextName = headTextName;
this.propertyName = propertyName;
this.cols = cols;
}
public String getHeadTextName() {
return headTextName;
} public void setHeadTextName(String headTextName) {
this.headTextName = headTextName;
} public String getPropertyName() {
return propertyName;
}
}

导入导出工具类

ExcelUtil.java

/**
* Created by LT on 2017-08-23.
*/
public class ExcelUtil {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
/**
* Excel导入
*/
public static List<List<Object>> getBankListByExcel(InputStream in, String fileName) throws Exception{
List<List<Object>> list = null;
//创建Excel工作薄
Workbook work = getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new ArrayList<List<Object>>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;}
//遍历当前sheet中的所有行
//包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
//读取一行
row = sheet.getRow(j);
//去掉空行和表头
if(row==null||row.getFirstCellNum()==j){continue;}
//遍历所有的列
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
li.add(getCellValue(cell));
}
list.add(li);
}
}
return list;
}
/**
* 描述:根据文件后缀,自适应上传文件的版本
*/
public static Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2003L.equals(fileType)){
wb = new HSSFWorkbook(inStr); //2003-
}else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}else{
throw new Exception("解析的文件格式有误!");
}
return wb;
}
/**
* 描述:对表格中数值进行格式化
*/
public static Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化字符类型的数字
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue());
}else{
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
}
/**
* 导入Excel表结束
* 导出Excel表开始
* @param sheetName 工作簿名称
* @param clazz 数据源model类型
* @param objs excel标题列以及对应model字段名
* @param map 标题列行数以及cell字体样式
*/
public static XSSFWorkbook createExcelFile(Class clazz, List objs, Map<Integer, List<ExcelBean>> map, String sheetName) throws
IllegalArgumentException,IllegalAccessException,InvocationTargetException,
ClassNotFoundException, IntrospectionException, ParseException {
// 创建新的Excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
XSSFSheet sheet = workbook.createSheet(sheetName);
// 以下为excel的字体样式以及excel的标题与内容的创建,下面会具体分析;
createFont(workbook); //字体样式
createTableHeader(sheet, map); //创建标题(头)
createTableRows(sheet, map, objs, clazz); //创建内容
return workbook;
}
private static XSSFCellStyle fontStyle;
private static XSSFCellStyle fontStyle2;
public static void createFont(XSSFWorkbook workbook) {
// 表头
fontStyle = workbook.createCellStyle();
XSSFFont font1 = workbook.createFont();
//font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font1.setFontName("黑体");
font1.setFontHeightInPoints((short) 11);// 设置字体大小
fontStyle.setFont(font1);
fontStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
fontStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
fontStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
fontStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
fontStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
// 内容
fontStyle2=workbook.createCellStyle();
XSSFFont font2 = workbook.createFont();
font2.setFontName("宋体");
font2.setFontHeightInPoints((short) 12);// 设置字体大小
fontStyle2.setFont(font2);
fontStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
fontStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
fontStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
fontStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
fontStyle2.setAlignment(XSSFCellStyle.ALIGN_RIGHT); // 居中
}
/**
* 根据ExcelMapping 生成列头(多行列头)
*
* @param sheet 工作簿
* @param map 每行每个单元格对应的列头信息
*/
public static final void createTableHeader(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map) {
int startIndex=0;//cell起始位置
int endIndex=0;//cell终止位置
for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
XSSFRow row = sheet.createRow(entry.getKey());
List<ExcelBean> excels = entry.getValue();
for (int x = 0; x < excels.size(); x++) {
//合并单元格
if(excels.get(x).getCols()>1){
if(x==0){
endIndex+=excels.get(x).getCols()-1;
CellRangeAddress range=new CellRangeAddress(0,0,startIndex,endIndex);
sheet.addMergedRegion(range);
startIndex+=excels.get(x).getCols();
}else{
endIndex+=excels.get(x).getCols();
CellRangeAddress range=new CellRangeAddress(0,0,startIndex,endIndex);
sheet.addMergedRegion(range);
startIndex+=excels.get(x).getCols();
}
XSSFCell cell = row.createCell(startIndex-excels.get(x).getCols());
cell.setCellValue(excels.get(x).getHeadTextName());// 设置内容
if (excels.get(x).getCellStyle() != null) {
cell.setCellStyle(excels.get(x).getCellStyle());// 设置格式
}
cell.setCellStyle(fontStyle);
}else{
XSSFCell cell = row.createCell(x);
cell.setCellValue(excels.get(x).getHeadTextName());// 设置内容
if (excels.get(x).getCellStyle() != null) {
cell.setCellStyle(excels.get(x).getCellStyle());// 设置格式
}
cell.setCellStyle(fontStyle);
}
}
}
}
public static void createTableRows(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map, List objs, Class clazz)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException,
ClassNotFoundException, ParseException {
int rowindex = map.size();
int maxKey = 0;
List<ExcelBean> ems = new ArrayList<ExcelBean>();
for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
if (entry.getKey() > maxKey) {
maxKey = entry.getKey();
}
}
ems = map.get(maxKey);
List<Integer> widths = new ArrayList<Integer>(ems.size());
for (Object obj : objs) {
XSSFRow row = sheet.createRow(rowindex);
for (int i = 0; i < ems.size(); i++) {
ExcelBean em = (ExcelBean) ems.get(i);
// 获得get方法
PropertyDescriptor pd = new PropertyDescriptor(em.getPropertyName(), clazz);
Method getMethod = pd.getReadMethod();
Object rtn = getMethod.invoke(obj);
String value = "";
// 如果是日期类型进行转换
if (rtn != null) {
if (rtn instanceof Date) {
value = DateUtils.dateToString((Date)rtn);
} else if(rtn instanceof BigDecimal){
NumberFormat nf = new DecimalFormat("#,##0.00");
value=nf.format((BigDecimal)rtn).toString();
} else if((rtn instanceof Integer) && (Integer.valueOf(rtn.toString())<0 )){
value="--";
}else {
value = rtn.toString();
}
}
XSSFCell cell = row.createCell(i);
cell.setCellValue(value);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(fontStyle2);
// 获得最大列宽
int width = value.getBytes().length * 300;
// 还未设置,设置当前
if (widths.size() <= i) {
widths.add(width);
continue;
}
// 比原来大,更新数据
if (width > widths.get(i)) {
widths.set(i, width);
}
}
rowindex++;
}
// 设置列宽
for (int index = 0; index < widths.size(); index++) {
Integer width = widths.get(index);
width = width < 2500 ? 2500 : width + 300;
width = width > 10000 ? 10000 + 300 : width + 300;
sheet.setColumnWidth(index, width);
}
}
}

Excel表导出

ExcelController.java

/**
* 上传excel并将内容导入数据库中
*
* @return
*/
@RequestMapping(value = "/import")
@Permission("login") public Object importExcel(MultipartFile file, HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
try {
if (request.getSession().getAttribute("userName") == null || request.getSession().getAttribute("userName").toString().isEmpty()) {
map.put("code", "20000");
map.put("mes", "请先登录再进行操作!!!");
return map;
}
System.out.println(file.getOriginalFilename());
InputStream in = file.getInputStream();
List<List<Object>> listob = ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
List<Inventory> inventoryList = new ArrayList<Inventory>();
String createBy = request.getSession().getAttribute("userName").toString();
//遍历listob数据,把数据放到List中
for (int i = 0; i < listob.size(); i++) {
List<Object> ob = listob.get(i);
Inventory inventory = new Inventory();
//通过遍历实现把每一列封装成一个model中,再把所有的model用List集合装载
inventory.setCompany(String.valueOf(ob.get(0)).trim());
inventory.setArea(String.valueOf(ob.get(1)).trim());
inventory.setWarehouse(String.valueOf(ob.get(2)).trim());
inventory.setWarehouseName(String.valueOf(ob.get(3)).trim());
inventory.setStoreAttributes(String.valueOf(ob.get(4)).trim());
inventory.setMaterialBig(String.valueOf(ob.get(5)).trim());
inventory.setMaterialMid(String.valueOf(ob.get(6)).trim());
inventory.setMaterialSmall(String.valueOf(ob.get(7)).trim());
inventory.setMaterialModel(String.valueOf(ob.get(8)).trim());
inventory.setMaterialCode(String.valueOf(ob.get(9)).trim());
inventory.setMaterialTips(String.valueOf(ob.get(10)).trim());
inventory.setServiceAttribute(String.valueOf(ob.get(11)).trim());
inventory.setPlanner(String.valueOf(ob.get(12)).trim());
inventory.setSales(String.valueOf(ob.get(13)).trim());
inventory.setEndingCount(String.valueOf(ob.get(14)).trim());
inventory.setTransferin(String.valueOf(ob.get(15)).trim());
inventory.setInventory(String.valueOf(ob.get(16)).trim());
inventory.setCreateTime(new Date());
inventory.setCreateBy(createBy);
inventoryList.add(inventory);
}
//批量插入
inventoryService.insertInfoBatch(inventoryList);
} catch (Exception e) {
LogUtil.error("ExcelController-----importExcel:" + e.getMessage());
map.put("code", "30000");
map.put("mes", "上传异常");
return map;
}
map.put("code", "10000");
map.put("mes", "上传成功");
map.put("url","user/crud");
LogUtil.info("ExcelController-----importExcel:" + map.toString());
return map;
}

Excel表导出

ExcelController.java

/**
* 将数据库中的数据导出为excel
*
* @return
*/
@RequestMapping("/output")
@Permission("login")
@ResponseBody public Object outputExcel(HttpServletRequest request, HttpServletResponse response) { response.reset(); //清除buffer缓存
Map<String, Object> map = new HashMap<String, Object>(), TempMap = new HashMap<String, Object>();
System.out.println("startDate:"+request.getParameter("startDate"));
System.out.println("endDate:"+request.getParameter("endDate"));
try {
if (request.getSession().getAttribute("userName") == null || request.getSession().getAttribute("userName").toString().isEmpty()) {
map.put("code", "20000");
map.put("mes", "请先登录再进行操作!!!");
return map;
}
String fileName = DateUtils.getCurrentDate() + "~";
if (request.getParameter("startDate") != null&& !"".equals(request.getParameter("startDate"))) {
TempMap.put("startDate", request.getParameter("startDate")); fileName = DateUtils.formatString(request.getParameter("startDate"))+ "~";
}
if (request.getParameter("endDate") != null&&!"".equals(request.getParameter("endDate"))) {
TempMap.put("endDate", request.getParameter("endDate"));
fileName =fileName+ DateUtils.formatString(request.getParameter("endDate"));
} else {
fileName = fileName + DateUtils.dateToString(new Date());
} // 指定下载的文件名
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
List<Inventory> list = inventoryService.getList(TempMap);
List<ExcelBean> excel = new ArrayList<ExcelBean>();
Map<Integer, List<ExcelBean>> mapExcel = new LinkedHashMap<Integer, List<ExcelBean>>();
XSSFWorkbook xssfWorkbook = null;
//设置标题栏
excel.add(new ExcelBean("行政公司", "company", 0));
excel.add(new ExcelBean("区域", "area", 0));
excel.add(new ExcelBean("门店-仓库", "warehouse", 0));
excel.add(new ExcelBean("门店-仓库名称", "warehouseName", 0));
excel.add(new ExcelBean("门店属性", "storeAttributes", 0));
excel.add(new ExcelBean("物料大类", "materialBig", 0));
excel.add(new ExcelBean("物料中类(手机制式)", "materialMid", 0));
excel.add(new ExcelBean("物料小类", "materialSmall", 0));
excel.add(new ExcelBean("物料型号", "materialModel", 0));
excel.add(new ExcelBean("物料编码", "materialCode", 0));
excel.add(new ExcelBean("物料说明", "materialTips", 0));
excel.add(new ExcelBean("业务属性", "serviceAttribute", 0));
excel.add(new ExcelBean("计划员", "planner", 0));
excel.add(new ExcelBean("销量", "sales", 0));
excel.add(new ExcelBean("期末数量", "endingCount", 0));
excel.add(new ExcelBean("调拨在途", "transferin", 0));
excel.add(new ExcelBean("库存", "inventory", 0));
mapExcel.put(0, excel);
String sheetName = fileName + "天翼库存表";
xssfWorkbook = ExcelUtil.createExcelFile(Inventory.class, list, mapExcel, sheetName);
OutputStream output;
try {
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
xssfWorkbook.write(bufferedOutPut);
bufferedOutPut.close();
} catch (IOException e) {
LogUtil.error("ExcelController-----outputExcel:" + e.getMessage());
e.printStackTrace();
map.put("code", "30000");
map.put("mes", "导出异常");
return map;
}
} catch (Exception e) {
LogUtil.error("ExcelController-----outputExcel:" + e.getMessage());
map.put("code", "30000");
map.put("mes", "导出异常");
return map;
}
map.put("code", "10000");
map.put("mes", "导出成功");
LogUtil.info("ExcelController-----outputExcel:" + map.toString());
return map;
}

mapper.xml配置

InventoryMapping.xml

<insert id="insertInfoBatch" parameterType="java.util.List">
insert into inventory (
company, area,warehouse, warehouseName, storeAttributes,materialBig,
materialMid, materialSmall, materialModel, materialCode, materialTips,serviceAttribute,
planner , sales, endingCount, transferin, inventory, createTime, createBy
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.company}, #{item.area}, #{item.warehouse},#{item.warehouseName}, #{item.storeAttributes}, #{item.materialBig},
#{item.materialMid},#{item.materialSmall}, #{item.materialModel},#{item.materialCode}, #{item.materialTips}, #{item.serviceAttribute},
#{item.planner}, #{item.sales}, #{item.endingCount},#{item.transferin}, #{item.inventory}, #{item.createTime}, #{item.createBy}
)
</foreach>
</insert>

为了方便所以 将service层的代码写到了Controller里了

项目效果图

SSM中使用POI实现excel的导入导出的更多相关文章

  1. poi实现excel的导入导出功能

    Java使用poi实现excel的导入导出功能: 工具类ExcelUtil,用于解析和初始化excel的数据:代码如下 package com.raycloud.kmmp.item.service.u ...

  2. POI实现excel的导入导出

    引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</arti ...

  3. java 中Excel的导入导出

    部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字  的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...

  4. java实现excel的导入导出(poi详解)[转]

    java实现excel的导入导出(poi详解) 博客分类: java技术 excel导出poijava  经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个 ...

  5. Access中一句查询代码实现Excel数据导入导出

    摘 要:用一句查询代码,写到vba中实现Excel数据导入导出,也可把引号中的SQL语句直接放到查询分析器中执行正 文: 导入数据(导入数据时第一行必须是字段名): DoCmd.RunSQL &quo ...

  6. JAVA对Excel的导入导出

    今天需要对比2个excel表的内容找出相同:由于要学的还很多上手很慢所以在这做个分享希望对初学的有帮助: 先是pom的配置: <dependency> <groupId>org ...

  7. Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)

    Excel导入导出工具(简单.好用且轻量级的海量Excel文件导入导出解决方案.) 置顶 2019-09-07 16:47:10 $9420 阅读数 261更多 分类专栏: java   版权声明:本 ...

  8. excel的导入导出的实现

    1.创建Book类,并编写set方法和get方法 package com.bean; public class Book { private int id; private String name; ...

  9. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

随机推荐

  1. python 3.x 学习笔记11 (静态、类、属性、特殊成员方法)

    1.静态方法通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法.静态方法是不可以访问实例变量或类变量的即没有self,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什 ...

  2. JavaScript学习——JS对象和全局函数

    1. Array对象 数组的特点:长度可变!数组的长度=最大角标+1 2.Boolean对象 如果value 不写,那么默认创建的结果为false 3.Date对象 getTime()返回1970年1 ...

  3. 解决maven 无法下载java-memcached的依赖问题

    1.进入https://github.com/gwhalin/Memcached-Java-Client/downloads 下载java-memcached的jar包. 2.使用cmd进入maven ...

  4. str 数据类型的用法

    ---------------------------------------------------------------------------------------------------- ...

  5. How Javascript works (Javascript工作原理) (二) 引擎,运行时,如何在 V8 引擎中书写最优代码的 5 条小技巧

    个人总结: 一个Javascript引擎由一个标准解释程序,或者即时编译器来实现. 解释器(Interpreter): 解释一行,执行一行. 编译器(Compiler): 全部编译成机器码,统一执行. ...

  6. Flux架构与Redux简介

    Flux架构区别于传统的MVC架构 在facebook实践中, 当用户接收到新消息时,右上角会弹出你有一条新消息, 右下角的对话框也会提示有新消息, 如果用户在对话框中查看了新消息,那么右上角的这个新 ...

  7. CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除

    题目链接:https://cn.vjudge.net/problem/CodeForces-920E 题意 给一个补图,问各个联通块有几个元素,升序排列 注意maxn=2e5, maxm=2e10 思 ...

  8. 51nod 麦克打电话(AC自动机+树状数组)

    SAM+线段树合并的裸题. 但我们讨论AC自动机的做法. 先建出AC自动机.考虑询问在[a,b]中出现的次数就是\([1,b]\)的出现次数-\([1,a-1]\)的出现次数.把询问离线.然后我们要求 ...

  9. Docker学习总结(8)——利用Docker开启持续交付之路

    持续交付即Continuous Delivery,简称CD,随着DevOps的流行正越来越被传统企业所重视.持续交付讲求以短周期.小细粒度,自动化的方式频繁的交付软件,在这个过 程中要求开发.测试.用 ...

  10. MySQL数据类型及后面小括号的意义

    1,数值类型 1.1数值类型的种类 标准 SQL 中的数值类型,包括严格数值类型(INTEGER.SMALLINT.DECIMAL.NUMERIC),以及近似数值数据类型(FLOAT.REAL.DOU ...