Servlet實現匯出下載csv檔案
工作上遇到的場景,需要將客戶列表資料匯出成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();
}
}
實現效果:
相關文章
- Java實現CSV檔案的匯出Java
- js實現使用檔案流下載csv檔案JS
- servlet實現檔案下載demoServlet
- 使用Servlet實現檔案下載Servlet
- Mysql匯入csv檔案MySql
- java匯出CSV檔案Java
- PHP 匯出 CSV 格式檔案PHP
- SQLServer匯入大CSV檔案SQLServer
- mysql匯入csv格式檔案MySql
- oralce 匯出csv格式檔案
- java快速實現匯出生成csv檔案(含註釋程式碼)Java
- Servlet下載檔案Servlet
- SQLite3 匯出 CSV 檔案SQLite
- Jsp+Servlet實現檔案上傳下載(一)--檔案上傳JSServlet
- PHP 匯出大資料 CSV 檔案PHP大資料
- C# 匯出DataTable至csv檔案C#
- JS彈出下載對話方塊以及實現常見檔案型別的下載JS型別
- 實現簡單的csv檔案上傳和bootstrap表格的下載boot
- 使用PHP原生匯出Excel和CSV檔案PHPExcel
- office for Mac Excel匯入csv檔案亂碼MacExcel
- 批次匯出csv檔案的基本嘗試
- 批量匯出csv檔案的基本嘗試
- django下載csv檔案筆記Django筆記
- servlet實現下載功能Servlet
- servlet檔案下載例項剖析Servlet
- 使用Vue+go實現前後端檔案的上傳下載,csv檔案上傳下載可直接照搬VueGo後端
- PHP匯出大量資料,儲存為CSV檔案PHP
- linux 下MySQL命令列匯出csv檔案格式LinuxMySql命令列
- ServiceNow如何匯出豎線分割的CSV檔案?
- 將csv檔案匯入到neo4j中
- 下載內表到本地.CSV檔案中
- 解決PHP匯出CSV檔案中文亂碼問題PHP
- 修改以前寫的合同匯入CSV檔案程式碼
- Flutter實現檔案下載Flutter
- AJAX實現檔案下載----
- 使用go語言對csv檔案進行解析處理,匯入匯出。Go
- csv是什麼格式檔案 csv檔案是什麼意思
- 如何實現十億級離線 CSV 匯入 Nebula Graph