Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

Counter issue

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
Avatar of suprapto45
suprapto45
Flag of Singapore image

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);
                    }        
                }      
            }
Avatar of princehyderabad
princehyderabad

ASKER

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...
Then can't help you much. Sorry.
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)
ASKER CERTIFIED SOLUTION
Avatar of jessegivy
jessegivy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
        }
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