How to iterate over columns to get row labeled data in Excel?
I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:
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
add a comment |
I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:
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
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 usingWorkbookFactory
to create your workbook, andDataFormatter
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 thei
). I don't exactly know what you mean byPOGO class
. Did you meanPOJO
?
– winklerrr
Nov 23 '15 at 20:33
add a comment |
I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:
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
I am writing a Java program in which I want to retrieve data which has row headers. My Excel sheet looks like below:
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
java excel apache-poi
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 usingWorkbookFactory
to create your workbook, andDataFormatter
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 thei
). I don't exactly know what you mean byPOGO class
. Did you meanPOJO
?
– winklerrr
Nov 23 '15 at 20:33
add a comment |
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 usingWorkbookFactory
to create your workbook, andDataFormatter
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 thei
). I don't exactly know what you mean byPOGO class
. Did you meanPOJO
?
– 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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
Try following
for (Cell cell : sheet.getRow(i )) {
//your logic
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '15 at 20:31
winklerrrwinklerrr
1,57411633
1,57411633
add a comment |
add a comment |
Try following
for (Cell cell : sheet.getRow(i )) {
//your logic
}
add a comment |
Try following
for (Cell cell : sheet.getRow(i )) {
//your logic
}
add a comment |
Try following
for (Cell cell : sheet.getRow(i )) {
//your logic
}
Try following
for (Cell cell : sheet.getRow(i )) {
//your logic
}
edited Nov 23 '18 at 6:42
Sankumarsingh
7,61084063
7,61084063
answered Nov 23 '18 at 5:49
Suresh GuptaSuresh Gupta
50754
50754
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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, andDataFormatter
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 byPOGO class
. Did you meanPOJO
?– winklerrr
Nov 23 '15 at 20:33