Java导出csv格式,百万数据
我遇到的问题是:导出csv格式的报内存溢出错误,数据量小没问题,7万左右会出现问题,,看百度上说可以:导入csv格式的时候,写缓冲区什么的,可我不是很明白怎么写,没搜到想要的答案,所以想请大家帮忙了,谢谢了啊
下面是我的代码
public class ExportUtil {
/**
* 导出成CSV TXT resultData中每个是一行数据。
*
* @param out
* @param colTitleList
* 标题
* @param resultData
* 数据。
* @throws Exception
* String title,String[] rowName,List<Object[]> dataList
*/
public static void writeCSV(HttpServletResponse response, String title,
String[] colTitleList, List<Object[]> resultData) throws Exception {
System.out.println("writeCSV 方法进入 ");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
String dates = sdf2.format(new Date());
title = dates +title;
String downloadFileName = new String(title.getBytes("gbk"), "iso8859-1");
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="
+ downloadFileName + ".csv");// 指定下载的文件名
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
if (resultData == null)
resultData = new ArrayList();
PrintWriter pw = null;
pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(
output, 1024), "GBK"));
if (colTitleList != null) {// 写标题
for (int i = 0; i < colTitleList.length; i++) {
String outValue = String.valueOf(colTitleList[i]);
pw.print(outValue);
if (i != colTitleList.length - 1)
pw.print(",");
}
pw.println();
}
for (int i = 0; i < resultData.size(); i++) {
/* ArrayList rowData = (ArrayList)resultData.get(i); */
Object[] rowData = resultData.get(i);// 遍历每个对象
for (int j = 0; j < rowData.length; j++) {
String value = null;
if (!"".equals(rowData[j]) && rowData[j] != null) {
value = rowData[j].toString();
pw.print("\"" + value.replace("\"", "\"\"") + "\"");
}
if (j != rowData.length - 1)
pw.print(",");
}
pw.println();
}
pw.close();
output.close();
}
}
下面是我的代码
public class ExportUtil {
/**
* 导出成CSV TXT resultData中每个是一行数据。
*
* @param out
* @param colTitleList
* 标题
* @param resultData
* 数据。
* @throws Exception
* String title,String[] rowName,List<Object[]> dataList
*/
public static void writeCSV(HttpServletResponse response, String title,
String[] colTitleList, List<Object[]> resultData) throws Exception {
System.out.println("writeCSV 方法进入 ");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
String dates = sdf2.format(new Date());
title = dates +title;
String downloadFileName = new String(title.getBytes("gbk"), "iso8859-1");
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="
+ downloadFileName + ".csv");// 指定下载的文件名
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
if (resultData == null)
resultData = new ArrayList();
PrintWriter pw = null;
pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(
output, 1024), "GBK"));
if (colTitleList != null) {// 写标题
for (int i = 0; i < colTitleList.length; i++) {
String outValue = String.valueOf(colTitleList[i]);
pw.print(outValue);
if (i != colTitleList.length - 1)
pw.print(",");
}
pw.println();
}
for (int i = 0; i < resultData.size(); i++) {
/* ArrayList rowData = (ArrayList)resultData.get(i); */
Object[] rowData = resultData.get(i);// 遍历每个对象
for (int j = 0; j < rowData.length; j++) {
String value = null;
if (!"".equals(rowData[j]) && rowData[j] != null) {
value = rowData[j].toString();
pw.print("\"" + value.replace("\"", "\"\"") + "\"");
}
if (j != rowData.length - 1)
pw.print(",");
}
pw.println();
}
pw.close();
output.close();
}
}
...全文
相关推荐
java 导出百万条数据代码
java 导出百万条数据代码 附带jar包及实例
Java高效导出百万级数据实战指南
在实现百万级数据的导出过程中,我们遇到了以下问题:性能瓶颈:单一的读取和写入操作消耗时间长,存在性能瓶颈。内存溢出:大数据量导致内存使用率激增,存在内存溢出的风险。任务中断:长时间运行的任务容易因各种意外中断,需要解决数据一致性和完整性的问题。针对这些问题,我们采取了以下解决思路:分批处理:对数据进行分批读取和导出,减少单次处理的数据量。异步处理与多线程:使用异步和多线程技术,提高数据处理的效率。断点续传和分片下载。
java 百万千万级别excel数据导出问题
java应用场景解决方案
Excel百万级别数据的导入和导出
Excel百万级别数据的导入和导出 全部资料 源码 数据库 测试数据excel
Mybatis百万数据插入(含导出)表格
excel表格


