利用OpenXML获取Excel单元格的内容.doc_第1页
利用OpenXML获取Excel单元格的内容.doc_第2页
利用OpenXML获取Excel单元格的内容.doc_第3页
利用OpenXML获取Excel单元格的内容.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

利用OpenXML查询Excel单元格的内容在此程序中传递三个参数:工作薄的完整路径、工作表的名称和包含要检索值的单元格地址。然后使用 SpreadsheetDocument 对象的 Open 方法,以 Open XML 包的形式打开输入文件,并将数据载入一个 XML 文档。接着,使用 XmlNamespaceManager 对象并通过 d 限制符设置对默认 worksheetSchema 命名空间的引用,通过 s 限制符设置对 sharedStringSchema 的引用,来设置命名空间管理器。sharedStringSchema 命名空间引用 SharedStringTablePart 部件,该部件包含在多个单元格内共享的字符串。然后,通过选择 /d:sheet 节点的名称属性,在主工作簿部件中检索代表指定工作表的节点。如果找到节点,则检索工作表的关系 Id,并用其将工作表载入 XML 文档。然后检索值。在节点中,如果 t 属性包含 s,则表示是一个共享的字符串,必须在 SharedStringTablePart 部件中查找。否则,就可以直接从节点检索值。注意:此代码只单独检查布尔和字符串值。最后,程序返回单元格的值或一个布尔值,指定搜索是否成功。以下是程序代码:C#public static string XLGetCellValue(string fileName, string sheetName, string addressName) const string worksheetSchema = /spreadsheetml/2006/main; const string sharedStringSchema = /spreadsheetml/2006/main; string cellValue = null; / Retrieve the stream containing the requested / worksheets info. using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, false) / Get the main document part (workbook.xml). XmlDocument doc = new XmlDocument(); doc.Load(xlDoc.WorkbookPart.GetStream(); / Create a namespace manager, so you can search. / Add a prefix (d) for the default namespace. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace(d, worksheetSchema); nsManager.AddNamespace(s, sharedStringSchema); string searchString = string.Format(/d:sheetname=0, sheetName); XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager); if (sheetNode != null) / Get the relId attribute. XmlAttribute relationAttribute = sheetNode.Attributesr:id; if (relationAttribute != null) string relId = relationAttribute.Value; / Load the contents of the workbook. XmlDocument sheetDoc = new XmlDocument(nt); sheetDoc.Load(xlDoc.WorkbookPart.GetPartById(relId).GetStream(); XmlNode cellNode = sheetDoc.SelectSingleNode(string.Format(/d:sheetData/d:row/d:cr=0, addressName), nsManager); if (cellNode != null) XmlAttribute typeAttr = cellNode.Attributest; string cellType = string.Empty; if (typeAttr != null) cellType = typeAttr.Value; XmlNode valueNode = cellNode.SelectSingleNode(d:v, nsManager); if (valueNode != null) cellValue = valueNode.InnerText; if (cellType = b) if (cellValue = 1) cellValue = TRUE; else cellValue = FALSE; else if (cellType = s) if (xlDoc.WorkbookPart.SharedStringTablePart != null) XmlDocument stringDoc = new XmlDocument(nt); stringDoc.Load(xlDoc.WorkbookPart.SharedStringTablePart.GetStream(); / Add the string schema to the namespace manager. nsManager.AddNamespace(s, sharedStringSchema); int requestedString = Convert.ToInt32(cellValue); string strSearch = string.Format(/s:sst/s:si0, requestedString + 1); XmlN

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论