using Microsoft
.Office
.Interop
.Word
;
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
;
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();
Microsoft.Office.Interop.Word.Document doc
= null;
object unknow
= Type
.Missing
;
app
.Visible
= false;
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("处理完成!");
}
转载请注明原文地址: https://lol.8miu.com/read-27212.html