读取嵌入到word的Excel对象

时间:2024-07-05 18:05:44
Word.Document doc = this._wordApplication.Documents.Add(@"C:\Users\linmeicheng\Desktop\新建文件夹 (3)\Doc1.docx");
foreach (Word.ContentControl item in doc.ContentControls)
{
string title = item.Title;
// item.Appearance = WdContentControlAppearance.wdContentControlHidden; System.Diagnostics.Debug.WriteLine("标题:"+ title);
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.LoadXml(item.Range.WordOpenXML); System.Xml.XmlNamespaceManager manager = new System.Xml.XmlNamespaceManager(xmldoc.NameTable);
manager.AddNamespace("pkg", "http://schemas.microsoft.com/office/2006/xmlPackage"); var aa= xmldoc.SelectSingleNode("//pkg:package/pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']/pkg:binaryData", manager);
if (aa == null)
continue; //NetOffice.ExcelApi.Application excel = new NetOffice.ExcelApi.Application(); Stream stream = new MemoryStream(Convert.FromBase64String(aa.InnerText)); ExcelUntity excel = new ExcelUntity();
SpreadsheetDocument spreadsheetDocument = excel.Open(stream, false); SheetData sheetData = spreadsheetDocument.GetFirstSheetData();
SharedStringTablePart sharedStringTablePart = spreadsheetDocument.GetSharedStringTable().FirstOrDefault(); foreach(Row row in sheetData)
{
if (row.RowIndex == )
continue; string bb = row.GetCellValue("B"+row.RowIndex.ToString(), sharedStringTablePart);
string cc = row.GetCellValue("C" + row.RowIndex.ToString(), sharedStringTablePart); System.Diagnostics.Debug.WriteLine("第" + row.RowIndex.ToString()+"行,B列:"+bb+" C列:"+cc);
}
spreadsheetDocument.Close();
}