将数据到处到Excel
生活随笔
收集整理的這篇文章主要介紹了
将数据到处到Excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用開源的POI組件,包含實現對Excal文件的創建和寫入操作的類,使POI組件操作EXCEL文件的步驟
createSheet(String sheetname) //參數sheetname表示工作表的名稱
rownum) //參數rownum表示工作表中行對象的行號
columnIndex) //參數columnIndex表示單元格對象的列編號
index.jsp頁面
<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
ExportServlet的Servlet類,doPost方法獲得信息,POI組件將信息導出到POI組件
public class ExportServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("application/vnd.ms-excel");//設置響應正文的MIME類型,該類型表示Excel//response.addHeader("Content-Disposition", "attachment;filename=logininfo.xls");String name = request.getParameter("name");String pwd =request.getParameter("pwd");String sex = request.getParameter("sex");String age = request.getParameter("age");String email = request.getParameter("email");ServletOutputStream out = response.getOutputStream();//響應輸出流對象HSSFWorkbook wb = new HSSFWorkbook(); //創建Excel表格HSSFSheet sheet = wb.createSheet("用戶注冊信息"); //創建工作簿sheet.setColumnWidth(4, 5000); //設置列寬HSSFRow titleRow = sheet.createRow(0); //創建Excel中的標題行HSSFCell titleCell1 =titleRow .createCell(0); //在行中創建第1個單元格titleCell1.setCellValue("用戶姓名"); //設置第1個單元格的值HSSFCell titleCell2= titleRow.createCell(1); //在行中創建第2個單元格titleCell2.setCellValue("密碼"); //設置第2個單元格的值HSSFCell titleCell3 =titleRow .createCell(2); //在行中創建第3個單元格titleCell3.setCellValue("性別"); //設置第3個單元格的值HSSFCell titleCell4= titleRow.createCell(3); //在行中創建第4個單元格titleCell4.setCellValue("年齡"); //設置第4個單元格的值HSSFCell titleCell5= titleRow.createCell(4); //在行中創建第5個單元格titleCell5.setCellValue("Email"); //設置第5個單元格的值HSSFRow valueRow = sheet.createRow(1); //創建第2行HSSFCell nameCell = valueRow.createCell(0); //在第2行中創建單元格nameCell.setCellValue(name);HSSFCell pwdCell = valueRow.createCell(1);pwdCell.setCellValue(pwd);HSSFCell sexCell = valueRow.createCell(2);sexCell.setCellValue(sex);HSSFCell ageCell = valueRow.createCell(3);ageCell.setCellValue(age);HSSFCell emailCell = valueRow.createCell(4);emailCell.setCellValue(email);HSSFCellStyle cellStyle = wb.createCellStyle();wb.write(out); //將響應流輸入到Excel表格中out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}web.xml文件配置
<servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>ExportServlet</servlet-name><servlet-class>com.lh.servlet.ExportServlet</servlet-class></servlet><servlet-mapping><servlet-name>ExportServlet</servlet-name><url-pattern>/export</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>總結
以上是生活随笔為你收集整理的将数据到处到Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记录用户访问次数
- 下一篇: 利用Servlet生成动态验证码