编程学习资讯网

java导出大批量(百万以上)数据的excel文件--编程学习网

发布时间:2017-05-02 13:47:39   来源:本站编辑   浏览次数:

本文实例为大家分享了java导出百万以上数据的excel文件,供大家参考,具体内容如下

1.传统的导出方式会消耗大量的内存,2003每个sheet页最多65536条数据,2007每个sheet页可以达到100万条数据以上,2007会在生成Workbook时清理数据,所以2007导出量更大;

2.可以导出多个excel文件到某个目录中,然后打包下载;

3.导出excel格式的xml文件,这种方式可以分批导出数据,适用于大批量数据的导出,以下简单介绍这种方式:

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
packagecom.epay.utils;
 
/**
 * 大数据量导出成EXCEL或XML
 * @author qad
 * 2017-04-22
 */
importjava.io.BufferedOutputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
 
publicclassTest {
  publicstaticvoidmain(String[] args) {
    StringBuffer sb =newStringBuffer();
    try{
      DataOutputStream rafs =newDataOutputStream(
          newBufferedOutputStream(newFileOutputStream(newFile(
              "d://test.xls"))));
      sb.append("<?xml version=\"1.0\" encoding=\"GBK\" ?>");
      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("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"> ");
      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");
      intsheetcount =0;
      intrecordcount =65535;
      intcurrentRecord =0;
      inttotal =100000;
      intcol =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(inti =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(intj =0; j < col; j++) {
          System.out.println(i);
          sb.append("<Cell><Data ss:Type=\"String\">111</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) {
      e.printStackTrace();
    }catch(IOException e) {
      e.printStackTrace();
    }
  }
}

导出xml文件之后直接修改后缀名为.xlsx就可以直接打开,弊端:无法导出.xls格式的excel文件.以上只是一种简单想法,留待以后具体实现.

扩展:既然可以使用xml文件导出excel,那么导出csv文件之后也可以直接改后缀名为.xls,目前只是一种想法,尚未证实可行性.

编程学习网 http://www.javalearns.cn

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java