Poi obtains the value of the first column in the first row of the merged cell.

Source: Internet
Author: User

When reading an Excel file, the content displayed in the 1st rows and 1st columns is: Merge cells.

Other cells in the merged cells area are not displayed.

 

The sample code is as follows:

1 import Java. io. fileinputstream; 2 Import Java. io. filenotfoundexception; 3 Import Java. io. ioexception; 4 5 import Org. apache. poi. hssf. usermodel. hssfworkbook; 6 Import Org. apache. poi. SS. usermodel. cell; 7 Import Org. apache. poi. SS. usermodel. dataformatter; 8 Import Org. apache. poi. SS. usermodel. row; 9 Import Org. apache. poi. SS. usermodel. sheet; 10 Import Org. apache. poi. SS. usermodel. workbook; 11 import org. Apache. poi. SS. util. cellrangeaddress; 12 Import Org. apache. poi. xssf. usermodel. xssfworkbook; 13 14 public class testexcel {15 Private Static final dataformatter formatter = new dataformatter (); 16 17/** 18 * Get the cell content 19*20 * @ Param cell 21 * Cell Object 22 * @ return convert the cell content into a string 23 */24 Private Static string getcellcontent (cell cell) {25 return formatter. formatcellvalue (cell); 26} 27 28 public stat IC Boolean ismergedregion (sheet, cell) {29 // obtain the number of merged cells in a sheet. 30 int sheetmergercount = sheet. getnummergedregions (); 31 for (INT I = 0; I <sheetmergercount; I ++) {32 // obtain the specific merged cell 33 cellrangeaddress CA = sheet. getmergedregion (I); 34 // get the starting row, ending row, starting column, ending column 35 int firstc = Ca. getfirstcolumn (); 36 int lastc = Ca. getlastcolumn (); 37 int firstr = Ca. getfirstrow (); 38 int lastr = Ca. getlastrow (); 39 // determines whether the cell is within the range of merged cells. If yes, true 40 if (cell. getcolumnindex () <= lastc & cell. getcolumnindex ()> = firstc) {41 if (cell. getrowindex () <= lastr & cell. getrowindex ()> = firstr) {42 return true; 43} 44} 45} 46 Return false; 47} 48 49 public static string getmergedregionvalue (sheet, cell) {50 // obtain the number of merged cells in a sheet: 51 int sheetmergercount = sheet. getnumme Rgedregions (); 52 // facilitate merging cells 53 for (INT I = 0; I <sheetmergercount; I ++) {54 // obtain the merging cells 55 cellrangeaddress CA = sheet. getmergedregion (I); 56 // get the starting row, ending row, starting column, ending column 57 int firstc = Ca. getfirstcolumn (); 58 int firstr = Ca. getfirstrow (); 59 60 if (cell. getcolumnindex () = firstc & cell. getrowindex () = firstr) {61 Return "th" + (cell. getrowindex () + 1) + "row number" + (cell. getcolumnindex () + 1) + "The column content is:" 62 + getcellcontent (cell) + ","; 63} 64 65} 66 return ""; 67} 68 69 Private Static string getexcelvalue (string filepath, int sheetindex) {70 string value = ""; 71 try {72 // create a workbook for the Excel Workbook file 73 = NULL; 74 try {75 book = new xssfworkbook (New fileinputstream (filepath); 76} catch (exception ex) {77 book = new hssfworkbook (New fileinputstream (filepath )); 78} 79 80 s HEET sheet = book. getsheetat (sheetindex); 81 // get all rows in the Excel file 82 int rows = sheet. getphysicalnumberofrows (); 83 // system. out. println ("rows:" + rows); 84 // traverse rows 85 86 for (INT I = 0; I <rows; I ++) {87 // read the top left cell 88 row = sheet. getrow (I); 89 // The row is not null 90 if (row! = NULL) {91 // get all columns in the Excel file 92 int cells = row. getphysicalnumberofcells (); 93 // system. out. println ("cells:" + cells); 94 95 // traverse column 96 for (Int J = 0; j <cells; j ++) {97 // obtain the column value 98 cell = row. getcell (j); 99 If (cell! = NULL) {100 if (ismergedregion (sheet, cell) {101 value + = getmergedregionvalue (sheet, cell ); 102} the content of the else {103 value + = "th" + (I + 1) + "row th" + (J + 1) + "column is: "+ getcellcontent (cell) +", "; 104} 105 106} 107 108} 109} 110} catch (filenotfoundexception e) {111 E. printstacktrace (); 113} catch (ioexception e) {114 E. printstacktrace (); 115} 116 117 return value; 118 119} 120 121 public static void main (string [] ARGs) {122 123 string filepath = "F: // example.xls "; 124 int sheetindex = 0; 125 126 string [] val = getexcelvalue (filepath, sheetindex ). split (","); 127 for (INT I = 0; I <Val. length; I ++) {128 system. out. println (Val [I]); 129} 130} 131}

Poi obtains the value of the first column in the first row of the 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.