MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Обход объединенных ячеек в Word"
Answer 811478
Даже при сохранении в html-формате пишется кол-во объединенных колонок и строк.
Так может и воспользоваться этой деталью? Перегнать doc в html и распарсить атрибут colspan/rowspan
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; //Add Reference: Microsoft HTML Object Library namespace ConsoleApplication1 { class Program { public static List<string> FindMergedColumns(string html) { List<string> res = new List<string>(); mshtml.HTMLDocument doc = null; mshtml.IHTMLDocument2 d2 = null; mshtml.IHTMLDocument3 d = null; try { doc = new mshtml.HTMLDocument(); d2 = (mshtml.IHTMLDocument2)doc; d2.write(html); d2.close(); d = (mshtml.IHTMLDocument3)doc; var coll = d.getElementsByTagName("table"); object val; int numtable = 1; int row = 1, column = 1; int span; foreach (mshtml.IHTMLElement2 table in coll) { var rows = table.getElementsByTagName("tr"); foreach (mshtml.IHTMLElement2 tr in rows) { var cells = tr.getElementsByTagName("td"); foreach (mshtml.IHTMLElement td in cells) { val = td.getAttribute("colspan"); if(val == null)val = 0; span = Convert.ToInt32(val); if (span > 1) { res.Add(String.Format("Table {0}, Row {1}, Column {2}: {3} columns merged",numtable,row,column,span)); } column++; } row++; column = 1; } numtable++; row = 1; column = 1; } doc.close(); } finally { //освобождение ресурсов if (doc != null) Marshal.ReleaseComObject(doc); if (d2 != null) Marshal.ReleaseComObject(d2); if (d != null) Marshal.ReleaseComObject(d); } return res; } public static void Main(string[] argv) { var word = new Microsoft.Office.Interop.Word.Application(); object miss = System.Reflection.Missing.Value; object path = "c:\\test\\test.doc"; object readOnly = true; var docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); string tmp = Path.GetTempPath() + "file.htm"; //конвертируем doc в html docs.SaveAs(FileName: tmp, FileFormat: WdSaveFormat.wdFormatHTML); ((_Document)docs).Close(); ((_Application)word).Quit(); //парсим HTML string html = File.ReadAllText(tmp); var res = FindMergedColumns(html); File.Delete(tmp); foreach (var line in res) Console.WriteLine(line); Console.ReadKey(); } } }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.