Java parsing excle and generating excle documents

Source: Internet
Author: User

Just stick out the key code, and the rest of the introduction



Java parsing excle file:


Workbook book = workbook.getworkbook (New file (fileName))  ; //filename for the excle to be parsed  sheet sheet = book.getsheet (0);  int rows = sheet.getrows ();int  Celllength = sheet.getrow (0). length;//only get the data for so many columns of headings, and the rest of it doesn't matter list<list> all_data =  new arraylist (); for (int i = 1; i < rows; i++)  {    //i=1, because the first row header needs to be removed   list data = new arraylist ();   cell  [] cell = sheet.getrow (i);                           for (int j =0; j<celllength; j++)  {  string content= "";  try{      //Save      content=sheet.getcell (j, i) for data on line I, Column J. GetContents ();    }catch (excepTion e) {  content= "";   }    data.add (content);  }   //finally all the data are stored in All_data   all_data.add (data);}


General operation needs to upload and re-parse, once silly thought can be uploaded when the direct analysis, only to find the wrong.

The real basic flow is, excle upload to local, get saved in the local road, read the path of the Excle file.


Generate the Excle file and download

response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8"); String filename = new string ("Payroll template. xls". GetBytes ("gb2312"), "iso8859-1"); Response.AddHeader ("Content-disposition",  "filename="  + filename); Outputstream os=response.getoutputstream () ;//Create a working thin writableworkbook workbook = workbook.createworkbook (OS); Writablesheet sheet = workbook.createsheet ("First sheet", 0);//A excle file can have multiple sheet//to create a title, Suppose only 2 columns Label l = new label (0,0, "employee Number"), Sheet.addcell (L); L = new label (1, 0, " Name "); Sheet.addcell (L);//Start adding data L = new label (0,1," n "); L = new label (1, 1," Zhang San "); Sheet.addcell (l); L = new label (0,2, "the") L = new label (1, 2, "John Doe"); Sheet.addcell (l );//Direct download, if it is necessary to write in a location, then another  workbook.write ();   workbook.close ();      Os.flush ();  os.close (); 


Description, the label inside the parameter is label (column, row, content)


This article is from the "Bulajunjun" blog, make sure to keep this source http://5148737.blog.51cto.com/5138737/1774554

Java parsing excle and generating excle documents

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.