首先要导入spring相关包,poi,和fileupload包,我是使用maven构建的。
一.导入excel
@RequestMapping(value = "/importBrandSort", method = RequestMethod.POST)
public ModelAndView importBrandSort(@RequestParam("filename") MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws Exception {
/*String temp = request.getSession().getServletContext()
.getRealPath(File.separator)
+ "temp"; // 临时目录
File tempFile = new File(temp);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(10 * 1024 * 1024); // 设置允许用户上传文件大小,单位:位
fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:位
fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
// 开始读取上传信息
// int index = 0;
List fileItems = null;
try {
fileItems = fu.parseRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件
FileItem fileItem = null;
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
fileItem = item;
// index++;
}
}
if (fileItem == null)
return null;
*/
if (file == null)
return null;
logger.info(file.getOriginalFilename());
String name = file.getOriginalFilename();// 获取上传文件名,包括路径
//name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
long size = file.getSize();
if ((name == null || name.equals("")) && size == 0)
return null;
InputStream in = file.getInputStream();
int count = brandService.importBrandPeriodSort(in);
// 改为人工刷新缓存KeyContextManager.clearPeriodCacheData(new
// PeriodDimensions());// 清理所有缓存
//int count = BrandMobileInfos.size();
String strAlertMsg ="";
strAlertMsg= "成功更新" + count + "条!";
logger.info(strAlertMsg);
//request.setAttribute("brandPeriodSortList", BrandMobileInfos);
//request.setAttribute("strAlertMsg", strAlertMsg);
request.getSession().setAttribute("msg",strAlertMsg);
return get(request, response);
//return null;
}
InputStream in = file.getInputStream();
List BrandMobileInfos = brandService
.importBrandPeriodSort(in);
读取excel的信息。
@Override
public List importBrandPeriodSort(InputStream in) throws Exception {
List brandMobileInfos = readBrandPeriodSorXls(in);
for (BrandMobileInfoEntity brandMobileInfo : brandMobileInfos) {
mapper.updateByConditions(brandMobileInfo);
}
return brandMobileInfos;
}
private List readBrandPeriodSorXls(InputStream is)
throws IOException, ParseException {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
List brandMobileInfos = new ArrayList();
BrandMobileInfoEntity brandMobileInfo;
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
brandMobileInfo = new BrandMobileInfoEntity();
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
for (int i = 0; i < hssfRow.getLastCellNum(); i++) {
HSSFCell brandIdHSSFCell = hssfRow.getCell(i);
if (i == 0) {
brandMobileInfo.setBrandId(Integer
.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 1) {
continue;
} else if (i == 2) {
brandMobileInfo.setMobileShowFrom(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 3) {
brandMobileInfo.setMobileShowTo(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 4) {
brandMobileInfo.setSellMarkValue(getCellValue(brandIdHSSFCell));
} else if (i == 5) {
brandMobileInfo.setWarehouse(getCellValue(brandIdHSSFCell));
} else if (i == 6) {
brandMobileInfo.setSortA1(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 7) {
brandMobileInfo.setSortA2(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 8) {
brandMobileInfo.setSortB(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 9) {
brandMobileInfo.setSortC10(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 10) {
brandMobileInfo.setSortC(Integer.parseInt(getCellValue(brandIdHSSFCell)));
} else if (i == 11) {
brandMobileInfo.setHitA(getCellValue(brandIdHSSFCell));
} else if (i == 12) {
brandMobileInfo.setHitB(getCellValue(brandIdHSSFCell));
} else if (i == 13) {
brandMobileInfo.setHitC(getCellValue(brandIdHSSFCell));
} else if (i == 14) {
brandMobileInfo.setCustomSellType(getCellValue(brandIdHSSFCell));
}else if (i == 15) {
continue;
}else if (i == 16) {
brandMobileInfo.setChannelId(Integer.parseInt(getCellValue(brandIdHSSFCell)));
}
}
brandMobileInfos.add(brandMobileInfo);
}
}
return brandMobileInfos;
}
$(".exportBrandSort").on('click', function() {
var url = contextPath+"/brand/exportBrandSort";
$('#searchform').attr('action', url);
$('#searchform').submit();
//还原action值
url = contextPath+"/brand/getBrand";
$('#searchform').attr('action', url);}
@RequestMapping(value = "/exportBrandSort", method = RequestMethod.GET)
public void exportBrandSort(HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
Map params = new HashMap<>();
// start time of selling
String startTimeStr = RequestUtil.getStringParameter(request,
"startTimeStr", null);
if (StringUtils.isNotBlank(startTimeStr)) {
params.put("startTimeStr", startTimeStr);
params.put("startTime", df.parse(startTimeStr).getTime() / 1000);
}
// end time of selling
String endTimeStr = RequestUtil.getStringParameter(request,
"endTimeStr", null);
if (StringUtils.isNotBlank(endTimeStr)) {
params.put("endTimeStr", endTimeStr);
params.put("endTime", df.parse(endTimeStr).getTime() / 1000);
}
// warehouse
String warehouse = RequestUtil.getStringParameter(request,
"warehouse");
if (StringUtils.isNotBlank(warehouse)) {
params.put("warehouse", warehouse);
}
// channel
String channel4ui = BrandConstants.CHANNEL_ID_SEARCH_DEFAULT;
String[] channel = request.getParameterValues("channel");
if (channel != null && channel.length > 0) {
channel4ui = stringArrayToString(channel);
}
params.put("channel", channel4ui);
String orderType = request.getParameter("orderType");
if (orderType == null || "".equals(orderType)) {
orderType = "C";
}
params.put("orderType", orderType);
// brand id
if (RequestUtil.getIntParameter(request, "brandId") > 0)
params.put("brandId",
(RequestUtil.getIntParameter(request, "brandId")));
// brand name
if (RequestUtil.getStringParameter(request, "brandName") != null
&& !"".equals(RequestUtil.getStringParameter(request,
"brandName").trim()))
// params.put("brandName", new
// String(RequestUtil.getStringParameter(request,
// "brandName").getBytes("ISO-8859-1"),"UTF-8"));
params.put("brandName",
RequestUtil.getStringParameter(request, "brandName"));
int count = brandService.countByConditions(params);
List list = brandService
.queryBrands(params);
// --------
byte[] fileNameByte = ("kxw.xls").getBytes("GBK");
String filename = new String(fileNameByte, "ISO8859-1");
byte[] bytes = brandService.exportBrandPeriodSort(list);
// logger.info("------------------------"+bytes.length);
response.setContentType("application/x-msdownload");
//response.setContentType("application/x-excel");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment;filename="
+ filename);
response.getOutputStream().write(bytes);
//response.getOutputStream().flush();
} catch (Exception ex) {
logger.debug(ex.getMessage());
}
}
byte[] fileNameByte = ("档期列表.xls").getBytes("GBK");
String filename = new String(fileNameByte, "ISO8859-1");
byte[] bytes = brandService.exportBrandPeriodSort(list);
若乱码:
response.setHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode("档期列表.xls", "UTF-8") );
response.getOutputStream().write(bytes);
@Override
public byte[] exportBrandPeriodSort(List list) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("档期排序表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//设置表头
List excelHead = getExcelHead();
HSSFCell cell = null;
// excel头
for (int i = 0; i < excelHead.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(excelHead.get(i));
cell.setCellStyle(style);
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到
//List list = getBrandPeriodSortDynamicOrder(entity, orderType);
BrandCompleteInfoEntity brandCompleteInfo = null; // 拼装excel内容
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
brandCompleteInfo = list.get(i);
// 创建单元格,并设置值
int j=0;
insertCell(row, j++, brandCompleteInfo.getBrandId());
insertCell(row, j++, brandCompleteInfo.getBrandName());
insertCell(row, j++, brandCompleteInfo.getMobileShowFrom());
insertCell(row, j++, brandCompleteInfo.getMobileShowTo());
insertCell(row, j++, brandCompleteInfo.getSellMarkValue());
insertCell(row, j++, brandCompleteInfo.getWarehouse());
insertCell(row, j++, brandCompleteInfo.getSortA1());
insertCell(row, j++, brandCompleteInfo.getSortA2());
insertCell(row, j++, brandCompleteInfo.getSortB());
insertCell(row, j++, brandCompleteInfo.getSortC10());
insertCell(row, j++, brandCompleteInfo.getSortC());
insertCell(row, j++, brandCompleteInfo.getHitA());
insertCell(row, j++, brandCompleteInfo.getHitB());
insertCell(row, j++, brandCompleteInfo.getHitC());
insertCell(row, j++, brandCompleteInfo.getCustomSellType());
insertCell(row, j++, channelInfoMapper.loadChannelNameById(brandCompleteInfo.getChannelId()));
insertCell(row, j++, brandCompleteInfo.getChannelId());
}
wb.write(out);
return out.toByteArray();
}
/**
* 获取excel表头
*
* @return
*/
private List getExcelHead() {
List result = new ArrayList(17);
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
result.add("XXXXX");
//。。。。
return result;
}
private void insertCell(HSSFRow row,int i,Object object){
if(object==null){
row.createCell(i).setCellValue("");
}else{
row.createCell(i).setCellValue(object.toString());
}
}