Solved

Counter issue

Posted on 2006-11-13
7
275 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object methods vs static methods 2 100
report generation frameworks 1 69
I am having a difficulty creating a responsive Page for my website. 10 87
spring example non maven 4 92
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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