How to delete Excel table rows and columns in C#, VB.NET?
C#
private void DeleteTableRow(IListObject table, int rowNumber)
{
IRange tableRange = table.Location;
IWorksheet worksheet = table.Worksheet;
int firstTableColumn = tableRange.Column;
int lastTableColumn = tableRange.LastColumn;
int firstTableRow = tableRange.Row;
int lastTableRow = tableRange.LastRow;
table.Worksheet[firstTableRow + rowNumber, firstTableColumn, firstTableRow + rowNumber, lastTableColumn].Clear(ExcelClearOptions.ClearContent);
if (firstTableRow + rowNumber < tableRange.LastRow)
{
for (int i = firstTableRow + rowNumber + 1; i <= worksheet.Range.LastRow; i++)
{
worksheet.Range[i, firstTableColumn, i, lastTableColumn].MoveTo(worksheet.Range[i - 1, firstTableColumn, i - 1, lastTableColumn]);
}
}
table.Location = table.Location[firstTableRow, firstTableColumn, lastTableRow - 1, lastTableColumn];
for (int i = 0; i < worksheet.ListObjects.Count; i++)
{
IListObject tableSample = worksheet.ListObjects[i];
int firstrow = tableSample.Location.Row;
int firstcolumn = tableSample.Location.Column;
if (table.Location.LastRow < firstrow && table.Location.LastColumn >= firstcolumn)
{
tableSample.Location = table.Location[firstrow - 1, firstcolumn, tableSample.Location.LastRow - 1, tableSample.Location.LastColumn];
}
}
}
C#
private void DeleteTableColumn(IListObject table, int columnNumber)
{
IRange tableRange = table.Location;
IWorksheet worksheet = table.Worksheet;
int firstTableColumn = tableRange.Column;
int lastTableColumn = tableRange.LastColumn;
int firstTableRow = tableRange.Row;
int lastTableRow = tableRange.LastRow;
table.Worksheet[firstTableRow, firstTableColumn + columnNumber, lastTableRow, firstTableColumn + columnNumber].Clear(ExcelClearOptions.ClearContent);
if (firstTableColumn + columnNumber < tableRange.LastColumn)
{
for (int i = firstTableColumn + columnNumber + 1; i <= worksheet.Range.LastColumn; i++)
{
worksheet.Range[firstTableRow, i, lastTableRow, i].MoveTo(worksheet.Range[firstTableRow, i - 1, lastTableRow, i - 1]);
}
}
table.Location = table.Location[firstTableRow, firstTableColumn, lastTableRow, lastTableColumn - 1];
for (int i = 0; i < worksheet.ListObjects.Count; i++)
{
IListObject tableSample = worksheet.ListObjects[i];
int lastrow = tableSample.Location.LastRow;
int firstcolumn = tableSample.Location.Column;
if (table.Location.LastColumn < firstcolumn && table.Location.LastRow >= lastrow)
{
tableSample.Location = table.Location[tableSample.Location.Row, firstcolumn - 1, lastrow, tableSample.Location.LastColumn - 1];
}
}
}
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
IListObject table = worksheet.ListObjects[0];
Program prgm = new Program();
//Delete table column
table.Columns.RemoveAt(2);
prgm.DeleteTableColumn(table, 2);
//Delete table row
prgm.DeleteTableRow(table, 4);
workbook.SaveAs("Output.xlsx");
System.Diagnostics.Process.Start("Output.xlsx");
}
Take a moment to
peruse the documentation
Click here
Note:
Conclusion
You can refer to our
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.