@ResponseBody
@RequestMapping("downDataList")
public boolean downDataList(HttpServletRequest request,
HttpServletResponse response, PageUtil pageUtil,
MyTStoreVO myStoreVO, MyTSkuVO mySkuVO) {
List<TSku> sku = skuBO.find(mySkuVO);
if (sku.size() != 0) {
String[] skunoIn = new String[sku.size()];
for (int i = 0; i < sku.size(); i++) {
skunoIn[i] = sku.get(i).getSkuno();
}
myStoreVO.setSkunoIn(skunoIn);
List<TStore> list = storeBO.find(myStoreVO, pageUtil.getRows(),
pageUtil.getPage());
for (TStore u : list) {
for (TSku c : sku) {
if (u.getSkuno() != null && u.getSkuno() != ""
&& u.getSkuno().equals(c.getSkuno())) {
u.setSkuno(c.getSkuname());
}
}
if (Integer.parseInt(u.getCount()) <= 20) {
u.setCountWarning("库存不足");
}
}
final Collator collator = Collator.getInstance(Locale.CHINA);
Collections.sort(list, new Comparator<TStore>() {
@Override
public int compare(TStore o1, TStore o2) {
return collator.compare(o1.getSkuno(), o2.getSkuno());
}
});
for (TStore u : list) {
System.out.println(u.toString());
}
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wkb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wkb.createSheet("库存情况");
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell = row1.createCell(0);
// 设置单元格内容
cell.setCellValue("库存情况表" + MyDateUtil.getCurrentDate2());
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
HSSFRow row2 = sheet.createRow(1);
// 创建单元格并设置单元格内容
row2.createCell(0).setCellValue("SKU名称");
row2.createCell(1).setCellValue("入库数量");
row2.createCell(2).setCellValue("出库数量");
row2.createCell(3).setCellValue("库存量");
row2.createCell(4).setCellValue("库存状态");
row2.createCell(5).setCellValue("在售数量");
int i = 0;
for (TStore u : list) {
HSSFRow row = sheet.createRow(i + 2);
row.createCell(0).setCellValue(u.getSkuno());
row.createCell(1).setCellValue(u.getIncnt());
row.createCell(2).setCellValue(u.getOutcnt());
row.createCell(3).setCellValue(u.getCount());
row.createCell(4).setCellValue(u.getCountWarning());
row.createCell(5).setCellValue(u.getSalescnt());
i++;
}
// 输出Excel文件
try {
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=库存情况表"+ MyDateUtil.getCurrentDate2()+".xls");
response.setContentType("application/msexcel");
wkb.write(output);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else
return false;
}
设断点看了,全部执行,但是就是没下载表格的反应
记得,提问把报错信息 拿出来,不然谁知道你什么问题?
把@ResponseBody去掉
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=库存情况表"+ MyDateUtil.getCurrentDate2()+".xls");
response.setContentType("application/msexcel");
wkb.write(output);
你把你的这几行换成我的代码试试,自己改一下生成文档路径和名称
OutputStream out = new FileOutputStream("G:/导出文件测试/" + FILE_OUTPUT_NAME);
// 设置输出文档的头文件
FILE_OUTPUT_NAME = FileUtils.encodeDownloadFilename(FILE_OUTPUT_NAME, request.getHeader("user-agent"));
response.setContentType(request.getSession().getServletContext().getMimeType(FILE_OUTPUT_NAME));
response.setHeader("content-disposition", "attachment;filename=" + FILE_OUTPUT_NAME);
hssfWorkbook.write(out);
用接口的方式debug ,
第一步 测 response.getOutputStream().write("abc")
第二步 试试普通的文件下载
String path = this.getServletContext().getRealPath("c:\\abc.text");
String filename = path.substring(path.lastIndexOf("\\") + 1);
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
int len = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
while((len = in.read(buffer)) > 0) {
out.write(buffer,0,len);
}
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
if(in != null) {
try {
in.close();
}catch(Exception e) {
throw new RuntimeException(e);
}
}
}
第三步 考虑下 return 去掉。
......
我把我的方法贴下出来:
工具类:
public static InputStream getPoiExportExcel(String[][] data,
String[] title) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
putDataOnPoi(out, data, title);
InputStream stream = new ByteArrayInputStream(out.toByteArray());
try {
out.flush();
out.close();
} catch (Exception e) {
try {
stream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
e.printStackTrace();
}
return stream;
}
//poi导出excel
public static void putDataOnPoi(OutputStream os, String[][] data,
String[] title) {
SXSSFWorkbook wb = new SXSSFWorkbook();
// 创建工作表
Sheet sheet = null;
try {
sheet = wb.createSheet("共导出" + (data.length) + "条记录");
// 创建列名
// 定义格式, 字体, 下划线, 斜体, 粗体, 颜色
CellStyle sty = wb.createCellStyle();
sty.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 设置居中
// cellstyle.setWrapText(true);//自动换行
XSSFFont font = (XSSFFont) wb.createFont(); // 创建字体样式
// font.setFontName("Times New Roman"); //设置字体类型
font.setFontHeightInPoints((short) 9); // 设置字体大小
sty.setFont(font);
sty = (XSSFCellStyle) wb.createCellStyle();// 创建标题样式
sty.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 设置垂直居中
sty.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 设置水平居中
XSSFFont headerFont = (XSSFFont) wb.createFont(); // 创建字体样式
headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
// headerFont.setFontName("黑体"); //设置字体类型
headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
sty.setFont(headerFont); // 为标题样式设置字体样式
sty.setBorderBottom(XSSFCellStyle.BORDER_THIN);
sty.setBorderLeft(XSSFCellStyle.BORDER_THIN);
sty.setBorderRight(XSSFCellStyle.BORDER_THIN);
sty.setBorderTop(XSSFCellStyle.BORDER_THIN);
sty.setFillForegroundColor(HSSFColor.YELLOW.index);// 设置背景色
sty.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
Row row = sheet.createRow(0);
for (int i = 0; i < title.length; i++) {
row.createCell(i).setCellValue(title[i]);
}
for (int i = 0; i < (data.length); i++) {
row = sheet.createRow(i+1);
for (int j = 0; j < data[i].length; j++) {
row.createCell(j).setCellValue(data[i][j]);
}
}
// 添加数据
if (null != wb) {
// 下面开始添加单元格数据
// 从内存中写入文件中
wb.write(os);
}
} catch (Exception e) {
e.printStackTrace();
}
}
业务层:
@Override
public InputStream exportDailyRetainedUser(String ddate, String adminId,
String spId, String areaId,String cpFlag,String areaFlag) {
Map<String,Object> paramMap = new HashMap<String, Object>();
paramMap.put("ddate", ddate);
paramMap.put("adminId", adminId);
paramMap.put("spId", spId);
paramMap.put("areaId", areaId);
paramMap.put("cpFlag", cpFlag);
paramMap.put("areaFlag", areaFlag);
List<DailyRetainedUser> dailyRetainedUsers = monthUserDAO.findDailyRetainedUserList(paramMap);
String title[] = { };
String data[][] = new String [dailyRetainedUsers.size()][title.length];
for(int i = 0; i < dailyRetainedUsers.size(); i++){
DailyRetainedUser drUser = dailyRetainedUsers.get(i);
data[i][0] = i + 1 + "";
data[i][1] = String.valueOf(drUser.getDate());
}
return ExcelUtil.getPoiExportExcel(data, title);
}
HSSFWorkbook wkb = new HSSFWorkbook(new FileInputStream(new File(filePath)));