Solved

Counter issue

Posted on 2006-11-13
7
272 Views
Last Modified: 2006-11-30
hi,

this class method writes row into excel file. Is there a way I can allow only to write 10 rows and ignore rest if any.?????

-----------------------------------------
........
public void addRow(Object[] theValues) throws Exception {
       
        try {
            mCurrentRow++;    
            mSheet.insertRow(mCurrentRow);
            for (int i = 0; i < theValues.length; i++) {
                Object v = theValues[i];
                if (v != null) {
                    WritableCell cell = new Label(i, mCurrentRow, v.toString());
                    mSheet.addCell(cell);
                }                
            }
        }
        catch (Exception e) {
           ...
        }        
    }
........
----------------------------------------------


I tried this but didnt worked......still wrote all rows.
while (counter < 10)
{
 try { ...}
}

thx.
PH
0
Comment
Question by:princehyderabad
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17935118
Might be

            for (int i = 0; i < theValues.length; i++) {
                if (i <= 10) {
                    Object v = theValues[i];
                    if (v != null) {
                        WritableCell cell = new Label(i, mCurrentRow, v.toString());
                        mSheet.addCell(cell);
                    }        
                }      
            }
0
 

Author Comment

by:princehyderabad
ID: 17935130
nope I already tried that as well didnt worked.

Someone wht the knowledge how xls file would be created using java will have good idea to solve this...
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17935250
Then can't help you much. Sorry.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17939215
for (int i = 0; i < theValues.length; i++) {
                    Object v = theValues[i];
                    if (v != null) {
                        WritableCell cell = new Label(i, mCurrentRow, v.toString());
                        mSheet.addCell(cell);
                    }        
                if(i==9)
       break;
            }
it will break on the 10th row after writing.

now you need to keep in mind that it will not stop my making extra cells in the excel sheet. ( its still gonna default to 65556 rows in Excel because thats what excel do when it creates a empty sheet)
0
 
LVL 12

Accepted Solution

by:
jessegivy earned 125 total points
ID: 17941359
       try {
            mCurrentRow++;    
            mSheet.insertRow(mCurrentRow);
            for (int i = 0; i < theValues.length && i<10; i++) {
                Object v = theValues[i];
                if (v != null) {
                    WritableCell cell = new Label(i, mCurrentRow, v.toString());
                    mSheet.addCell(cell);
                }                
            }
        }
        catch (Exception e) {
           ...
        }  

...the second argument of the for loop is a condition, you can specify additional conditions using && or ||

the break statement will work, but I find this solution a bit cleaner and may be ever so slightly beneficial for performance.

~Jesse
0
 

Author Comment

by:princehyderabad
ID: 17941541
I tried this and work. But tell me the most beneficial for performance wise.

 try {
            mCurrentRow++;    
if (mCurrentRow < 10)
            {
            mSheet.insertRow(mCurrentRow);
             for loop { ..}
            }//end if
        }
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 17963228
if it works, it works, you're talking about iterating ten times so performance isn't an issue, I believe including a second condition would slow slightly as opposed to iteration within because it's using an and statement which in essence is running two conditions simultainiously, and requires less code, how the processor interprets this code is dbatable, I doubt anyone knows?  Ultimatly though, this would only be a consideration if you were doing hundreds or thousands of iterations or calling the function that contains this loop quite a few times.

make sense?  Hope I didn't confuse, just showing a different, more condensed way to accomplish the same thing.

Cheers,

Jesse
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A Short Story about the Best File Recovery Software – Acronis True Image 2017
Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now