將資料匯出到Excel

weixin_33866037發表於2018-08-21

UseServlet.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

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;

@WebServlet("/user.do")
public class UseServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //設定字符集格式為utf-8
        //req.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/vnd.ms-excel");
        resp.addHeader("Content-Disposition", "attachment;filename=logininfo.xls");
        String name= req.getParameter("name");
        String pwd=req.getParameter("pwd");
        String sex= req.getParameter("sex");
        String age= req.getParameter("age");
        String email= req.getParameter("email");
        ServletOutputStream out = resp.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);//在行中建立第一個單元格
        titleCell1.setCellValue("使用者姓名");//設定第一個單元格的值
        HSSFCell titleCell2 = titleRow.createCell(1);
        titleCell2.setCellValue("密碼");
        HSSFCell titleCell3 = titleRow.createCell(2);
        titleCell3.setCellValue("性別");
        HSSFCell titleCell4 = titleRow.createCell(3);
        titleCell4.setCellValue("年齡");
        HSSFCell titleCell5 = titleRow.createCell(4);
        titleCell5.setCellValue("Email");
        HSSFRow valueRow = sheet.createRow(1);//在第二行中建立單元格
        HSSFCell nameCell= valueRow.createCell(0);
        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);//將響應流輸入到表格中
        out.flush();
        out.close();

index.jsp

    <form action="user.do" method="post">
        <table align="center">
            <tr>
                <td>使用者名稱:</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>密碼:</td>
                <td><input type="password" name="pwd"/></td>
            </tr>
            <tr>
                <td>性別:</td>
                <td>
                    <input type="radio" name="sex" value="男"/>男
                    <input type="radio" name="sex" value="女"/>女
                </td>
            </tr>
            <tr>
                <td>年齡:</td>
                <td><input type="text" name="age"/></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                <input type="submit" value="輸入到表格"/>
                <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>
    </form>
    
9995085-7c092767d5f4788b.PNG
333.PNG

輸出到表格時是從瀏覽器下載下來的,具體的我也不知道是什麼原理...

相關文章