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