Servlet實現匯出下載csv檔案
阿新 • • 發佈:2019-01-28
工作上遇到的場景,需要將客戶列表資料匯出成csv檔案。測試了一個最簡單版本,記錄一下。
訂單實體類:
package entity; /** * 訂單資訊實體 * Created by dylan-pc on 2017/8/17. */ public class Order { private String orderNo; private String orderStatus; private String orderDt; private String productName; private double price; public Order(String orderNo, String orderStatus, String orderDt, String productName, double price) { this.orderNo = orderNo; this.orderStatus = orderStatus; this.orderDt = orderDt; this.productName = productName; this.price = price; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getOrderStatus() { return orderStatus; } public void setOrderStatus(String orderStatus) { this.orderStatus = orderStatus; } public String getOrderDt() { return orderDt; } public void setOrderDt(String orderDt) { this.orderDt = orderDt; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
匯出訂單Servlet
package servlet; import entity.Order; import util.CSVUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; /** * Created by dylan-pc on 2017/8/26. */ @WebServlet(name = "exportOrderServlet", urlPatterns = {"/exportOrder"}) public class exportOrderServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定response response.setContentType("application/x-download;charset=utf-8"); //生成樣例資料 List<Order> orders = Arrays.asList(new Order("sa20170826001", "shipping", "2017/8/26", "羅西尼女表", 499), new Order("sa20170826002", "submit", "2017/8/26", "耐克籃球鞋", 1099), new Order("sa20170826001", "finish", "2017/8/26", "新百倫G500慢跑鞋", 234)); //設定檔名 SimpleDateFormat dtf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String curDateStr = dtf.format(new Date()); String fileName = "訂單列表-" + curDateStr + ".csv"; fileName = URLEncoder.encode(fileName, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); //獲得列印流 PrintWriter pw = response.getWriter(); pw.println("訂單號, 訂單狀態, 下單日期,商品名稱,商品價格"); StringBuilder sb = new StringBuilder(); for (Order o : orders) { sb.append(o.getOrderNo() + "," + o.getOrderStatus() + "," + o.getOrderDt() + "," + o.getProductName() + "," + Double.toString(o.getPrice()) + "\n"); } pw.print(sb); pw.close(); } }