Convert EXCEL to XML

Source: Internet
Author: User

From: http://dongisland.iteye.com/blog/1432019

Environment

Jdk1.6 and jxl. Jar

Both of them can be online, I'm under this jxl (http://ishare.iask.sina.com.cn/f/14559561.html? From = dl) You can operate on excel2010

Model class (in case there are too many parameters to create a class)

package com.island;public class User {    /**     * UID     */    private static final long serialVersionUID = 346821702783141852L;        private String username;    private String usermail;    private String password;        public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUsermail() {return usermail;}public void setUsermail(String usermail) {this.usermail = usermail;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}        }

Title excuse (keyword excuse)

Package COM. island; public interface userkeys {public static final string user_name_value = "username"; public static final string user_email_value = "User email"; public static final string user_password_value = "User Password ";}

Excel2xml class

Package COM. island; import Java. io. bufferedwriter; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstream; import Java. io. outputstreamwriter; import Java. io. unsupportedencodingexception; import Java. util. arraylist; import Java. util. iterator; import jxl. cell; import jxl. sheet; import jxl. workbook; import jxl. read. biff. BIF Fexception; public class excel2xml {/*** the output stream to write to */private outputstream out;/*** the encoding to write */private string encoding; public excel2xml (outputstream out, string ENC) {encoding = ENC; this. out = out; If (encoding = NULL |! Encoding. equals ("unicodebig") {encoding = "utf8" ;}}/*** read the Excel file, save it to formbean, and put it in arraylist to return **/public static arraylist readexcel (File file) {arraylist userlist = new arraylist (); workbook WB = NULL; try {WB = workbook. getworkbook (File);} catch (biffexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} If (WB = NULL) return NULL; sheet [] sheets = WB. getsheets (); If (sheets! = NULL & sheets. length> 0) {for (INT I = 0; I <sheets. length; I ++) {int rownum = sheets [I]. getrows (); int columnnum = sheets [I]. getcolumns (); cell [] Title = NULL; For (int row = 0; row <rownum; row ++) {If (ROW = 0) {Title = sheets [I]. getrow (ROW);} else {Cell [] rowcontent = sheets [I]. getrow (ROW); User user = paserunit (title, rowcontent); userlist. add (User) ;}}} WB. close (); Return userlist ;}/** * Generate xml * @ Param filmlist * @ throws ioexception */Public void generatexml (arraylist userlist) throws ioexception {try {outputstreamwriter OSW = new outputstreamwriter (Out, encoding) based on the given information ); bufferedwriter BW = new bufferedwriter (OSW); BW. write ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "); BW. newline (); iterator it = userlist. iterator (); BW. write ("<user_info_root>"); BW. newline (); While (it. hasnext () {user = (User) it. next (); BW. write ("<user_info>"); BW. newline (); BW. write ("<Name>" + User. getUserName () + "</Name>"); BW. newline (); BW. write ("<email>" + User. getusermail () + "</Email>"); BW. newline (); BW. write ("<password>" + User. getPassword () + "</Email>"); BW. newline (); BW. write ("</user_info>"); BW. newline ();} BW. write ("</user_info_root>"); BW. flush (); BW. close ();} catch (unsupportedencodingexception e) {e. printstacktrace () ;}}/*** parses each row of data in Excel Based on the Excel header, put formbean * @ Param title * @ Param rowcontent * @ return */public static user paserunit (cell [] title, cell [] rowcontent) {user usercontent = new user (); for (INT I = 0; I <rowcontent. length; I ++) {Cell filmcell = title [I]; string user = filmcell. getcontents (); system. out. println (User); cell filmcontentcell = rowcontent [I]; string uservalue = filmcontentcell. getcontents (); If (userkeys. user_name_value.equals (User) {usercontent. setusername (uservalue);} If (userkeys. user_email_value.equals (User) {usercontent. setusermail (uservalue);} If (userkeys. user_password_value.equals (User) {usercontent. setpassword (uservalue) ;}} return usercontent;}/*** integer verification * @ Param Str * @ return */public static integer validateinteger (string Str) {INTEGER result = NULL; if (STR = NULL | Str. equals ("") {result = new INTEGER ("0"); return result ;}else {return New INTEGER (STR );}} /*** character encoding conversion * @ Param Str * @ return */public static string stringconersion (string Str) {string result = NULL; byte context []; try {context = Str. getbytes ("UTF-8"); Result = new string (context, "gb2312");} catch (unsupportedencodingexception e) {e. printstacktrace ();} return result;}/*** Excel-to-XML generation * @ Param excelfile * @ Param xmlfile */public static void exce2xmlcomplete (File excelfile, file xmlfile) {arraylist list = excel2xml. readexcel (excelfile); fileoutputstream Fos; try {Fos = new fileoutputstream (xmlfile); excel2xml OBJ = new excel2xml (FOS, "UTF-8"); try {obj. generatexml (list);} catch (ioexception e) {e. printstacktrace () ;}} catch (filenotfoundexception e) {e. printstacktrace () ;}/ *** @ Param ARGs */public static void main (string [] ARGs) {file = new file ("D: \ user.xls "); arraylist list = excel2xml. readexcel (File); fileoutputstream Fos; try {Fos = new fileoutputstream (new file ("D: \ User. XML "); excel2xml OBJ = new excel2xml (FOS," UTF-8 "); try {obj. generatexml (list);} catch (ioexception e) {e. printstacktrace () ;}} catch (filenotfoundexception e) {e. printstacktrace ();}}}

The last step is to implement the style in the attachment. It may be a bit complicated to simply use this method. It is recommended to do so, which will be helpful for future improvement!

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.