zoukankan      html  css  js  c++  java
  • java导出excel(解决导出几万条数据内存溢出的问题)

    
    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) {  
        	System.out.println(System.currentTimeMillis());
            StringBuffer sb = new StringBuffer();  
            try {  
                DataOutputStream rafs = new DataOutputStream(  
                        new BufferedOutputStream(new FileOutputStream(new File(  
                                "d://test.xlsx"))));  
                sb.append("<?xml version="1.0"?>");  
                sb.append("
    ");  
                sb.append("<?mso-application progid="Excel.Sheet"?>");  
                sb.append("
    ");  
                sb.append("<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"");  
                sb.append("
    ");  
                sb.append("  xmlns:o="urn:schemas-microsoft-com:office:office"");  
                sb.append("
    ");  
                sb.append(" xmlns:x="urn:schemas-microsoft-com:office:excel"");  
                sb.append("
    ");  
                sb.append(" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"");  
                sb.append("
    ");  
                sb.append(" xmlns:html="http://www.w3.org/TR/REC-html40">");  
                sb.append("
    ");  
                sb.append(" <Styles>
    ");  
                sb.append("  <Style ss:ID="Default" ss:Name="Normal">
    ");  
                sb.append("   <Alignment ss:Vertical="Center"/>
    ");  
                sb.append("   <Borders/>
    ");  
                sb.append("   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
    ");  
                sb.append("   <Interior/>
    ");  
                sb.append("   <NumberFormat/>
    ");  
                sb.append("   <Protection/>
    ");  
                sb.append("  </Style>
    ");  
                sb.append(" </Styles>
    ");  
    //             int sheetcount = 0;  
                int recordcount = 100000;  
                int currentRecord = 0;  
                int total = 100000;  
                int col = 10;  
                sb.append("<Worksheet ss:Name="Sheet0">");  
                sb.append("
    ");  
                sb.append("<Table ss:ExpandedColumnCount="" + col  
                        + "" ss:ExpandedRowCount="" + total  
                        + "" x:FullColumns="1" x:FullRows="1">");  
                sb.append("
    ");  
                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("
    ");  
                        sb.append("<ProtectObjects>False</ProtectObjects>");  
                        sb.append("
    ");  
                        sb.append("<ProtectScenarios>False</ProtectScenarios>");  
                        sb.append("
    ");  
                        sb.append("</WorksheetOptions>");  
                        sb.append("
    ");  
                        sb.append("</Worksheet>");  
                        sb.append("<Worksheet ss:Name="Sheet" + i / recordcount  
                                + "">");  
                        sb.append("
    ");  
                        sb.append("<Table ss:ExpandedColumnCount="" + col  
                                + "" ss:ExpandedRowCount="" + recordcount  
                                + "" x:FullColumns="1" x:FullRows="1">");  
                        sb.append("
    ");  
                    }  
                    sb.append("<Row>");  
                    for (int j = 0; j < col; j++) {  
                    	if(i==0) sb.append("<Cell><Data ss:Type="String">标题"+j+"</Data></Cell>"); 
                    	else sb.append("<Cell><Data ss:Type="String">c"+i+"&"+j+"</Data></Cell>"); 
                        
                        sb.append("
    ");  
                    }  
                    sb.append("</Row>");  
                    if (i % 5000 == 0) {  
                        rafs.write(sb.toString().getBytes("utf-8"));  //注意编码
                        rafs.flush();  
                        sb.setLength(0);  
                    }  
                    sb.append("
    ");  
                    currentRecord++;  
                }  
                rafs.write(sb.toString().getBytes());  
                sb.setLength(0);  
                sb.append("</Table>");  
                sb.append("<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">");  
                sb.append("
    ");  
                sb.append("<ProtectObjects>False</ProtectObjects>");  
                sb.append("
    ");  
                sb.append("<ProtectScenarios>False</ProtectScenarios>");  
                sb.append("
    ");  
                sb.append("</WorksheetOptions>");  
                sb.append("
    ");  
                sb.append("</Worksheet>");  
                sb.append("</Workbook>");  
                sb.append("
    ");  
                rafs.write(sb.toString().getBytes());  
                rafs.flush();  
                rafs.close();  
                System.out.println(System.currentTimeMillis());
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    } 
    

      

  • 相关阅读:
    上门量体不能停,量品打造“一人一版”的私人定制衬衫,半年覆盖30个城市
    直击中小互联网企业痛点,程序员客栈推出短期雇佣功能
    现在,培训老师也有可能是你未来的 Boss
    【蓝领日志】捷库,给B端提供蓝领员工的标准化培训工具
    【调研】华图教育:领跑公职培训市场
    社区服务中心+护理员培训+云平台,中康行健“一体两翼”构建养老生态体系
    餐厅孵化器“优粮生活”,用孵化模式打造统一独立外卖品牌
    语培市场热度不减,“手韩”背后的垂直领域空间在哪?
    母基金_百度百科
    小编亲测杭州最火的海南鸡饭,最好吃的居然是……-搜狐吃喝!!!
  • 原文地址:https://www.cnblogs.com/Nbge/p/3792781.html
Copyright © 2011-2022 走看看