我正在用工作表填寫我的工作簿,而我無法弄清爲什麼空工作表出現在我的工作簿中。如何從C中的工作簿中刪除空工作表#
- 我需要刪除空表 或
- 確定爲什麼創建空表。
excelApp.DisplayAlerts = false;
workBook = (Microsoft.Office.Interop.Excel._Workbook)(excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet));
//DataTable to Excel
foreach(DataTable table in dataSet.Tables)
{
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workSheet.Name = table.TableName;
for (int i = 0; i < table.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = table.Columns[i].ColumnName;
}
for (int i = 0; i < table.Rows.Count; i++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
workSheet.Cells[(i + 2), (k + 1)] = table.Rows[i][k];
}
}
workBook.Worksheets.Add(workSheet);
}
//Trying to delete empty sheets
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in workBook.Sheets)
{
excelApp.DisplayAlerts = false;
if (sheet.Cells.MaxRow == -1 && sheet.Cells.MaxColumn == -1)
{
sheet.Delete();
}
}
無法刪除空單元格只是把一些警告框 – jose
我不想刪除空單元格,我想從工作簿中刪除所有的空表。 –
,因爲你在開始和循環結尾添加'.Worksheets.Add' – Slai