CSV导出使用实例
来源:互联网 发布:linux kill函数 编辑:程序博客网 时间:2024/06/03 22:09
package com.cdel.portal.stage.appointment.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletResponse;import com.cdel.portal.stage.appointment.entity.AppointmentSign;import com.cdel.portal.stage.appointment.entity.MemberAccTicket;/** * csv 导出工具类 * @author zhangheng * @date 2017年7月7日下午1:27:07 */public class CSVUtils { /** * 导出签到信息 * @date 2017年7月7日下午1:27:07 * @param response * @param courseOrders */ public static void exportCvs(HttpServletResponse response ,List<AppointmentSign> appointmentSigns){ try { response.reset(); response.setContentType("application/csv;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename=Sign.csv"); response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.print("姓名,联系电话,听课城市,签到时间 \r"); for (AppointmentSign appointmentSign:appointmentSigns) { Date createDate =appointmentSign.getAppointmentTime(); String date =""; if(createDate!=null){ SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); date =sdf.format(createDate); } String str = "\t"+appointmentSign.getUserName()+" ," +"\t"+appointmentSign.getPhone()+" ," +"\t"+appointmentSign.getCityName()+" ," +"\t"+date; str = str.replace("null", ""); out.print(str+"\r"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void exportCvs1(HttpServletResponse response ,List<MemberAccTicket> memberAccTickets){ try { response.reset(); response.setContentType("application/csv;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename=AppointMent.csv"); response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.print("姓名,联系电话,听课城市,签到时间 \r"); for (MemberAccTicket memberAccTicket:memberAccTickets) { Date createDate =memberAccTicket.getCreateDate(); String date =""; if(createDate!=null){ SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); date =sdf.format(createDate); } String str = "\t"+memberAccTicket.getUserName()+" ," +"\t"+memberAccTicket.getMobile()+" ," +"\t"+memberAccTicket.getCityName()+" ," +"\t"+date; str = str.replace("null", ""); out.print(str+"\r"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void exportCvs2(HttpServletResponse response ,List<MemberAccTicket> memberAccTickets){ try { response.reset(); response.setContentType("application/csv;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename=AppointMent.csv"); response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.print("姓名,联系电话,听课城市,QQ,签到时间 \r"); for (MemberAccTicket memberAccTicket:memberAccTickets) { Date createDate =memberAccTicket.getCreateDate(); String date =""; if(createDate!=null){ SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); date =sdf.format(createDate); } String str = "\t"+memberAccTicket.getUserName()+" ," +"\t"+memberAccTicket.getMobile()+" ," +"\t"+memberAccTicket.getCityName()+" ," +"\t"+memberAccTicket.getEmail()+" ," +"\t"+date; str = str.replace("null", ""); out.print(str+"\r"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static boolean exportCsv(File file, List<String> dataList){ boolean isSucess=false; FileOutputStream out=null; OutputStreamWriter osw=null; BufferedWriter bw=null; try { out = new FileOutputStream(file); osw = new OutputStreamWriter(out); bw =new BufferedWriter(osw); if(dataList!=null && !dataList.isEmpty()){ for(String data : dataList){ bw.append(data).append("\r"); } } isSucess=true; } catch (Exception e) { isSucess=false; }finally{ if(bw!=null){ try { bw.close(); bw=null; } catch (IOException e) { e.printStackTrace(); } } if(osw!=null){ try { osw.close(); osw=null; } catch (IOException e) { e.printStackTrace(); } } if(out!=null){ try { out.close(); out=null; } catch (IOException e) { e.printStackTrace(); } } } return isSucess; } /** * 导入 * * @param file csv文件(路径+文件) * @return */ public static List<String> importCsv(File file){ List<String> dataList=new ArrayList<String>(); BufferedReader br=null; try { br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { dataList.add(line); } }catch (Exception e) { }finally{ if(br!=null){ try { br.close(); br=null; } catch (IOException e) { e.printStackTrace(); } } } return dataList; }}
阅读全文
0 0
- CSV导出使用实例
- php 导出csv文件实例
- mysql csv 使用实例
- Java导出CSV文件实例程序
- python csv模块使用实例
- php导出csv文件,可导出前导0实例
- Silverlight4使用radgridview读取导出CSV文件
- 使用PHP导入和导出CSV文件
- 使用PHP导入和导出CSV文件
- 使用PHP导入和导出CSV文件
- 使用PHP导入和导出CSV文件
- 导出报表CSV,卸载迅雷,使用IE
- 使用PHP导入和导出CSV文件
- 合理使用导出csv、excel方式
- [php] thinkphp使用csv导出excel表格
- 使用PHP导入和导出CSV文件
- 使用PHP导入和导出CSV文件
- 使用PHP导入和导出CSV文件
- 漫漫职场路,一步都不能走错!
- tomcat7优化之配置线程池
- apache
- 如何知道网站是用哪个语言开发的
- 大话数据结构笔记-串
- CSV导出使用实例
- MultipartHttpServletRequest 多文件上传。个人demo
- Linux入门基础教程
- git pull 失败 ,提示:fatal: refusing to merge unrelated histories
- 解决两个日期之间计算工作日实现2
- windows下采用Anaconda2中的Python画出caffe深度卷积神经网络结构图
- jqueryeasyui input框变textarea
- 大话数据结构笔记-栈和队列
- PHP使用Solr全文搜索引擎