求Java poi 大数据量导出(五万行数据) 例子
如题,需要导出五万行的数据,(不要问我为什么或者改变导出文件的格式,因为这是客户的操蛋需求不能改)希望有做过的高手发个例子给我,时间紧迫.控制台导出Excel即可,但要保...
如题, 需要导出五万行的数据, (不要问我为什么或者改变导出文件的格式, 因为这是客户的操蛋需求不能改) 希望有做过的高手发个例子给我,时间紧迫. 控制台导出Excel即可, 但要保证5W数据不会内存溢出.
代码要求, 导出(生成)Excel的方法传一个List<String[]>和导出路径即可, 可用代码追加100分,十分感谢! 展开
代码要求, 导出(生成)Excel的方法传一个List<String[]>和导出路径即可, 可用代码追加100分,十分感谢! 展开
展开全部
我给你个办法,是要导出excel吗?别使用poi,使用xml来描述excel即可,就像流文件一样,多少都不会流出,给你个思路,你自己扩展,希望你能看明白。
/**
* @author J.W
* 2011-06-08
*/
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
try {
DataOutputStream rafs = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(
"d://test.xml"))));
sb.append("<?xml version=\"1.0\"?>");
sb.append("\n");
sb.append("<?mso-application progid=\"Excel.Sheet\"?>");
sb.append("\n");
sb.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.append("\n");
sb.append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sb.append("\n");
sb.append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sb.append("\n");
sb.append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.append("\n");
sb.append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sb.append("\n");
sb.append(" <Styles>\n");
sb.append(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n");
sb.append(" <Alignment ss:Vertical=\"Center\"/>\n");
sb.append(" <Borders/>\n");
sb.append(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>\n");
sb.append(" <Interior/>\n");
sb.append(" <NumberFormat/>\n");
sb.append(" <Protection/>\n");
sb.append(" </Style>\n");
sb.append(" </Styles>\n");
int sheetcount = 0;
int recordcount = 20;
int currentRecord = 0;
int total = 100;
int col = 20;
sb.append("<Worksheet ss:Name=\"Sheet0\">");
sb.append("\n");
sb.append("<Table ss:ExpandedColumnCount=\"" + col
+ "\" ss:ExpandedRowCount=\"" + total
+ "\" x:FullColumns=\"1\" x:FullRows=\"1\">");
sb.append("\n");
for (int i = 0; i < total; i++) {
if ((currentRecord == recordcount
|| currentRecord > recordcount || currentRecord == 0)
&& i != 0) {// 一个sheet写满
currentRecord = 0;
rafs.write(sb.toString().getBytes());
sb.setLength(0);
sb.append("</Table>");
sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.append("\n");
sb.append("<ProtectObjects>False</ProtectObjects>");
sb.append("\n");
sb.append("<ProtectScenarios>False</ProtectScenarios>");
sb.append("\n");
sb.append("</WorksheetOptions>");
sb.append("\n");
sb.append("</Worksheet>");
sb.append("<Worksheet ss:Name=\"Sheet" + i / recordcount
+ "\">");
sb.append("\n");
sb.append("<Table ss:ExpandedColumnCount=\"" + col
+ "\" ss:ExpandedRowCount=\"" + recordcount
+ "\" x:FullColumns=\"1\" x:FullRows=\"1\">");
sb.append("\n");
}
sb.append("<Row>");
for (int j = 0; j < col; j++) {
System.out.println(i);
sb.append("<Cell><Data ss:Type=\"String\">10000000000000000123</Data></Cell>");
sb.append("\n");
}
sb.append("</Row>");
if (i % 5000 == 0) {
rafs.write(sb.toString().getBytes());
rafs.flush();
sb.setLength(0);
}
sb.append("\n");
currentRecord++;
}
rafs.write(sb.toString().getBytes());
sb.setLength(0);
sb.append("</Table>");
sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.append("\n");
sb.append("<ProtectObjects>False</ProtectObjects>");
sb.append("\n");
sb.append("<ProtectScenarios>False</ProtectScenarios>");
sb.append("\n");
sb.append("</WorksheetOptions>");
sb.append("\n");
sb.append("</Worksheet>");
sb.append("</Workbook>");
sb.append("\n");
rafs.write(sb.toString().getBytes());
rafs.flush();
rafs.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
可以直接执行,只是给你个思路。
/**
* @author J.W
* 2011-06-08
*/
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
try {
DataOutputStream rafs = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(
"d://test.xml"))));
sb.append("<?xml version=\"1.0\"?>");
sb.append("\n");
sb.append("<?mso-application progid=\"Excel.Sheet\"?>");
sb.append("\n");
sb.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.append("\n");
sb.append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sb.append("\n");
sb.append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sb.append("\n");
sb.append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.append("\n");
sb.append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sb.append("\n");
sb.append(" <Styles>\n");
sb.append(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n");
sb.append(" <Alignment ss:Vertical=\"Center\"/>\n");
sb.append(" <Borders/>\n");
sb.append(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>\n");
sb.append(" <Interior/>\n");
sb.append(" <NumberFormat/>\n");
sb.append(" <Protection/>\n");
sb.append(" </Style>\n");
sb.append(" </Styles>\n");
int sheetcount = 0;
int recordcount = 20;
int currentRecord = 0;
int total = 100;
int col = 20;
sb.append("<Worksheet ss:Name=\"Sheet0\">");
sb.append("\n");
sb.append("<Table ss:ExpandedColumnCount=\"" + col
+ "\" ss:ExpandedRowCount=\"" + total
+ "\" x:FullColumns=\"1\" x:FullRows=\"1\">");
sb.append("\n");
for (int i = 0; i < total; i++) {
if ((currentRecord == recordcount
|| currentRecord > recordcount || currentRecord == 0)
&& i != 0) {// 一个sheet写满
currentRecord = 0;
rafs.write(sb.toString().getBytes());
sb.setLength(0);
sb.append("</Table>");
sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.append("\n");
sb.append("<ProtectObjects>False</ProtectObjects>");
sb.append("\n");
sb.append("<ProtectScenarios>False</ProtectScenarios>");
sb.append("\n");
sb.append("</WorksheetOptions>");
sb.append("\n");
sb.append("</Worksheet>");
sb.append("<Worksheet ss:Name=\"Sheet" + i / recordcount
+ "\">");
sb.append("\n");
sb.append("<Table ss:ExpandedColumnCount=\"" + col
+ "\" ss:ExpandedRowCount=\"" + recordcount
+ "\" x:FullColumns=\"1\" x:FullRows=\"1\">");
sb.append("\n");
}
sb.append("<Row>");
for (int j = 0; j < col; j++) {
System.out.println(i);
sb.append("<Cell><Data ss:Type=\"String\">10000000000000000123</Data></Cell>");
sb.append("\n");
}
sb.append("</Row>");
if (i % 5000 == 0) {
rafs.write(sb.toString().getBytes());
rafs.flush();
sb.setLength(0);
}
sb.append("\n");
currentRecord++;
}
rafs.write(sb.toString().getBytes());
sb.setLength(0);
sb.append("</Table>");
sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.append("\n");
sb.append("<ProtectObjects>False</ProtectObjects>");
sb.append("\n");
sb.append("<ProtectScenarios>False</ProtectScenarios>");
sb.append("\n");
sb.append("</WorksheetOptions>");
sb.append("\n");
sb.append("</Worksheet>");
sb.append("</Workbook>");
sb.append("\n");
rafs.write(sb.toString().getBytes());
rafs.flush();
rafs.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
可以直接执行,只是给你个思路。
追问
很感谢你的思路,只是XML是没有格式的,可读性和excel也无法相比,所以没办法用xml
追答
xml可以描述Excel的任何格式,你只是没有去观察,至于使用poi或者jxl都不行,这个路我已经走过了。希望你不要走弯路,因为他们在写入的机制一样,都是一个单元格一个对象,你想如果5w数据10列的话都多少个单元格对象在内存中?这样你的内存不溢出才怪。
我刚才给你写的demo 你把生成的文件格式直接修改为 xls试试看看什么效果
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询