OpenXML操作Excel创建和删除工作表

更新时间:2023-07-20 19:50:34 阅读: 评论:0

OpenXML操作excel创建和删除工作表
一、创建工作表
Open XML SDK 中,SpreadsheetDocument类表示 Excel 文档包。若要打开并使用 Excel 文档,要基于文档创建 SpreadsheetDocument 类的一个实例。调用 Open 方法之一。本示例代码使用带有需要两个参数的签名的 Open(String, Boolean) 方法。第一个参数采用表示要打开的文档的完整路径字符串。第二个参数是 true fal,如果此参数为true,表示是否要打开文件以进行编辑。如果此参数为 fal,则不会保存对该文档所做的任何更改。
下面的 using 语句中显示了调用 Open 方法的代码。
// Open the document for editing.
我不会忘记using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
拉市海湿地公园{
/
/ Inrt other code here.
}
using 语句提供典型 .Open, .Save, .Clo 序列的建议备选序列。它确保在遇到右大括号时会自动调用 Dispo 方法(Open XML SDK 用来清理资源的内部方法)。using 语句后面的块为 using 语句中创建或指定的对象设定范围,在此示例中这个范围就是 spreadsheet
SpreadsheetML 文档的基本文档结构由引用 Workbook中的工作表Sheets Sheet 元素组成。将为每个 Worksheet 创建单独的 XML 文件。
西红柿汤怎么做好吃以 SpreadsheetDocument 文档包形式打开文档进行编辑后,代码会使用 AddNewPart 方法向 WorkbookPart 对象中添加一个新 WorksheetPart 对象。然后,它向 WorksheetPart 对象中添加一个新 Worksheet 对象。
乖巧的意思
以下是使用 C# Visual Basic 编写的完整示例代码。
// Given a document name, inrts a new worksheet.
publicstaticvoid冬季养生茶 InrtWorksheet(string docName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
    {
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart=spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
  newWorksheetPart.Worksheet = newWorksheet(new SheetData());
  Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
土属性字
if (sheets.Elements<Sheet>().Count() > 0)
        {食品管理制度
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }
// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
// Append the new worksheet and associate it with the workbook.
Sheet sheet = newSheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
    }
}
二、删除工作表
public static bool XLDeleteSheet(string fileName, string sheetToDelete)
{
  bool returnValue = fal;
  using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, true))
  {
      XmlDocument doc = new XmlDocument();
      doc.Load(xlDoc.WorkbookPart.GetStream());
      XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
      nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI);
      string archString = string.Format("//d:sheet[@name='{0}']", sheetToDelete);
      XmlNode node = doc.SelectSingleNode(archString, nsManager);
      if (node != null)
      {
        XmlAttribute relationAttribute = node.Attributes["r:id"];
        if (relationAttribute != null)
        {
            string relId = relationAttribute.Value;
            xlDoc.WorkbookPart.DeletePart(relId);
            node.ParentNode.RemoveChild(node);
            doc.Save(xlDoc.WorkbookPart.GetStream(FileMode.Create));
            returnValue = true;
          }
      }
  }
  return returnValue;
}
在此程序中传递两个参数:工作薄的完整路径和要删除的工作表的名称。然后使用 SpreadsheetDocument 对象的 Open 方法,以 Open XML 包的形式打开输入文件。接着,
将工作薄中的内容载入 XML DOM 文档。接着,使用 XmlNamespaceManager 对象并通过 d 限制符设置对默认 SpreadsheetML 命名空间的引用,来设置命名空间管理器。然后使用 //d:sheet 节点的名称属性搜索指定工作表的文档。对于所有匹配的节点(如果存在),检索关系 Id 并删除与该 Id 对应的工作表。最后,将更新的 SpreadsheetML 标识保存回主工作薄部件。白贝

本文发布于:2023-07-20 19:50:34,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1107519.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文档   工作   参数   打开   指定
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图