public static List<FL> ImportDOC(object fileName,out StringBuilder meg) {
List<FL> flist = new List<FL>();
Application app = new Application();
Document doc = new Document();
meg = new StringBuilder();
try {
#region 导入word数据
//打开文件 if (app.Documents.Count > 0)
{ //if (MessageBox.Show("已经打开了一个word文档,你想关闭重新打开该文档吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
//{
// object unknow = Type.Missing;
// doc = app.ActiveDocument;
// if (MessageBox.Show("你想保存吗?", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
// {
// app.ActiveDocument.Save();
// }
// app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
// app.Visible = false;
//}
//else
//{
// return;
//}
}
else {
object unknow = Type.Missing;
app.Visible = true;
doc = app.Documents.Open(ref fileName,
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);
}
#region 读取数据
int docCount=doc.Paragraphs.Count;//world数据行数
if (docCount> 0) {
f f = new f();
for (int i = 1; i < docCount; i++) {
string linedate=doc.Paragraphs[i].Range.Text.Trim();
if (linedate.Contains("<MessageType>"))
{
if (!linedate.Contains("N")) {
meg.Append("文件类型不对");//文件类型不对不予理会
break;
}
}
if (!string.IsNullOrEmpty(getlinedata(linedate, "CallSign")))
{ f.FLIGHTID = getlinedata(linedate, "CallSign");
}
if (!string.IsNullOrEmpty(getlinedata(linedate, "ScheduleDate")))
{
f.SCHEDULEDATE =getlinedata(linedate, "ScheduleDate");
}
if (linedate.Contains("</Item>"))
{
flist.Add(f);
f = new f();
}
}
}
#endregion
//关闭文件
object unknow1 = Type.Missing;
object saveChanges1 = WdSaveOptions.wdPromptToSaveChanges;
app.ActiveDocument.Close(ref saveChanges1, ref unknow1, ref unknow1);
//关闭程序
object unknow2 = Type.Missing;
object saveChanges2 = WdSaveOptions.wdSaveChanges;
app.Quit(ref saveChanges2, ref unknow2, ref unknow2);
#endregion
}
catch (Exception e)
{
meg.Append(e.Message);
}
return flist;
//string text2 = Regex.Replace(wordDoc.Content.Text, @"(a|t|n|s+)", ""); //Regex.Replace(input, @"(a|t|n|s+)", "") }
public static string getlinedata(string linedate,string oldstr) { string str=string.Empty; try { if (linedate.Contains("<" + oldstr + ">")) { str = linedate.Replace("<"+oldstr+">", string.Empty).Replace("</"+oldstr+">", string.Empty); } } catch (Exception e) { } return str; } //public static void replaceChar() //{ // try // { // object replaceAll =WdReplace.wdReplaceAll; // object missing = Type.Missing;
// app.Selection.Find.ClearFormatting(); // app.Selection.Find.Text = "^l";
// app.Selection.Find.Replacement.ClearFormatting(); // app.Selection.Find.Replacement.Text = "^p";
// app.Selection.Find.Execute( // ref missing, ref missing, ref missing, ref missing, ref missing, // ref missing, ref missing, ref missing, ref missing, ref missing, // ref replaceAll, ref missing, ref missing, ref missing, ref missing); // } // catch (Exception e) // { // //MessageBox.Show("文档出现错误,请重新操作"); // } // }