static void Main(string[] args) { string folderName = @"d:\1\"; //место для сохранения xml по умолчанию string DocmFileName; string DocmFilePath; Console.WriteLine("Enter name of docm file: "); DocmFileName = Console.ReadLine(); DocmFilePath = folderName + DocmFileName + ".docm"; string XmlFilePath = System.IO.Path.Combine(folderName, (DocmFileName+".xml")); // string DocmFilePath = System.IO.Path.Combine(folderName, DocmFileName); StringBuilder str = new StringBuilder(); DocBook db = new DocBook(); Docm dm = new Docm(); Stopwatch sWatch = new Stopwatch(); sWatch.Start(); string st1 = dm.ReadDocmDocument(DocmFilePath, db); str.Append(st1); // Console.WriteLine(str.ToString()); // Console.WriteLine(str.Length); db.createXML(XmlFilePath, str); sWatch.Stop(); Console.WriteLine("Parse time (milliseconds): " + sWatch.ElapsedMilliseconds.ToString()); // Console.WriteLine(db.ReadXmlDocument(XmlFilePath)); // WriteDocmDocument(); Console.ReadKey(); }
public string ReadDocmDocument(string DocmFilePath, DocBook db) { StringBuilder sb = new StringBuilder(); WordprocessingDocument package = WordprocessingDocument.Open(DocmFilePath, true); // Open a WordprocessingDocument for editing using the DocmFilePath. OpenXmlElement element = package.MainDocumentPart.Document.Body; if (element == null) { return string.Empty; } sb.Append(GetText(element, db)); package.Close(); return sb.ToString(); }
public string GetText(OpenXmlElement element, DocBook db) { StringBuilder PlainTextInWord = new StringBuilder(); foreach (OpenXmlElement section in element.Elements()) { switch (section.LocalName) { // Text case "t": PlainTextInWord.Append(section.InnerText); // db.addPara(PlainTextInWord, "t"); break; case "cr": // Carriage return case "br": // Page break PlainTextInWord.Append(Environment.NewLine); //db.addPara(PlainTextInWord, "br"); break; // Tab case "tab": PlainTextInWord.Append("\t"); break; // Paragraph case "p": PlainTextInWord.Append(GetText(section,db)); PlainTextInWord.AppendLine(Environment.NewLine); break; default: PlainTextInWord.Append(GetText(section,db)); break; } } return PlainTextInWord.ToString(); }