zoukankan      html  css  js  c++  java
  • 导入word数据

     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("文档出现错误,请重新操作");        //     }        // }

  • 相关阅读:
    Python列表去重的三种方法
    关于Python的 a, b = b, a+b
    Python爬取B站视频信息
    Linux文件管理命令
    (一)MySQL学习笔记
    Linux特殊字符含义
    在父容器div中图片下方有一条空隙问题
    对Json的各种遍历方法
    for循环使用append问题
    IE兼容性
  • 原文地址:https://www.cnblogs.com/Ilin631/p/3370158.html
Copyright © 2011-2022 走看看