标签:
// 1先创建工作簿对 HSSFWorkbook workbook2003 = new HSSFWorkbook(); // 2创建工作表对象并命名 HSSFSheet sheet = workbook2003.createSheet("学生信息统计表"); //3 创建行创建了第一行 HSSFRow row = sheet.createRow(0); //4在第一行创建一列 HSSFCell nameCell = row.createCell(0); nameCell.setCellValue("测试"); //5创建文件 File file = new File("d:\\123.xls"); FileOutputStream fos = null; try { fos = new FileOutputStream(file); workbook2003.write(fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (Exception e) { e.printStackTrace(); } } } //处理文件名 String realname = "d:\\123.xls"; FileInputStream in; //创建输出流 OutputStream out; //设置响应头,控制浏览器下载该文件 try { response.setHeader("content-disposition", "attachment; filename=" + URLEncoder.encode(realname, "UTF-8")); //读取要下载的文件,保存到文件输入流 in = new FileInputStream("d:\\123.xls"); out = response.getOutputStream(); //创建缓冲区 byte buffer[] = new byte[1024]; int len = 0; //循环将输入流中的内容读取到缓冲区当中 while((len=in.read(buffer))>0){ //输出缓冲区的内容到浏览器,实现文件下载 out.write(buffer, 0, len); } //关闭文件输入流 in.close(); //关闭输出流 out.close(); } catch (Exception e) { e.printStackTrace(); }finally{ }
标签:
原文地址:http://www.cnblogs.com/moodcalm/p/5404257.html