参考博客: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 }
时间: 2024-10-08 15:47:06