A small example of using JXL to read an Excel merged cell

Source: Internet
Author: User

time 2014-11-09 14:25:20 csdn Blog Original http://blog.csdn.net/th676759829/article/details/40949545 ThemeExcelJava

At work we may want to save records of Excel files to the database,

Today, I encountered the problem of merging lattice when I read the Excel file with JXL, and I recorded the following:

such as Excel files are as follows:

The records read are:

As can be seen, the merged cells are only output for the first time, so that when the data is saved

To the database when there will be missing data, so did a certain transformation, the code is as follows:

Package temp;Import Java.io.File;Import java.io.IOException;Import JXL. Range;Import JXL. Sheet;Import JXL. Workbook;Import jxl.read.biff.BiffException;PublicClassSimpleexcelread { Publicvoid Readexcel (File file)Throws Biffexception, IOException { Workbook WB = workbook.getworkbook (file);Get an Excel Workspace object from a file stream Sheet Sheet = Wb.getsheet (0); System.out.println (File.getname ()); System.out.println ("The first sheet name is:" + sheet.getname ()); System.out.println ("First sheet total:" + sheet.getrows () +Line   + sheet.getcolumns () +"Column"); System.out.println ("The specific contents are as follows:"); range[] Rangecell = Sheet.getmergedcells ();  for (int i =0; I < sheet.getrows (); i++) {   for (Int J =0; J < Sheet.getcolumns (); J + +) {   String str =Null   str = Sheet.getcell (j, i). getcontents ();    for (Range R:rangecell) {     if (i > R.gettopleft (). GetRow ()      && i <= r.getbottomright (). GetRow ()      && J >= R.gettopleft (). GetColumn ()      && J <= R.getbottomright (). GetColumn ()) {     str = Sheet.getcell (R.gettopleft (). GetColumn (),       R.gettopleft (). GetRow ()). getcontents ();    }   }   System.out.print (str + "\ t");   }   System.out.println ();  }  Wb.close ();  public static void main ( String[] args) throws biffexception, IOException {  simpleexcelread sr = new simpleexcelread ();   file File = new File ( "Test.xls");  Sr.readexcel (file); }              /span>             

The output results are as follows:

When you save your data, you don't miss the data.

A small example of using JXL to read an Excel merged cell

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.