用CSV框架导出csv文件实现下载(jsp)

<%@page import="com.fantasia.common.model.storage.Inventory"%>
<%@page import="com.fantasia.common.model.sale.Selling"%>
<%@page import="com.fantasia.common.model.product.Product"%>
<%@page import="com.fantasia.common.model.product.item.SaleItem"%>
<%@page import="com.fantasia.common.util.StringUtil"%>
<%@page import="com.fantasia.common.model.product.Predict.WAYS"%>
<%@page import="com.fantasia.common.model.product.Predict.TYPES"%>
<%@page import="com.fantasia.predict.service.PredictManager"%>
<%@page import="com.fantasia.common.model.product.Predict"%>
<%@page import="com.fantasia.common.model.Config"%>
<%@page import="java.util.List"%>
<%@page import="com.csvreader.CsvWriter"%>
<%@page import="com.fantasia.common.model.Cache"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
if(session.getAttribute("user")!=null){
String BASE_PATH=com.fantasia.common.util.ServletUtil.getBase(request);
String name="analyzeproduct.csv";
String warehouse = request.getParameter("warehouse");
String marketplace= request.getParameter("marketplace");
String type= request.getParameter("type");
String way= request.getParameter("way");
String rank= request.getParameter("rank");
String keyword= request.getParameter("keyword");
String category= request.getParameter("category");
int pageindex= 1;
int pagesize= 10000;
String active= request.getParameter("active");
String fba= request.getParameter("fba");
String brand= request.getParameter("brand");
String pse= request.getParameter("pse");
String pasin= request.getParameter("pasin");
String aft = request.getParameter("aft");
boolean top30 = StringUtil.parseBoolean(request.getParameter("top30"));
List<Predict> items = Cache.get(PredictManager.class).predict_query(pagesize,pageindex,Predict.TYPES.valueOf(type),Predict.WAYS.valueOf(way),
		rank,category,warehouse,marketplace,keyword,StringUtil.parseBoolean(active),StringUtil.parseBoolean(fba),brand,pse,pasin,aft,top30).items;
if(items!=null&&items.size()>0){
String path=Config.FILE_PATH+Config.TEMP_PATH+name;
CsvWriter writer=new CsvWriter(path,',',java.nio.charset.Charset.forName("UTF-8"));
writer.writeRecord(new String[]{"Sku","discontinue","actual margin","RPS","Current Turnover","Future Turnover","last 1","last 2","last 3",
		"last 4","last 5","last 6","last 7","库","留","占","售","计","订","入","途","UN","CT","FT"});
for(Predict item : items){
	SaleItem saleitem = null;
	if(item instanceof Product){
		saleitem = ((Product)item).sale(warehouse);
	}
	else{
		saleitem = ((Selling)item).sitem;
	}
	if(saleitem==null)continue;
	int[] weeksale = saleitem.weeksale;
	Inventory inventory = saleitem.inventory();
	writer.writeRecord(new String[]{item.sku,saleitem.discontinue==1?"true":"false",saleitem.actmargin+"",saleitem.allps+"",saleitem.future+"",""+saleitem.future_onway_operate,
			weeksale.length>0?weeksale[0]+"":"",weeksale.length>1?weeksale[1]+"":"",weeksale.length>2?weeksale[2]+"":"",weeksale.length>3?weeksale[3]+"":"",
			weeksale.length>4?weeksale[4]+"":"",weeksale.length>5?weeksale[5]+"":"",weeksale.length>6?weeksale[6]+"":"",inventory.fulfillable+"",
			inventory.reserved+"",inventory.pending+"",(inventory.fulfillable-inventory.reserved-inventory.pending)+"",inventory.planned+"",inventory.ordered+"",
			inventory.received+"",inventory.shipped+"",inventory.unsellable+"",""+(inventory.fulfillable+inventory.unsellable),
			""+(inventory.fulfillable+inventory.unsellable+inventory.planned+inventory.ordered+inventory.received+inventory.shipped)});
}
if(warehouse.endsWith("FIRST")){
	for(int i=2;i>0;i--){
		writer.writeRecord(new String[]{""});
	}
	warehouse = warehouse.equals("US_FIRST")?"US_AMAZON":"UK_AMAZON";
	writer.writeRecord(new String[]{warehouse+" Sku","库","留","占","售","计","订","入","途","UN","CT","FT"});
	for(Predict item : items){
		SaleItem saleitem = null;
		if(item instanceof Product){
			saleitem = ((Product)item).sale(warehouse);
		}
		else{
			saleitem = ((Selling)item).sitem;
		}
		if(saleitem==null)continue;
		Inventory inventory = saleitem.inventory();	
		writer.writeRecord(new String[]{item.sku,inventory.fulfillable+"",
				inventory.reserved+"",inventory.pending+"",(inventory.fulfillable-inventory.reserved-inventory.pending)+"",inventory.planned+"",inventory.ordered+"",
				inventory.received+"",inventory.shipped+"",inventory.unsellable+"",""+(inventory.fulfillable+inventory.unsellable),
				""+(inventory.fulfillable+inventory.unsellable+inventory.planned+inventory.ordered+inventory.received+inventory.shipped)});
	}
}
writer.flush();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\""+name+"\"");
try{
java.io.OutputStreamWriter outWriter = new java.io.OutputStreamWriter( response.getOutputStream(), "GBK" );
outWriter.write(com.fantasia.common.util.FileUtil.read(path,"UTF-8"));
outWriter.flush();
outWriter.close();response.flushBuffer();out.clear();out=pageContext.pushBody();
}catch(Exception e){e.printStackTrace();}}}
%>

核心代码是:

CsvWriter writer=new CsvWriter(path,',',java.nio.charset.Charset.forName("UTF-8"));
writer.writeRecord(new String[]{"Sku","discontinue","actual margin","RPS","Current Turnover","Future Turnover","last 1","last 2","last 3",
		"last 4","last 5","last 6","last 7","库","留","占","售","计","订","入","途","UN","CT","FT"});


参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

龙少X100

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值