c#读取 word 内容

it2025-07-07  9

using Microsoft.Office.Interop.Word; /// 截取字符串中开始和结束字符串中间的字符串 /// </summary> /// <param name="source">源字符串</param> /// <param name="startStr">开始字符串</param> /// <param name="endStr">结束字符串</param> /// <returns>中间字符串</returns> public string SubstringSingle(string source, string startStr, string endStr) { Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline); return rg.Match(source).Value; } private void button2_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; string foldPath; if (dialog.ShowDialog() == DialogResult.OK) { foldPath = dialog.SelectedPath; //MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information); List<String> listDoc = new List<string>(); List<String> listExcel = new List<string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(foldPath); FileInfo[] thefileInfo = theFolder.GetFiles("*.doc", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 { listDoc.Add(NextFile.FullName); } foreach (string item in listDoc) { if (!Directory.Exists(foldPath + "\\bak")) { Directory.CreateDirectory(foldPath + "\\bak"); } string text = ""; //全文文本 try { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();//打开word程序 Microsoft.Office.Interop.Word.Document doc = null;//实例化一个新的word文档 object unknow = Type.Missing; app.Visible = false; //object paramSourceDocPath = "G:\\work\\20201021\\5\\1_2.doc"; object paramSourceDocPath = item; doc = app.Documents.Open(ref paramSourceDocPath, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow); if (doc != null) { text = doc.Content.Text.Trim();//将全篇内容存入字符串中 doc.Close(ref unknow, ref unknow, ref unknow); string temp = SubstringSingle(text, "地籍号:", "权利人"); string temp3 = temp.Replace("\r\r", ""); string temp1 = SubstringSingle(text, "身份证号", "身份证号"); string temp2 = temp1.Replace("\r\a", ""); string path1 = item; string path2 = foldPath + "\\bak\\" + temp3 + "_" + temp2 +".doc"; File.Copy(path1, path2); } } catch (Exception) { } } MessageBox.Show("处理完成!"); }
最新回复(0)