//第二篇 public string[] ReadList2() { string fileName = @"C:\Users\Administrator.MICROSO-IJAJHM4\Desktop\github\C#_work\CSharp\test3\国考_标准答案2.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, false)) { // Create a Body object. DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; List <string> list2 = new List <string>(); foreach (var g in body.Elements()) { list2.Add(g.InnerText); } //初始化一个字符数组 char[] ch = new char[list2.Count]; //将list转化为字符串数组 string[] str = list2.ToArray(); //调用函数转化为字符数组 Operation CH = new Operation(); ch = CH.OperationCh(str, list2.Count); //再转换为string[] string[] s = new string[ch.Length]; //调用函数 s = CH.changeStr(ch); return(s); } }
//第一位 public string[] readList2() { string fileName = @"D:\Program Files\VS2017_workplace\test4\file_test4\students_answer\1001_张三.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, false)) { // Create a Body object. DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; List <string> list2 = new List <string>(); foreach (var g in body.Elements()) { list2.Add(g.InnerText); } //初始化一个字符数组 char[] chArr = new char[list2.Count]; //将list转化为字符串数组 string[] str = list2.ToArray(); //调用函数转化为字符数组 Change CH = new Change(); chArr = CH.changeCh(str, list2.Count); //再转换为string[] string[] s = new string[chArr.Length]; //调用函数 s = CH.changeStr(chArr); return(s); } }
//第一篇 public string[] readList1() { string fileName = @"F:\国考_标准答案1.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, false)) { // Create a Body object. DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; List <string> list1 = new List <string>(); foreach (var g in body.Elements()) { list1.Add(g.InnerText); } //初始化一个字符数组 char[] chArr = new char[list1.Count]; //将list转化为字符串数组 string[] str = list1.ToArray(); //调用函数转化为字符数组 Change CH = new Change(); chArr = CH.changeCh(str, list1.Count); //再转换为string[] string[] s = new string[chArr.Length]; //调用函数 s = CH.changeStr(chArr); return(s); } }
//第三位 public string[] readList4() { string fileName = @"C:\Users\lenovo\Desktop\18wang\CSharp\test4\file_test4\students_answer\1003_王五.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, false)) { // Create a Body object. DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; List <string> list4 = new List <string>(); foreach (var g in body.Elements()) { list4.Add(g.InnerText); } //初始化一个字符数组 char[] chArr = new char[list4.Count]; //将list转化为字符串数组 string[] str = list4.ToArray(); //调用函数转化为字符数组 Change CH = new Change(); chArr = CH.changeCh(str, list4.Count); //再转换为string[] string[] s = new string[chArr.Length]; //调用函数 s = CH.changeStr(chArr); return(s); } }
private void MatchFont2() { string s = System.IO.Directory.GetCurrentDirectory(); workingDoc.SaveAs("C:/Users/minad/AppData/Local/WordTestData/tmp.docx"); DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = null; try { doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open( "C:/Users/minad/AppData/Local/WordTestData/con_Rong_chau_Tien.docx", false); } catch (DocumentFormat.OpenXml.Packaging.OpenXmlPackageException) { return; } catch (System.IO.IOException) { return; } DocumentFormat.OpenXml.Wordprocessing.Body body = doc.MainDocumentPart.Document.Body; //int idx = -1; foreach (DocumentFormat.OpenXml.Wordprocessing.Paragraph p in body.ChildElements.OfType <DocumentFormat.OpenXml.Wordprocessing.Paragraph>()) { DocumentFormat.OpenXml.Wordprocessing.Run r = p.ChildElements.First <DocumentFormat.OpenXml.Wordprocessing.Run>(); while (r != null) { Console.WriteLine(r.InnerText); r = r.NextSibling <DocumentFormat.OpenXml.Wordprocessing.Run>(); } } doc.Close(); }
public ReadWord(string filename) { try { using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filename, true)) { // 获取主体 DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // 获取所有元素 IEnumerable <DocumentFormat.OpenXml.OpenXmlElement> elements = body.Elements <DocumentFormat.OpenXml.OpenXmlElement>(); //遍历元素列表,输出内容 foreach (DocumentFormat.OpenXml.OpenXmlElement element in elements) { //Text += element.InnerText+ "\r\n"; //Console.WriteLine(element.InnerText); strList += element.InnerText.ToString(); TextList.Add(element.InnerText); } } } catch { Console.WriteLine("没有找到文件"); Console.ReadKey(); } }
private string ExtractTextFromDoc(string srcFilePath) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(srcFilePath, false)) { DocumentFormat.OpenXml.Wordprocessing.Body body = wordDoc.MainDocumentPart.Document.Body; return(body.InnerText); } }
private string ReadWordDocument(IFormFile fileForUpload) { string contents = ""; WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileForUpload.OpenReadStream(), false); DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; contents = body.InnerText; return(contents.Trim()); }
public static string GetDocumentBodyAsPlainText(string document) { string content = ""; using (WordprocessingDocument file = WordprocessingDocument.Open(document, true)) { DocumentFormat.OpenXml.Wordprocessing.Body body = file.MainDocumentPart.Document.Body; content = body.InnerText; } return(content); }
public override void SearchTextInFile() { if (!File.Exists(FilePath)) { Logger.Error("DocSearch::SearchTextInFile - File does not exist. Aborting search!"); return; } using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(FilePath, false)) { DocumentFormat.OpenXml.Wordprocessing.Body body = wordDoc.MainDocumentPart.Document.Body; // Search for patterns and generate report. if (SearchTexts.Count > 0) { Dictionary <string, int> dSearchPatterns = new Dictionary <string, int>(); foreach (string searchText in SearchTexts) { dSearchPatterns.Add(searchText, Regex.Matches(body.InnerText, searchText).Count); } //GenerateSearchReport(dSearchPatterns, 1); } // Search for exceptions and generate report if they occur. if (Exceptions.Count > 0) { Dictionary <string, int> dExceptions = new Dictionary <string, int>(); int count = 0; bool frequency = false; foreach (string exception in Exceptions) { count = Regex.Matches(body.InnerText, exception).Count; dExceptions.Add(exception, count); if (count > 0) { frequency = true; } } // Check if atleast one from the exception set occurs in the file. If true then // generate exception report. if (true == frequency) { //GenerateExceptionReport(dExceptions); //SearchExceptionReportBuilder(dExceptions); } } } }
public void read() { string fileName = @"D:\Program Files\VS2017_workplace\C#_Git_Work\CSharp\test2\科研细则.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, false)) { // Create a Body object. DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Get the Run elements after the specified element. Console.WriteLine("Run elements after the first child are: "); foreach (var paragraph in body.Elements()) { Console.WriteLine(paragraph.InnerText); } Console.ReadKey(); } }