servlet简单的导入导出EXCEL


public String exp(){

HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=Test.csv");
List<Test> list=page.getDataList();
DataOutputStream stream=new DataOutputStream(response.getOutputStream());
String content="名称, 二维码, 状态,创建时间,主题数量,推广量\n";
stream.write(content.getBytes("GBK"));
for (Test test : list) {
content=test.getName()+","+test.getCode()+","+test.getStatus().getDisplayName()+","+test.getCreateTime()+","+test.getTopicSize()+","+test.getPopularize()+"\n";
stream.write(content.getBytes("GBK"));
}
stream.flush();
} catch (Exception e) {
log.error(e);
}
return null;
}

public String imp(){
BufferedReader in =null;
try {
if(upLoadFile!=null){
if(upLoadFile.getName().indexOf(".csv")==-1){
this.setMessage("错误的数据文件,请重新选择文件!");
list();
}
String line;
in = new BufferedReader(new FileReader(upLoadFile));
List<Test> batch=new ArrayList<Test>();
line = in.readLine();
while((line = in.readLine()) != null){
Test tmp=new Test();
tmp.setCreateTime(TimeUtil.getCurrentDateTime());
tmp.setStatus(Test.Status.PENDING);
tmp.setTimes(0);
String[] datas=line.split(",");
if(datas!=null&&datas.length>0){
tmp.setName(datas[0]);//FIXME the format of data
tmp.setCode(datas[1]);
}
batch.add(tmp);
}
}
} catch (Exception e) {
log.error("Update load file error:", e);

}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
in = null;
}
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值