Solved

Counter issue

Posted on 2006-11-13
7
273 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JTable - RowFilter & Columns Header. 3 140
J2SE 5.0 metadata 1 51
spring example errors 33 251
HTTPSessionBindingListernter not configured in deployment descriptor 2 33
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

12 Experts available now in Live!

Get 1:1 Help Now