"Java" without additional packages, the contents of Java output to Excel, no garbled, absolutely compatible with EXCEL2003 and 2007

Source: Internet
Author: User
Tags php programming

Java output a text to txt everyone basically, this is a required course of learning Java, will not have no problem, specifically can see "Java" input and output and JDK1.5 after the new string StringBuilder "(Click to open the link). Most of the content that translates into Excel for Java content online is what POI package, JSL package, and a bunch of weird jar plugins are needed. In fact, just use java.io.*; This basic package will be able to output Java content into the Excel table, of course, if you are to deal with MySQL database do not do this, a direct MySQL query command will be able to output MySQL query results in the Excel table. Specifically, "MySQL" will be a table of MySQL exported to Excel (click the Open link) said, here no longer repeat. Just to give an example, highlight Java if you print content to Excel.


I. BASIC OBJECTIVES

In Java there are the following student class student definitions:

Class Student {public int s_no;public String s_name;public int s_class;}

Establish three student classes, respectively, set the studious number, name and class pressed into a special store students class ArrayList,

public static void Main (String args[]) throws IOException {arraylist<student> Studentarr = new Arraylist<student > (); Student S1 = new Student () S1.s_no = 1;s1.s_name = "Chinese"; s1.s_class = 102;studentarr.add (S1); Student s2 = new Student (); s2.s_no = 2;s2.s_name = "Yes No"; s2.s_class = 101;studentarr.add (s2); Student s3 = new Student (); s3.s_no = 3;s3.s_name = "Problem!" "; s3.s_class = 103;studentarr.add (S3); Javatoexcel (Studentarr); SYSTEM.OUT.PRINTLN ("Student table. XML has been generated, this XML is specifically opened in Excel XML");}
Then call Javatoexcel This method, the core method is the focus of the next explanation, and finally the three classes output to the C: \ Student table. XML, the specific effect is as follows.



Second, the basic idea

See here someone might think I'm typing a typo, what? The Excel file has a suffix name other than. xls or. xlsx? Or someone who thought I was deliberately exporting XML and then right-clicking Open with Excel.

No, the default way to open this XML is Microsofe Office excel2003/2007.


After Office 2003 is installed, the. xml file is opened in Windows2003 in the same way as Microsofe office Excel2003.


this. xml file is not in the "Java" configuration file concept, Java operation of the configuration file (click the Open link) has been described in the configuration file, but Excel2003 and Excel2007 in one of the save format, but people are not used to it.


Of course, this thing is also an ordinary. xml configuration file, because of its header, note that this is the XML file to be interpreted into Excel, so its default open way is Excel, if you set to Notepad open this student table. xls, it can happen that the content is like this, the slightest garbled not. Unlike the. doc files and the. xls files, it is a bunch of garbled characters.

<?xml version= "1.0"? ><?mso-application progid= "Excel.Sheet"? ><workbook xmlns= "urn: Schemas-microsoft-com:office:spreadsheet "xmlns:o=" Urn:schemas-microsoft-com:office:office "xmlns:x=" urn: Schemas-microsoft-com:office:excel "xmlns:ss=" Urn:schemas-microsoft-com:office:spreadsheet "xmlns:html="/HTTP/ Www.w3.org/TR/REC-html40 "><worksheet ss:name=" Student table "><table><row><cell><data Ss:type = "string" > Study number </data></cell><cell><data ss:type= "string" > Name </Data></Cell> <cell><data ss:type= "String" > class </Data></Cell></Row><Row><Cell>< Data ss:type= "string" >1</data></cell><cell><data ss:type= "string" > Chinese </Data>< /cell><cell><data ss:type= "String" >102</data></cell></row><row><cell ><data ss:type= "string" >2</data></cell><cell><data ss:type= "string" > Yes No </ Data></cell><celL><data ss:type= "String" >101</data></cell></row><row><cell><data SS: Type= "string" >3</data></cell><cell><data ss:type= "string" > Problem! </data></cell><cell><data ss:type= "String" >103</Data></Cell></Row> </Table></Worksheet></Workbook>

This XML, everyone with the end of the effect of Excel open, you can obviously find the corresponding relationship:


It is because of the. XML cross-platform no garbled features, so this output Java data to excel things, there is no package, in any Java environment, including JSP+SERVLET+JDBC, Struts+hibernate+spring, As long as you use the Java language, such as Android, you can output this kind of Excel table with. xls, all we need is to make the output of the data in the form of a string just like this! Office 2003 in Windows can be read without garbled characters.


Third, the production process

Therefore, you have the following Excel table. The Javatoexcel construction method of the XML file, which requires an array of good classes to be stored, and ultimately nothing is returned. The key is to construct a good <Row> and <cell>, I believe that there are ASP, JSP, PHP programming experience of the great God there is no problem for this, like the constant construction of <td>, to the end of the line structure <tr> can be.

public static void Javatoexcel (Arraylist<student> studentarr) throws IOException {//using StringBuilder is more efficient than string , there are not so many computer resources occupied. Before the final output, turn to a string. StringBuilder Excelxmlstringbuilder = new StringBuilder ("");//Construct Good Excelxml head excelxmlstringbuilder.append ("<?xml Version=\ "1.0\"? ><?mso-application progid=\ "Excel.sheet\"? ><workbook xmlns=\ "urn: Schemas-microsoft-com:office:spreadsheet\ "xmlns:o=\" urn:schemas-microsoft-com:office:office\ "xmlns:x=\" urn: Schemas-microsoft-com:office:excel\ "xmlns:ss=\" urn:schemas-microsoft-com:office:spreadsheet\ "xmlns:html=\" http ://www.w3.org/tr/rec-html40\ "><worksheet ss:name=\" student table \ "><Table>");// Output Good header excelxmlstringbuilder.append ("<row><cell><data ss:type=\" string\ "> No. </data></ Cell><cell><data ss:type=\ "string\" > Name </data></cell><cell><data ss:Type=\ " String\ "> Class </Data></Cell></Row>");//construct each line for (int i = 0; i < studentarr.size (); i++) {//construct first <row> node Excelxmlstringbuilder.append ("<Row>"); Student Student = Studentarr.get (i);//continue to construct each <Cell> node Excelxmlstringbuilder.append ("<cell><data Ss:type=\ "string\" > "+ student.s_no+" </data></cell><cell><data ss:Type=\ "String\" > "+ student.s_name+ "</data></cell><cell><data ss:type=\" string\ ">" + Student.s_class + "</ Data></cell> "); Excelxmlstringbuilder.append (" </Row> ");} Then construct the Excelxml excelxmlstringbuilder.append ("</Table></Worksheet></Workbook>");// Finally, the string is printed to the C: \ Student table. XML is done.//false represents the overwrite output, not the end of this file to continue output printwriter PrintWriter = new PrintWriter ("FileWriter Student table. xml ", false));//Convert Excelxmlstringbuilder to String printwriter.print (Excelxmlstringbuilder +") before output;// Empty the output buffer Printwriter.flush ();//must close the file output stream, Java will print out a string in the file, that is, the binary stream, Printwriter.close ();}


Iv. Summary and Prospect

So the entire Java file is like this, which is an example of XML application! Maybe someone will ask me why not directly output to. csv, the main. csv this thing in Office2003 and Office2007 has a bug, the author of the Test no matter how to code, its default open is a bug, it must be the user to do a certain action to show normal, this is rather bad, and. xls format is too complex, if not The help of extra packages is very difficult. In particular, some of the egg-hurting bosses are asking for no messy introduction. jar, that's all you have to do. xml format is just fine.

Import Java.io.*;import java.util.*;class Student {public int s_no;public String s_name;public int s_class;} public class Excelprint {public static void Javatoexcel (Arraylist<student> studentarr) throws IOException {// Using StringBuilder is more efficient than string and consumes less computer resources. Before the final output, turn to a string. StringBuilder Excelxmlstringbuilder = new StringBuilder ("");//Construct Good Excelxml head excelxmlstringbuilder.append ("<?xml Version=\ "1.0\"? ><?mso-application progid=\ "Excel.sheet\"? ><workbook xmlns=\ "urn: Schemas-microsoft-com:office:spreadsheet\ "xmlns:o=\" urn:schemas-microsoft-com:office:office\ "xmlns:x=\" urn: Schemas-microsoft-com:office:excel\ "xmlns:ss=\" urn:schemas-microsoft-com:office:spreadsheet\ "xmlns:html=\" http ://www.w3.org/tr/rec-html40\ "><worksheet ss:name=\" student table \ "><Table>");// Output Good header excelxmlstringbuilder.append ("<row><cell><data ss:type=\" string\ "> No. </data></ Cell><cell><data ss:type=\ "string\" > Name </data></cell><cell><datA ss:type=\ "string\" > Class </Data></Cell></Row> ");//constructs each line for (int i = 0; i < studentarr.size (); i+ +) {//First construct the <Row> node excelxmlstringbuilder.append ("<Row>"); Student Student = Studentarr.get (i);//continue to construct each <Cell> node Excelxmlstringbuilder.append ("<cell><data Ss:type=\ "string\" > "+ student.s_no+" </data></cell><cell><data ss:Type=\ "String\" > "+ student.s_name+ "</data></cell><cell><data ss:type=\" string\ ">" + Student.s_class + "</ Data></cell> "); Excelxmlstringbuilder.append (" </Row> ");} Then construct the Excelxml excelxmlstringbuilder.append ("</Table></Worksheet></Workbook>");// Finally, the string is printed to the C: \ Student table. XML is done.//false represents the overwrite output, not the end of this file to continue output printwriter PrintWriter = new PrintWriter ("FileWriter Student table. xml ", false));//Convert Excelxmlstringbuilder to String printwriter.print (Excelxmlstringbuilder +") before output;// Empty the output buffer Printwriter.flush ();//must close the file output stream, Java will print out a string in the file, that is, the binary stream, Printwriter.close ();} Public STAtic void Main (String args[]) throws IOException {arraylist<student> Studentarr = new arraylist<student> (); Student S1 = new Student () S1.s_no = 1;s1.s_name = "Chinese"; s1.s_class = 102;studentarr.add (S1); Student s2 = new Student (); s2.s_no = 2;s2.s_name = "Yes No"; s2.s_class = 101;studentarr.add (s2); Student s3 = new Student (); s3.s_no = 3;s3.s_name = "Problem!" "; s3.s_class = 103;studentarr.add (S3); Javatoexcel (Studentarr); SYSTEM.OUT.PRINTLN ("Student table. XML has been generated, the XML is specifically opened in Excel XML");}}

"Java" without additional packages, the contents of Java output to Excel, no garbled, absolutely compatible with EXCEL2003 and 2007

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.