加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

生成XML文件

发布时间:2020-12-16 00:40:33 所属栏目:百科 来源:网络整理
导读:StringBuffer sb = new StringBuffer(); try { DataOutputStream rafs = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); sb.append("?xml version="1.0" encoding='GBK' ?"); sb.append("n"); sb.append("?mso-applicatio

StringBuffer sb = new StringBuffer();
try {
DataOutputStream rafs = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
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(" <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="10"/>n"); sb.append(" <Interior/>n"); sb.append(" <NumberFormat/>n"); sb.append(" <Protection/>n"); sb.append(" </Style>n"); sb.append(" </Styles>n"); int recordcount = 50000; int currentRecord = 0; int total = table.getRowCount(); int col = table.getColumnCount(); sb.append("<Worksheet Name='" + sheetName + "'>"); 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=" + sheetName + 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>"); if (i == 0) { sb.append("<Font ss:Size="16">"); } for (int j = 0; j < col; j++) { String data = getCell(i,j,table); String type = "String"; try { Double.parseDouble(data); type = "Number"; } catch (Exception ex) { type = "String"; } sb.append("<Cell><Data ss:Type="" + type + "">" + data + "</Data></Cell>"); sb.append("n"); } // sb.append("</Font>"); if (i == 0) { sb.append("</Font>"); } 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("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(); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读