csv文件导出
Posted 个子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csv文件导出相关的知识,希望对你有一定的参考价值。
参考博客: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,String outPutPath) 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;
// String downloadFilePath = "C:" + 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, outPutPath, fileNames);
// File csvFile = createCSVFile(headList, dataList, downloadFilePath, fileNames);
return csvFile;
}
2.。class 调用 csv构造文件
rs = userList(conn, sql);// 得到结果集
// 得到下载路径start
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
// System.out.println("文件夹:"+file.getAbsolutePath());
String outPutPath = file.getAbsolutePath();
// 得到下载路径end
// 这个是导出csv文件start
Object[] head ={ "用户名", "操作时间", "操作模块", "操作内容" };// 标题
ExportCSV exportCsv = new ExportCSV();
// String outPutPath="C:";
exportCsv.createCSVFile(request, rs, head, "导出操作日志数据",outPutPath);
// 这个是导出csv文件end
//这个是导出excel start
// Workbook wb = new HSSFWorkbook();
// String headers[] = { "用户名", "操作时间", "操作模块", "操作内容" };// 标题
// fillExcelData(rs, wb, headers);
// export(response, wb, "导出操作日志数据.xls");
//这个是导出excel end
以上是关于csv文件导出的主要内容,如果未能解决你的问题,请参考以下文章