Free Support Forum - aspose.com

Deleting Columns From a Worksheet

I have a function that removes certain columns from my worksheet based on some set of criteria. My problem is that I ALWAYS end up with one extra blank column left over.

Here is the code:

private void RemoveBogusColumnsFromWorksheet(Worksheet _wksht)
{
byte nMaxColumn = _wksht.Cells.MaxColumn;
for (int i = nMaxColumn; i >=0; i–)
{
string colName = _wksht.Cells[0, (byte)i].StringValue;
if ( /* some condition holds true */ )
_wksht.Cells.DeleteColumn((byte)i);
}

for (int i = 0; i <= _wksht.Cells.MaxColumn; i++)
Response.Write(i.ToString() + ": " + _wksht.Cells[0, (byte)i].StringValue + “
”);
}

If I run this function, it removes all the columns that meet my IF condition, but I always end up with one extra blank column. Any idea why?

-Steve

Hi Steve,

Do you set some formatting on your extra blank column? MaxColumn property return the max column with data or formatting. Please check it.

Laurence,

No there is extra formatting on the columns. It just seems that whenever I delete a column, I end up with one extra blank column at the end.

-Steve

Hi Steve,

Yes. That’s the routine of MS Excel to delete a column. Does that trouble you?

Yes,

It’s causing me a problem, b/c I’m ending up with a blank column (that has no header value), but MaxColumn is indicating that there is data in that column. When I try to access it, I run into a problem.

-Steve

Hi Steve,

I am still not sure about your problem. Could you send me your template file and more of your code? Thanks.