Java Generate Excel File tool class instance

Source: Internet
Author: User


Import Java.io.file;import Java.io.ioexception;import JXL. Workbook;import Jxl.write.label;import Jxl.write.writablesheet;import Jxl.write.writableworkbook;import Jxl.write.writeexception;import Jxl.write.biff.rowsexceededexception;import Org.apache.log4j.logger;public Class excelutils {static private Logger Logger = Logger.getlogger (excelutils.class); static public int SUCCESS = 0;static Public  int ERROR = -1;/** * Create a single sheet Excel file, specify the path yourself, use a two-dimensional array to map the corresponding cell **/static public int createexcel (String filerootpath,string Filename,string sheetname,string[][] dataarr) {int result = Error;try {if (Filerootpath = = NULL | | "". Equals (Filerootpath) | | FileName = = NULL | | ". Equals (FileName)) {throw new Exception (" Create Excel File Error: Createexcel method parameter value is empty! ");} if (Dataarr = = NULL | | dataarr[0] = = null) {throw new Exception ("Input parameter Dataarr error!");} String FilePath = Filerootpath + fileName; File File = new file (Filerootpath), if (!file.exists ()) {file.mkdirs ();} Writableworkbook book = workbook.createworkbook (New filePath)); if (sheetname = = NULL | | "". Equals (SheetName)) {sheetname = "first page";} A worksheet named SheetName is generated, and the parameter 0 indicates that this is the first page writablesheet sheet = book.createsheet (sheetname, 0);//The name of the cell in the constructor of the label object is the first row ( 0,0)//And cell contents testfor (int i=0; i<dataarr.length;i++) {for (int j=0;j<dataarr[0].length;j++) {Label label = new Label (J, I, Dataarr[i][j]); Sheet.addcell (label);//Add a defined cell to the worksheet}}book.write (); Book.close (); result = SUCCESS;} catch (IOException e) {logger.error ("Excel File path Error!"); Logger.error (E.tostring ()); return result;} catch (Rowsexceededexception e) {logger.error ("When creating Excel, line exceeds!"); Logger.error (E.tostring ()); return result;} catch (WriteException e) {logger.error ("Write error when creating Excel!"); Logger.error (E.tostring ()); return result;} catch (Exception e) {logger.error (e.tostring ()); return result;} return result;} public static void Main (String args[]) {string[][] Myarr = new string[2][3];myarr[0][0]= "one"; myarr[0][1]= "n"; myarr[0][ "2]=", "myarr[1][0]=", "myarr[1][1]="; myarr[1][2]= "23"; Excelutils.createexcel ("d:/test/"," Test.xls "," Dealer Account List ", Myarr);} 


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.