码迷,mamicode.com
首页 > 其他好文 > 详细

csv文件导出

时间:2017-05-07 11:41:05      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ret   exists   工具   工具类   cti   list   str   tor   导入   

参考博客:http://www.cnblogs.com/mingforyou/p/4103132.html

导入jar包javacsv.jar

链接:http://pan.baidu.com/s/1i5IDQ1R 密码:mycm

<!--导出csv-->
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>

新建工具类;

public class ExportCSV {
	/**
  * CSV文件生成方法
  * @param head
  * @param dataList
  * @param outPutPath
  * @param filename
  * @return
  */
 public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
         String outPutPath, String filename) {

     File csvFile = null;
     BufferedWriter csvWtriter = null;
     try {
         csvFile = new File(outPutPath + File.separator + filename + ".csv");
         File parent = csvFile.getParentFile();
         if (parent != null && !parent.exists()) {
             parent.mkdirs();
         }
         csvFile.createNewFile();

         // GB2312使正确读取分隔符","
         csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                 csvFile), "GB2312"), 1024);
         // 写入文件头部
         writeRow(head, csvWtriter);

         // 写入文件内容
         for (List<Object> row : dataList) {
             writeRow(row, csvWtriter);
         }
         csvWtriter.flush();
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         try {
             csvWtriter.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     return csvFile;
 }
 /**
  * 写一行数据方法
  * @param row
  * @param csvWriter
  * @throws IOException
  */
 private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
     // 写入文件头部
     for (Object data : row) {
         StringBuffer sb = new StringBuffer();
         String rowStr = sb.append("\"").append(data).append("\",").toString();
         csvWriter.write(rowStr);
     }
     csvWriter.newLine();
 }
 public File createCSVFile(HttpServletRequest request,ResultSet rs,Object[] head,String fileNames) throws Exception{
     
     // 设置表格头
     List<Object> headList = Arrays.asList(head);        
     // 设置数据
     List<List<Object>> dataList = new ArrayList<List<Object>>();
     List<Object> rowList = null;
 	while (rs.next()) {
 		rowList = new ArrayList<Object>();
 		for (int i = 0; i < head.length; i++) {
        	rowList.add(rs.getString(i+1));
        }
		dataList.add(rowList);
 	}
     // 导出文件路径
     String downloadFilePath = "C:" + File.separator + "系统日志" + File.separator + "download" + File.separator;
 
//     // 导出文件名称
     Date d = new Date();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     String dateNowStr = sdf.format(d);
//     String fileName = "导出操作日志数据" + dateNowStr;	        
     fileNames = fileNames+dateNowStr;
     // 导出CSV文件
     File csvFile = createCSVFile(headList, dataList, downloadFilePath, fileNames);
     
     return csvFile;
 }
 

}

  调用这个类;

			ResultSet rs = null;
			try {
				conn = DriverManager.getConnection(conf.getURL(),
						conf.getDBUser(), conf.getDBPasswd());// 获得数据库连接

				rs = userList(conn, sql);// 得到结果集
				
//				这个是导出csv文件start
				Object[] head ={ "用户名", "操作时间", "操作模块", "操作内容" };// 标题 
				ExportCSV exportCsv = new ExportCSV();
				
				exportCsv.createCSVFile(request, rs, head, "导出操作日志数据");
//				这个是导出csv文件end
				
				//这个是导出excel start
//				Workbook wb = new HSSFWorkbook();
//				String headers[] = { "用户名", "操作时间", "操作模块", "操作内容" };// 标题
//				fillExcelData(rs, wb, headers);
//				export(response, wb, "导出操作日志数据.xls");
				//这个是导出excel end
				
			} 

  

 

csv文件导出

标签:ret   exists   工具   工具类   cti   list   str   tor   导入   

原文地址:http://www.cnblogs.com/jwlfpzj/p/6819667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!