How to iterate over columns to get row labeled data in Excel?












2















I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:



enter image description here



I have written a following simple Java code to retrieve values sequentially based on row headers:



....
try
{
fileSystem = new POIFSFileSystem (inpStrm);

HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
int column = 1;
for(int i = 0;i <=3;i++){

for(int j = 0;j<=3;j++){
HSSFRow row = sheet.getRow(j);
System.out.print(row.getCell(column).getStringCellValue() + " ");
}
System.out.println("");
column++;
}
}
catch(Exception e){
e.printStackTrace();
}
....


And after running code my output comes like below:



P1    M    C1    Hyderabad    
P2 M C2 Pune
P3 F C3 Pune
P4 M C4 Hyderabad


Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?



If I use the POGO class having above four properties then how can I get list of objects with values from Excel?










share|improve this question

























  • Just loop over all the rows? I'm not seeing what the problem is with your code?

    – Gagravarr
    Nov 19 '15 at 19:03











  • @Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

    – Madhusudan
    Nov 20 '15 at 9:38








  • 1





    I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

    – Gagravarr
    Nov 20 '15 at 9:57











  • You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

    – winklerrr
    Nov 23 '15 at 20:33
















2















I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:



enter image description here



I have written a following simple Java code to retrieve values sequentially based on row headers:



....
try
{
fileSystem = new POIFSFileSystem (inpStrm);

HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
int column = 1;
for(int i = 0;i <=3;i++){

for(int j = 0;j<=3;j++){
HSSFRow row = sheet.getRow(j);
System.out.print(row.getCell(column).getStringCellValue() + " ");
}
System.out.println("");
column++;
}
}
catch(Exception e){
e.printStackTrace();
}
....


And after running code my output comes like below:



P1    M    C1    Hyderabad    
P2 M C2 Pune
P3 F C3 Pune
P4 M C4 Hyderabad


Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?



If I use the POGO class having above four properties then how can I get list of objects with values from Excel?










share|improve this question

























  • Just loop over all the rows? I'm not seeing what the problem is with your code?

    – Gagravarr
    Nov 19 '15 at 19:03











  • @Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

    – Madhusudan
    Nov 20 '15 at 9:38








  • 1





    I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

    – Gagravarr
    Nov 20 '15 at 9:57











  • You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

    – winklerrr
    Nov 23 '15 at 20:33














2












2








2


1






I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:



enter image description here



I have written a following simple Java code to retrieve values sequentially based on row headers:



....
try
{
fileSystem = new POIFSFileSystem (inpStrm);

HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
int column = 1;
for(int i = 0;i <=3;i++){

for(int j = 0;j<=3;j++){
HSSFRow row = sheet.getRow(j);
System.out.print(row.getCell(column).getStringCellValue() + " ");
}
System.out.println("");
column++;
}
}
catch(Exception e){
e.printStackTrace();
}
....


And after running code my output comes like below:



P1    M    C1    Hyderabad    
P2 M C2 Pune
P3 F C3 Pune
P4 M C4 Hyderabad


Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?



If I use the POGO class having above four properties then how can I get list of objects with values from Excel?










share|improve this question
















I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:



enter image description here



I have written a following simple Java code to retrieve values sequentially based on row headers:



....
try
{
fileSystem = new POIFSFileSystem (inpStrm);

HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
int column = 1;
for(int i = 0;i <=3;i++){

for(int j = 0;j<=3;j++){
HSSFRow row = sheet.getRow(j);
System.out.print(row.getCell(column).getStringCellValue() + " ");
}
System.out.println("");
column++;
}
}
catch(Exception e){
e.printStackTrace();
}
....


And after running code my output comes like below:



P1    M    C1    Hyderabad    
P2 M C2 Pune
P3 F C3 Pune
P4 M C4 Hyderabad


Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?



If I use the POGO class having above four properties then how can I get list of objects with values from Excel?







java excel apache-poi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '15 at 0:35









pnuts

48.7k76297




48.7k76297










asked Nov 19 '15 at 16:10









MadhusudanMadhusudan

1,59962861




1,59962861













  • Just loop over all the rows? I'm not seeing what the problem is with your code?

    – Gagravarr
    Nov 19 '15 at 19:03











  • @Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

    – Madhusudan
    Nov 20 '15 at 9:38








  • 1





    I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

    – Gagravarr
    Nov 20 '15 at 9:57











  • You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

    – winklerrr
    Nov 23 '15 at 20:33



















  • Just loop over all the rows? I'm not seeing what the problem is with your code?

    – Gagravarr
    Nov 19 '15 at 19:03











  • @Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

    – Madhusudan
    Nov 20 '15 at 9:38








  • 1





    I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

    – Gagravarr
    Nov 20 '15 at 9:57











  • You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

    – winklerrr
    Nov 23 '15 at 20:33

















Just loop over all the rows? I'm not seeing what the problem is with your code?

