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(mCurrentR ow);
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
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(mCurrentR
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
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...
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried this and work. But tell me the most beneficial for performance wise.
try {
mCurrentRow++;
if (mCurrentRow < 10)
{
mSheet.insertRow(mCurrentR ow);
for loop { ..}
}//end if
}
try {
mCurrentRow++;
if (mCurrentRow < 10)
{
mSheet.insertRow(mCurrentR
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
make sense? Hope I didn't confuse, just showing a different, more condensed way to accomplish the same thing.
Cheers,
Jesse
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);
}
}
}