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.

@sstchur,
We would like to share that Aspose.Excel is no more under development and is discontinued. A new product Aspose.Cells has replaced it which supports all the features of its predecessor as well as contains all the latest features in different versions of MS Excel. You can perform number of operations on rows and columns using this new product like deleting a column as demonstrated in the following sample code:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);

// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

// Deleting a column from the worksheet at 5th position
worksheet.Cells.DeleteColumn(4);

// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");

// Closing the file stream to free all resources
fstream.Close();

Refer to the following link for more information about Inserting and Deleting rows and columns:
Inserting and Deleting Rows and Columns

Download the latest trail version here:
Aspose.Cells for .NET (Latest Version)

A comprehensive detailed solution is available here which contains hundreds of ready to run examples for testing different features of this product.