();
map.put("list", list);
map.put("breed", resourceAO.getMarketBreedJson());
map.put("city", resourceAO.getCityJsons());
map.put("exceed", exceed);
return new ModelAndView("/resource/publish/batch", "map", map);
}
AO层解析文件事例:
/**
* 解析excel并返回资源信息
*
* @param context
* @param file
* @param memberId
* @return
*/
@Override
public List
工具类excelUtil:
package com.banksteel.resource.util;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;
import com.banksteel.admin.entity.AdminContext;
/**
* 导入文件工具类
*
* @author Rick
* @time 2013-10-21上午9:13:20
*/
public class ExcelUtils
{
private static final String RESOURCE_HEAD_FIELD = "品名,规格,牌号,产地/品牌,交货地,城市,仓库,计重方式,件重,件数,价格,底价,捆包号(选填),备注,表面质量,质量标准,质保书,仓库类型";
public static final String RESOURCE_HEAD_BREED = "品名";
public static final String RESOURCE_HEAD_SPEC = "规格";
public static final String RESOURCE_HEAD_MATERIAL = "牌号";
public static final String RESOURCE_HEAD_BRAND = "产地/品牌";
public static final String RESOURCE_HEAD_CITY1 = "交货地";
public static final String RESOURCE_HEAD_CITY2 = "城市";
public static final String RESOURCE_HEAD_WAREHOUSE = "仓库";
public static final String RESOURCE_HEAD_WEIGHTWAY = "计重方式";
public static final String RESOURCE_HEAD_AVERAGEQTY = "件重";
public static final String RESOURCE_HEAD_INITEMS = "件数";
public static final String RESOURCE_HEAD_BASEPRICE = "底价";
public static final String RESOURCE_HEAD_NOTE = "备注";
public static final String RESOURCE_HEAD_QUALITY = "表面质量";
public static final String RESOURCE_HEAD_STANDARD = "质量标准";
public static final String RESOURCE_HEAD_WARRANTY = "质保书";
public static final String RESOURCE_HEAD_WAREHOUSETYPE = "仓库类型";
public static final String RESOURCE_HEAD_SERIALNO = "捆包号(选填)";
public static List> readExcel(File excel, AdminContext context) throws ExcelParseException
{
List> listMap = null;
try
{
Workbook workbook = null;
try
{
workbook = new XSSFWorkbook(excel.getPath());
}
catch (Exception e)
{
workbook = new HSSFWorkbook(new FileInputStream(excel));
}
for (int j = 0; j < 1; j++)
{
Sheet sheet = workbook.getSheetAt(j);
Row row;
Map rows;
if (sheet != null && sheet.getLastRowNum() > 0)
{
// 先获取sheet行数
int iRowCount = sheet.getLastRowNum() + 1;
// 获取整个表格的列数
int iColumns = 0;
for (int i = 0; i < iRowCount; i++)
{
if (sheet.getRow(i) != null)
{
int tempColumns = sheet.getRow(i).getLastCellNum();
if (tempColumns > iColumns)
{
iColumns = tempColumns;
}
}
}
Map m = new HashMap(iColumns);
int iRow = 0;
// 获取表头
for (iRow = 0; iRow < iRowCount; iRow++)
{
row = sheet.getRow(iRow);
for (int iCol = 0; iCol < iColumns; iCol++)
{
if (row != null)
{
String cv = getCellValue(row.getCell(iCol));
if (!"-".equals(cv) && !StringUtils.isTrimEmpty(cv))
{
String h = contain(cv.replace(" ", ""));
if (!StringUtils.isTrimEmpty(h))
{
m.put(iCol, h);
}
}
}
}
// 如果获取的m中没有品名等,则视为不是表头
if (ObjectUtils.notEmpty(m) && m.size() > 1)
{
boolean headTag = false;
for (Entry e : m.entrySet())
{
if ("品名".equals(e.getValue()))
{
headTag = true;
break;
}
}
if (headTag)
{
break;
}
}
else
{
m = new HashMap(iColumns);
}
}
// 判断是否取到表头
if (ObjectUtils.notEmpty(m))
{
// 判断数据是否为空
if (ObjectUtils.isEmpty(listMap))
{
listMap = new ArrayList>();
}
// 获取数据
for (int i = iRow + 1; i < iRowCount; i++)
{
row = sheet.getRow(i);
rows = new HashMap(iColumns);
for (int iCol = 0; iCol < iColumns; iCol++)
{
if (m.containsKey(iCol) && row != null)
{
rows.put(m.get(iCol), (getCellValue(row.getCell(iCol))).replace(" ", ""));
}
}
listMap.add(rows);
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
if (e instanceof ExcelParseException)
{
throw new ExcelParseException(e.getMessage());
}
else
{
throw new ExcelParseException("读取Excel文档发生错误");
}
}
return listMap;
}
private static String getCellValue(Cell cell)
{
if (cell == null)
{
return "";
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
{
return StringUtils.isTrimEmpty(cell.getStringCellValue()) ? "-" : cell.getStringCellValue();
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
{
return String.valueOf(cell.getNumericCellValue());
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK)
{
return "";
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN)
{
return String.valueOf(cell.getBooleanCellValue());
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA)
{
String sValue = "";
try
{
sValue = String.valueOf(cell.getNumericCellValue());
}
catch (Exception e)
{
sValue = cell.getStringCellValue();
}
return sValue;
}
else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR)
{
return String.valueOf(cell.getErrorCellValue());
}
else
{
return "";
}
}
/**
* 匹配表头信息
*
* @param cv
* @param headMap
* @return
*/
private static String contain(String cv)
{
String head = "";
if (RESOURCE_HEAD_FIELD.contains(cv))
{
head = cv;
if ("城市".equals(head))
{
head = "交货地";
}
if ("价格".equals(head))
{
head = "底价";
}
}
return head;
}
}
pom文件引入jar包:
org.apache.poi
poi-ooxml-schemas
3.7
jar
compile
导入直接解析文件事例:
@RequestMapping(value="/addStudents")
public ModelAndView getExample(HttpServletRequest request,HttpServletResponse response,MultipartFile uploadFile)
{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String id=request.getParameter("class");
Academy academy = academyService.getAcademyById(Integer.parseInt(id));
Map returnMap=new HashMap();
int count=0;
String message="";
try
{
Workbook wb = Workbook.getWorkbook(uploadFile.getInputStream());
Sheet st = wb.getSheet("Sheet1");
int row = st.getRows();
for(int i = 1;i {
Cell cell0 = st.getCell(0,i); //学号
Cell cell1 = st.getCell(1,i); //姓名
Cell cell2 = st.getCell(2,i); //手机
Cell cell3 = st.getCell(3,i); //邮箱
String sid = cell0.getContents();
String name = cell1.getContents();
String phone = cell2.getContents();
String email = cell3.getContents();
if(ObjectUtils.isNull(sid)) continue;
if(ObjectUtils.isNull(name)) continue;
if(ObjectUtils.isNull(phone)) continue;
if(ObjectUtils.isNull(email)) continue;
if(ObjectUtils.isNull(id))
{
returnMap.put("message","请检查操作步骤,班级一定要选!");
break;
}
User user=new User();
user.setSid(sid);
user.setName(name);
user.setPassword("111111");
user.setType(3);
user.setAcademyid(Integer.parseInt(id));
user.setPhone(phone);
user.setEmail(email);
user.setAcademyName(academy.getName());
if(userService.validate(user.getType(), name, user.getPassword()))
{
message=user.getName()+"已经存在,请检查导入表格!";
returnMap.put("message", message);
break;
}
userService.createOrUpdate(user);
count++;
}
if("".equals(message) || message.length() ==0)
{
message="共导入"+count+"条学生记录";
}
returnMap.put("message", message);
}
catch (BiffException | IOException e)
{
e.printStackTrace();
returnMap.put("message","导入学生失败,请联系管理员!");
}
returnMap.put("refererURL","/user/importStudents.jsp");
return new ModelAndView("message","map",returnMap);
}