Java Export as Excel or XML

Source: Internet
Author: User

Original: Java exported as Excel or XML

Source code: Http://www.zuidaima.com/share/1550463713774592.htm

Java is exported as Excel or XML, purely hand-written. Copy into eclipse to run the class directly, you can see the effect.

Package Com.zuidaima.file.exam.test;import Java.io.bufferedoutputstream;import Java.io.dataoutputstream;import Java.io.file;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import java.io.IOException;/** * Large data volumes exported to Excel or XML * @author Administrator www.zuidaima.com * */public class exportutil{public static void Main (string[] Arg s) {StringBuffer sb = new StringBuffer (); try {dataoutputstream rafs = new DataOutputStream (New Bufferedoutputstream Leoutputstream ("D://test2.xls")), 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:o Ffice: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=\ "Arial \" 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) {//one SHeet Write Full 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\" >111</Data></Cell> "); Sb.append (" \ n ");} Sb.append ("</Row>"); if (i% = 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 ()}}}


Java Export as Excel or XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.