国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-06 08:04:34
字體:
供稿:網(wǎng)友
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(); //報頭用于提供一個推薦的文件名,并強(qiáng)制瀏覽器顯示保存對話框 //attachment表示以附件方式下載。如果要在頁面中打開,則改為 inline response.setHeader("Content-Disposition", "attachment; filename=TestExcel1.xls "); //創(chuàng)建workbook工作薄 Workbook workbook = new HSSFWorkbook(); //創(chuàng)建工作表 Sheet sheet = workbook.createSheet("用戶信息"); //創(chuàng)建第二個工作薄 Sheet sheet2 = workbook.createSheet(); //為工作薄起名字 workbook.setSheetName(1, "口袋里的小龍"); //設(shè)置單元格樣式 HSSFCellStyle hssfCellStyle = (HSSFCellStyle) workbook.createCellStyle(); hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中顯示 hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//縱向居中 //創(chuàng)建行 Row row = sheet.createRow(0); //創(chuàng)建單元格 Cell cell = row.createCell(0); //設(shè)置第一行第一格的值 cell.setCellValue("姓名"); //設(shè)置單元格的文本居中顯示 cell.setCellStyle(hssfCellStyle); //創(chuàng)建單元格 Cell cell1 = row.createCell(1); //設(shè)置第一行第一格的值 cell1.setCellValue("性別"); cell1.setCellStyle(hssfCellStyle); //創(chuàng)建單元格 Cell cell2 = row.createCell(2); //設(shè)置第一行第一格的值 cell2.setCellValue("年齡"); cell2.setCellStyle(hssfCellStyle); //創(chuàng)建單元格 Cell cell3 = row.createCell(3); //設(shè)置第一行第一格的值 cell3.setCellValue("家庭住址"); cell3.setCellStyle(hssfCellStyle); for (int i = 1; i <= 5; i++) { //創(chuàng)建行 Row rows = sheet.createRow(i); //創(chuàng)建單元格 Cell cells = rows.createCell(0); //設(shè)置第一行第一格的值 cells.setCellValue("張三"+i); //創(chuàng)建單元格 Cell cell1s = rows.createCell(1); //設(shè)置第一行第一格的值 cell1s.setCellValue("男"); //創(chuàng)建單元格 Cell cell2s = rows.createCell(2); //設(shè)置第一行第一格的值 cell2s.setCellValue(18+i); //創(chuàng)建單元格 Cell cell3s = rows.createCell(3); //設(shè)置第一行第一格的值 cell3s.setCellValue("家庭住址"+i); } workbook.write(out); System.out.println("數(shù)據(jù)寫入成功!"); 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="descrCSS" href="styles.css"> --> </head> <body> <form action="servlet/ExcelServlet" method="post"> <input type="submit" value="測試"></input> </form> </body></html>

轉(zhuǎn)載自http://blog.csdn.net/koudailidexiaolong/article/details/45913207


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜昌市| 丹棱县| 信丰县| 墨江| 吕梁市| 廊坊市| 大渡口区| 肃宁县| 黎平县| 张北县| 东港市| 怀来县| 榆中县| 海南省| 资阳市| 金华市| 苏尼特左旗| 西华县| 新昌县| 定结县| 阳朔县| 恩施市| 垣曲县| 江山市| 沅陵县| 巍山| 绩溪县| 潮州市| 和龙市| 台北县| 长阳| 尼勒克县| 顺平县| 阜平县| 麻阳| 任丘市| 辉县市| 苗栗县| 四子王旗| 巫溪县| 连州市|