会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
hibernate3例子
博客园
首页
新随笔
联系
订阅
管理
利用POI生成Excel并通过Servlet下载示例
Java代码 package com.ljz; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class downloadExcelServletextends HttpServlet { private static final long serialVersionUID = 1L; public downloadExcelServlet() { super(); } public void destroy() { super.destroy(); } private InputStream getInputStream() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet1"); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell((short) 0); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("序号"); cell = row.createCell((short) 1); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("姓"); cell = row.createCell((short) 2); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("名"); cell = row.createCell((short) 3); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("年龄"); //创建一行记录 row = sheet.createRow(1); cell = row.createCell((short) 0); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("1"); cell = row.createCell((short) 1); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("刘"); cell = row.createCell((short) 2); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("继忠"); cell = row.createCell((short) 3); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("25"); // List<User> list = this.findAll(); // // for (int i = 0; i < list.size(); ++i) // { // User user = list.get(i); // // row = sheet.createRow(i + 1); // // cell = row.createCell((short) 0); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // cell.setCellValue(i + 1); // // cell = row.createCell((short) 1); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // cell.setCellValue(user.getFirstname()); // // cell = row.createCell((short) 2); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // cell.setCellValue(user.getLastname()); // // cell = row.createCell((short) 3); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // cell.setCellValue(user.getAge()); // } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); return is; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String("系统日志.xls".getBytes(),"iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(this.getInputStream()); 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 (final IOException e) { System.out.println("IOException."); throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void init() throws ServletException { // Put your code here } }
转自:http://ljzcome.iteye.com/blog/402157
posted @
2012-04-18 18:40
hibernate3例子
阅读(
561
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告