– Gagravarr
Nov 19 '15 at 19:03





Just loop over all the rows? I'm not seeing what the problem is with your code?

– Gagravarr
Nov 19 '15 at 19:03













@Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

– Madhusudan
Nov 20 '15 at 9:38







@Gagravarr Thanks for your reply :). There is no problem in my code. I am asking if there is any other easy way by which I can achieve the same requirement?

– Madhusudan
Nov 20 '15 at 9:38






1




1





I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

– Gagravarr
Nov 20 '15 at 9:57





I'd suggest using WorkbookFactory to create your workbook, and DataFormatter to turn the cell into a string, but otherwise it looks pretty simple and easy to me!

– Gagravarr
Nov 20 '15 at 9:57













You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

– winklerrr
Nov 23 '15 at 20:33





You could remove that unused for loop (the one with the i). I don't exactly know what you mean by POGO class. Did you mean POJO?

– winklerrr
Nov 23 '15 at 20:33












2 Answers
2






active

oldest

votes


















4















Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?




You could use the method sheet.getLastRowNum() as condition in your for loop to iterate over all rows and the method row.getLastCellNum() in a second loop to iterate over all cells in a specific row.



for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
HSSFRow row = sheet.getRow(rowNumber);

for(int columnNumber = 0; columnNumber < row.getLastCellNum(); columnNumber++) {
HSSFCell cell = row.getCell(columnNumber);
if(cell != null) {
// do something with the cell
}
}


Important: The code above loops first through each column and then through each row.






share|improve this answer































    0














    Try following



    for (Cell cell : sheet.getRow(i )) {

    //your logic
    }





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33808926%2fhow-to-iterate-over-columns-to-get-row-labeled-data-in-excel%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4















      Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?




      You could use the method sheet.getLastRowNum() as condition in your for loop to iterate over all rows and the method row.getLastCellNum() in a second loop to iterate over all cells in a specific row.



      for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
      HSSFRow row = sheet.getRow(rowNumber);

      for(int columnNumber = 0; columnNumber < row.getLastCellNum(); columnNumber++) {
      HSSFCell cell = row.getCell(columnNumber);
      if(cell != null) {
      // do something with the cell
      }
      }


      Important: The code above loops first through each column and then through each row.






      share|improve this answer




























        4















        Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?




        You could use the method sheet.getLastRowNum() as condition in your for loop to iterate over all rows and the method row.getLastCellNum() in a second loop to iterate over all cells in a specific row.



        for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
        HSSFRow row = sheet.getRow(rowNumber);

        for(int columnNumber = 0; columnNumber < row.getLastCellNum(); columnNumber++) {
        HSSFCell cell = row.getCell(columnNumber);
        if(cell != null) {
        // do something with the cell
        }
        }


        Important: The code above loops first through each column and then through each row.






        share|improve this answer


























          4












          4








          4








          Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?




          You could use the method sheet.getLastRowNum() as condition in your for loop to iterate over all rows and the method row.getLastCellNum() in a second loop to iterate over all cells in a specific row.



          for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
          HSSFRow row = sheet.getRow(rowNumber);

          for(int columnNumber = 0; columnNumber < row.getLastCellNum(); columnNumber++) {
          HSSFCell cell = row.getCell(columnNumber);
          if(cell != null) {
          // do something with the cell
          }
          }


          Important: The code above loops first through each column and then through each row.






          share|improve this answer














          Now is there any easy and feasible way to do the same for large Excel sheets (50 row headers)?




          You could use the method sheet.getLastRowNum() as condition in your for loop to iterate over all rows and the method row.getLastCellNum() in a second loop to iterate over all cells in a specific row.



          for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
          HSSFRow row = sheet.getRow(rowNumber);

          for(int columnNumber = 0; columnNumber < row.getLastCellNum(); columnNumber++) {
          HSSFCell cell = row.getCell(columnNumber);
          if(cell != null) {
          // do something with the cell
          }
          }


          Important: The code above loops first through each column and then through each row.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '15 at 20:31









          winklerrrwinklerrr

          1,57411633




          1,57411633

























              0














              Try following



              for (Cell cell : sheet.getRow(i )) {

              //your logic
              }





              share|improve this answer






























                0














                Try following



                for (Cell cell : sheet.getRow(i )) {

                //your logic
                }





                share|improve this answer




























                  0












                  0








                  0







                  Try following



                  for (Cell cell : sheet.getRow(i )) {

                  //your logic
                  }





                  share|improve this answer















                  Try following



                  for (Cell cell : sheet.getRow(i )) {

                  //your logic
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 6:42









                  Sankumarsingh

                  7,61084063




                  7,61084063










                  answered Nov 23 '18 at 5:49









                  Suresh GuptaSuresh Gupta

                  50754




                  50754






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33808926%2fhow-to-iterate-over-columns-to-get-row-labeled-data-in-excel%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin