java 中 excel生成并文件下载保存到本地

servlet类
 package com.dragon.action;
import Java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ExcelServlet extends HttpServlet {
 /**
  * Constructor of the object.
  */
 public ExcelServlet() {
  super();
 }
 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }
 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
//  response.setContentType("text/html");
//  PrintWriter out = response.getWriter();
//  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//  out.println("<HTML>");
//  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//  out.println("  <BODY>");
//  out.print("    This is ");
//  out.print(this.getClass());
//  out.println(", using the GET method");
//  out.println("  </BODY>");
//  out.println("</HTML>");
//  out.flush();
//  out.close();
  this.doPost(request, response);
 }
 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  response.setContentType("application/vnd.ms-excel");
  OutputStream out = response.getOutputStream();
  //报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框
  //attachment表示以附件方式下载。如果要在页面中打开,则改为 inline
  response.setHeader("Content-Disposition", "attachment; filename=TestExcel1.xls ");
  //创建workbook工作薄
  Workbook workbook = new HSSFWorkbook();
  //创建工作表
  Sheet sheet = workbook.createSheet("用户信息");
  //创建第二个工作薄
  Sheet sheet2 = workbook.createSheet();
  //为工作薄起名字
  workbook.setSheetName(1, "口袋里的小龙");
  //设置单元格样式
  HSSFCellStyle hssfCellStyle = (HSSFCellStyle) workbook.createCellStyle();
  hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中显示
  hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//纵向居中
  //创建行
  Row row = sheet.createRow(0);
  //创建单元格
  Cell cell = row.createCell(0);
  //设置第一行第一格的值
  cell.setCellValue("姓名");
  //设置单元格的文本居中显示
  cell.setCellStyle(hssfCellStyle);
  //创建单元格
  Cell cell1 = row.createCell(1);
  //设置第一行第一格的值
  cell1.setCellValue("性别");
  cell1.setCellStyle(hssfCellStyle);
  //创建单元格
  Cell cell2 = row.createCell(2);
  //设置第一行第一格的值
  cell2.setCellValue("年龄");
  cell2.setCellStyle(hssfCellStyle);
  //创建单元格
  Cell cell3 = row.createCell(3);
  //设置第一行第一格的值
  cell3.setCellValue("家庭住址");
  cell3.setCellStyle(hssfCellStyle);
  for (int i = 1; i <= 5; i++) {
   //创建行
   Row rows = sheet.createRow(i);
   //创建单元格
   Cell cells = rows.createCell(0);
   //设置第一行第一格的值
   cells.setCellValue("张三"+i);
   //创建单元格
   Cell cell1s = rows.createCell(1);
   //设置第一行第一格的值
   cell1s.setCellValue("男");
   //创建单元格
   Cell cell2s = rows.createCell(2);
   //设置第一行第一格的值
   cell2s.setCellValue(18+i);
   //创建单元格
   Cell cell3s = rows.createCell(3);
   //设置第一行第一格的值
   cell3s.setCellValue("家庭住址"+i);
  }
  workbook.write(out);
  System.out.println("数据写入成功!");
  out.flush();
  out.close();
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }
}

index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>

  <body>
    <form action="servlet/ExcelServlet" method="post">
     <input type="submit" value="测试"></input>
    </form>
  </body>
</html>

转载自http://blog.csdn.net/koudailidexiaolong/article/details/45913207

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值