java下载本地目录excel_java写简单Excel 首行是目录 然后前台下载

页面:

表格下载:

@RequestMapping("/download")

public void download(HttpServletRequest request, HttpServletResponse response, HttpSession session)

throws Exception {

ArrayList listTitle = new ArrayList();

listTitle.add("a");

listTitle.add("b");

listTitle.add("c");

listTitle.add("d");

listTitle.add("e");

// 创建表格及其第一行标题栏

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet("sheet1");

HSSFRow row0 = sheet.createRow(0);

for (int j = 0; j < 5; j++) {// 5列

HSSFCell cell = row0.createCell((short) j);

cell.setCellValue(listTitle.get(j).toString());

}

String maker = SystemUtil.getCureenUser(session);

List list = transportService.getlist(maker);

System.out.println(list.size());

// 从第二行输入

for (int i = 1; i <= list.size(); i++) { // 几行

HSSFRow row = sheet.createRow(i);

HSSFCell cell0 = row.createCell((short) 0);// 第n+1个

cell0.setCellValue(list.get(i - 1).getId().toString());

HSSFCell cell1 = row.createCell((short) 1);

cell1.setCellValue(list.get(i - 1).getContainerid().toString());

HSSFCell cell2 = row.createCell((short) 2);

cell2.setCellValue(list.get(i - 1).getIsocode().toString());

}

ByteArrayOutputStream os = new ByteArrayOutputStream();

wb.write(os);

byte[] content = os.toByteArray();

InputStream is = new ByteArrayInputStream(content);

// 设置response参数,可以打开下载页面

response.reset();

response.setContentType("application/vnd.ms-excel;charset=utf-8");

response.setHeader("Content-Disposition",

"attachment;filename=" + new String(("box" + ".xls").getBytes(), "iso-8859-1"));

ServletOutputStream out = response.getOutputStream();

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

bis = new BufferedInputStream(is);

bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];

int bytesRead;

// Simple read/write loop.

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

} finally {

if (bis != null)

bis.close();

if (bos != null)

bos.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值