-2

您能否提供从工作表中删除 Excel 表的 c# 代码。谢谢!

4

1 回答 1

0

这是删除所有工作表中所有表格的代码:

 using (SpreadsheetDocument xl = SpreadsheetDocument.Open(targetFile, true))
        {
            WorkbookPart workbookPart = xl.WorkbookPart;
            foreach (WorksheetPart sheet in workbookPart.WorksheetParts)
            {
                List<TableDefinitionPart> TableDefinitionPartToDelete = new List<TableDefinitionPart>();
                var TableParts = sheet.Worksheet.WorksheetPart.Worksheet.Descendants<TablePart>();

                List<TablePart> TablePartToDelete = new List<TablePart>();

                foreach (var Item in TableParts)
                {
                    TablePartToDelete.Add(Item);
                }
                foreach (var tp in TablePartToDelete)
                {
                    tp.Remove();
                }

                foreach (TableDefinitionPart Item in sheet.TableDefinitionParts)
                {
                    TableDefinitionPartToDelete.Add(Item);
                }

                foreach (TableDefinitionPart Item in TableDefinitionPartToDelete)
                {               
                    sheet.DeletePart(Item);
                }
            }

            xl.Close();
        }
于 2018-11-20T08:16:16.967 回答