Exemple #1
0
        private void больничныеЛистыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            kadr_rab_time examp = new kadr_rab_time();

            examp.Show();
            this.Close();
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
        public void close()
        {
            // 指定文档的保存操作。可以是下列 WdSaveOptions 值之一:
            // wdDoNotSaveChanges、wdPromptToSaveChanges 或 wdSaveChanges。
            // Object SaveChanges = Word.WdSaveOptions.wdSaveChanges;
            Object DoNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;

            // 指定文档的保存格式。可以是下列 WdOriginalFormat 值之一:
            // wdOriginalDocumentFormat、wdPromptUser 或 wdWordDocument。
            Object OriginalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;

            // 如果为 true,则将文档传送给下一个收件人。
            // 如果没有为文档附加传送名单,则忽略此参数。
            Object RouteDocument = false;


            try
            {
                if (wDoc != null)
                {
                    wDoc.Close(ref DoNotSaveChanges, ref OriginalFormat, ref RouteDocument);
                }
                if (wApp != null)
                {
                    wApp.Quit(ref DoNotSaveChanges, ref OriginalFormat, ref RouteDocument);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #3
0
        public void Init(string templateParh = "", string hash = "")
        {
            application = new Word.Application();
            object templatePathObj = templateParh;

            try
            {
                if (templateParh == String.Empty)
                {
                    document = application.Documents.Add(ref missingObj, ref missingObj, ref missingObj, ref missingObj);
                }
                else
                {
                    templatePathObj = AppDomain.CurrentDomain.BaseDirectory + "\\reports\\" + templatePathObj;
                    document        = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj);
                }
            }
            catch (Exception)
            {
                document.Close(ref falseObj, ref missingObj, ref missingObj);
                application.Quit(ref missingObj, ref missingObj, ref missingObj);
                document    = null;
                application = null;
                throw;
            }
        }
Exemple #4
0
        public void CreateNewDoc(String path, AppVisibility visibility)
        {
            //создаем обьект приложения word
            _application = new Word.Application();
            // создаем путь к файлу
            Object templatePathObj = path;

            // если вылетим на этом этапе, приложение останется открытым
            try
            {
                _document = _application.Documents.Add(ref templatePathObj, ref _missingObj, ref _missingObj, ref _missingObj);
            }
            catch (Exception error)
            {
                _document.Close(ref _falseObj, ref _missingObj, ref _missingObj);
                _application.Quit(ref _missingObj, ref _missingObj, ref _missingObj);
                _document    = null;
                _application = null;
                throw error;
            }
            if (visibility == AppVisibility.Visible)
            {
                // делаем word видимым
                _application.Visible = true;
            }
            else
            {
                _application.Visible = false;
            }
        }
Exemple #5
0
 public void Dispose()
 {
     _document.Close(ref _falseObj, ref _missingObj, ref _missingObj);
     _application.Quit(ref _missingObj, ref _missingObj, ref _missingObj);
     _document    = null;
     _application = null;
 }
        /// <summary>
        /// 化验单
        /// </summary>
        /// <param name="a"></param>
        private void PrintAssay(Assay a)
        {
            Log.Logs += $"正在打印化验单:{a.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintAssayDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据
            WordReplace(wApp, "#Name#", a.Name);
            if (a.Purchase != null)
            {
                WordReplace(wApp, "#CarnoOrStorename#", "车号:" + a.Purchase.CarNo);
            }
            if (a.Store != null)
            {
                WordReplace(wApp, "#CarnoOrStorename#", "油仓:" + a.Store.Name);
            }
            WordReplace(wApp, "#视密#", a.视密.ToString("0.000"));
            WordReplace(wApp, "#标密#", a.标密.ToString("0.000"));
            WordReplace(wApp, "#闭口闪点#", a.闭口闪点);
            WordReplace(wApp, "#Temperature#", a.Temperature.ToString("0.00"));
            WordReplace(wApp, "#OilTempTime#", a.OilTempTime.ToLongTimeString());
            WordReplace(wApp, "#SmellType#", strSmellType(a.SmellType));
            WordReplace(wApp, "#混水反应#", a.混水反应);
            WordReplace(wApp, "#十六烷值#", a.十六烷值);
            WordReplace(wApp, "#十六烷指数#", a.十六烷指数);
            WordReplace(wApp, "#初硫#", a.初硫.ToString("0.00"));
            WordReplace(wApp, "#Percentage10#", a.Percentage10.ToString("0.00"));
            WordReplace(wApp, "#Percentage50#", a.Percentage50.ToString("0.00"));
            WordReplace(wApp, "#Percentage90#", a.Percentage90.ToString("0.00"));
            WordReplace(wApp, "#回流#", a.回流.ToString("0.00"));
            WordReplace(wApp, "#干点#", a.干点.ToString("0.00"));
            WordReplace(wApp, "#蚀点#", a.蚀点.ToString("0.00"));
            WordReplace(wApp, "#凝点#", a.凝点.ToString("0.00"));
            WordReplace(wApp, "#含硫#", a.含硫.ToString("0.00"));
            WordReplace(wApp, "#CreatedBy#", a.CreatedBy);
            WordReplace(wApp, "#CreatedAt#", a.CreatedAt.ToString("yyyy-MM-dd HH:mm"));
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + a.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #7
0
        public void Close()
        {
            document.Close();
            application.Quit();

            ReleaseObject(document);
            ReleaseObject(application);
        }
        /// <summary>
        /// 陆上装车单
        /// </summary>
        /// <param name="order">model</param>
        private void PrintLandLoad(Order order)
        {
            Log.Logs += $"正在打印LoadOil:{order.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintLandLoadDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            decimal DiffWeightToLitre = order.DiffWeight / (decimal)order.Density * 1000;
            decimal OilCountTon       = order.OilCountLitre * (decimal)order.Density / 1000;
            decimal DiffPondLitre     = order.OilCountLitre - DiffWeightToLitre;

            //填充数据
            WordReplace(wApp, "#Name#", order.Name);
            WordReplace(wApp, "#ClientName#", order.Client.CarNo);
            WordReplace(wApp, "#CompanyName#", order.Client.Company?.Name);
            WordReplace(wApp, "#StoreName#", order.Store.Name);
            WordReplace(wApp, "#DiffWeight#", order.DiffWeight.ToString("0.0"));
            WordReplace(wApp, "#Count#", (order.Count * 1000).ToString("0.0"));
            WordReplace(wApp, "#DiffOrder#", (order.DiffWeight - order.Count * 1000).ToString("0.0"));
            WordReplace(wApp, "#Price#", order.IsPrintPrice? order.Price.ToString() : "0.00");
            WordReplace(wApp, "#TotalMoney#", order.TotalMoney.ToString());
            WordReplace(wApp, "#OilCount#", order.OilCountLitre.ToString());
            WordReplace(wApp, "#OilCountTon#", OilCountTon.ToString("0.0"));
            WordReplace(wApp, "#DiffWeight#", (order.OilCarWeight - order.EmptyCarWeight).ToString("0.0"));
            WordReplace(wApp, "#DiffWeightTon#", ((order.OilCarWeight - order.EmptyCarWeight) / 1000).ToString("0.0"));
            WordReplace(wApp, "#DiffWeightToLitre#", DiffWeightToLitre.ToString("0.00"));
            WordReplace(wApp, "#DiffPondLitre#", DiffPondLitre.ToString("0.0"));
            WordReplace(wApp, "#DiffPondTon#", (DiffPondLitre * (decimal)order.Density / 1000).ToString("0.0"));
            WordReplace(wApp, "#Instrument_bf#", (order.Instrument1 - order.OilCountLitre).ToString());
            WordReplace(wApp, "#Instrument_af#", order.Instrument1.ToString());
            WordReplace(wApp, "#Density#", order.Density.ToString("0.000"));
            WordReplace(wApp, "#OilTemperature#", order.OilTemperature.ToString());
            WordReplace(wApp, "#Worker#", order.Worker);
            WordReplace(wApp, "#LastUpdatedAt#", order.LastUpdatedAt.ToString());
            WordReplace(wApp, "#Salesman#", order.Salesman);
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + order.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
        private void PrintUnload(Purchase p)
        {
            Log.Logs += $"正在打印陆上卸油单:{p.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintUnloadDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据

            WordReplace(wApp, "#Name#", p.Name);
            WordReplace(wApp, "#CarNo#", p.CarNo);
            WordReplace(wApp, "#ProductName#", p.Product?.Name);
            WordReplace(wApp, "#Count#", p.Count.ToString());
            WordReplace(wApp, "#OilCount#", p.OilCount.ToString("0.00"));
            WordReplace(wApp, "#Density#", p.Density.ToString("0.000"));
            WordReplace(wApp, "#ScaleWithCar#", p.ScaleWithCar.ToString());
            WordReplace(wApp, "#Scale#", p.Scale.ToString());

            string strToStore = "";

            foreach (var st in p.ToStoresList)
            {
                strToStore += "\r";
                strToStore += "卸入仓:" + st.Name + "\r";
                strToStore += "卸油前表数:" + st.InstrumentBf + "\r";
                strToStore += "卸油后表数:" + st.InstrumentAf + "\r";
                strToStore += "卸油数量:" + st.Count + "升\r";
            }
            WordReplace(wApp, "#ToStores#", strToStore);

            WordReplace(wApp, "#DiffLitre#", p.DiffLitre.ToString("0.00"));
            WordReplace(wApp, "#DiffTon#", p.DiffTon.ToString("0.00"));
            WordReplace(wApp, "#Worker#", p.Worker);
            WordReplace(wApp, "#StartTime#", p.StartTime.HasValue ? DateTime.Parse(p.StartTime.ToString()).ToString("yyyy-MM-dd HH:mm") : "");
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + p.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #10
0
 // закрытие открытого документа и приложения
 public void Close()
 {
     if (_document != null)
     {
         _document.Close(ref _falseObj, ref _missingObj, ref _missingObj);
     }
     _application.Quit(ref _missingObj, ref _missingObj, ref _missingObj);
     _document    = null;
     _application = null;
 }
Exemple #11
0
 private void closeDoc()
 {
     if (_document != null)
     {
         _document.Close(ref _falseObj, ref _missingObj, ref _missingObj);
     }
     _application.Quit(ref _missingObj, ref _missingObj, ref _missingObj);
     _document    = null;
     _application = null;
 }
Exemple #12
0
        private void DisposeWord()
        {
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing);

            saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
        /// <summary>
        /// 调拨单 也是收款单 水上陆上共用
        /// </summary>
        /// <param name="order"></param>
        private void PrintOrder(Order order)
        {
            Log.Logs += $"正在打印Order:{order.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintOrderDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据
            WordReplace(wApp, "#Name#", order.Name);
            WordReplace(wApp, "#CarNo#", order.CarNo);
            WordReplace(wApp, "#ProductName#", "石化油");
            WordReplace(wApp, "#Count#", order.Count.ToString());
            WordReplace(wApp, "#Unit#", order.Unit);
            WordReplace(wApp, "#Price#", order.IsPrintPrice ? order.Price.ToString("0.00") : "");
            WordReplace(wApp, "#TotalMoney#", order.IsPrintPrice? order.TotalMoney.ToString("0.00") : "");
            WordReplace(wApp, "#Payments#", strPayments(order.Payments.ToList()));
            WordReplace(wApp, "#CNMoney#", ConvertToChinese(order.TotalMoney));
            WordReplace(wApp, "#Remark#", order.Remark);
            WordReplace(wApp, "#LastUpdatedBy#", order.Cashier);
            WordReplace(wApp, "#Salesman#", order.Salesman);
            WordReplace(wApp, "#CreatedAt#", order.CreatedAt.ToString("yyyy-MM-dd HH:mm"));
            PrintTime(wApp);

            string strInvoice = "";

            if (order.IsInvoice)
            {
                strInvoice += "开票单位:" + order.BillingCompany + "\r";
                strInvoice += "开票单价:¥" + order.BillingPrice + "\r";
                strInvoice += "开票数量:" + order.BillingCount + order.Unit + "\r";
                //strInvoice += "类型:" + strTicketType(order.TicketType);
            }
            WordReplace(wApp, "#InvoiceContent#", strInvoice);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + order.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #14
0
        public void Save(string filename)
        {
            log("Saving: " + filename);

            oDoc.SaveAs(filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            oDoc.SaveAs2(@"C:\Temp\TestDocumentWith1Paragraph.docx");

            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);

            oWord.Quit();
        }
Exemple #15
0
 /// <summary>
 /// Quit the current application
 /// </summary>
 /// <param name="saveChanges">Should the changes be saved to the document.</param>
 protected void QuitApplication(bool saveChanges = false)
 {
     if (_application != null)
     {
         object refSaveChanges = saveChanges;
         // Quit the application.
         _application.Quit(
             ref refSaveChanges,
             ref _refMissing,
             ref _refMissing);
     }
     _application = null;
 }
Exemple #16
0
 /// <summary>
 /// Close the Powerpoint Application
 /// </summary>
 private void CloseOfficeApplication()
 {
     if (wordApplication != null)
     {
         wordApplication.Quit(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
         wordApplication = null;
     }
     if (powerPointApplication != null)
     {
         powerPointApplication.Quit();
         powerPointApplication = null;
     }
 }
Exemple #17
0
 public static bool Quit()
 {
     try
     {
         object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
         oWord.Quit(ref saveOption, ref Nothing, ref Nothing);
         oWord = null;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Exemple #18
0
        // 内存回收
        #region CollectGC
        /// <summary>
        /// 内存回收
        /// </summary>
        /// <param name="myWordApp"></param>
        public static void CollectGC(ref Word._Application myWordApp)
        {
            object missing = Type.Missing;

            if (myWordApp != null)
            {
                myWordApp.Quit(ref missing, ref missing, ref missing);
                myWordApp = null;
            }
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(myWordApp);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            //GC.Collect();
            //GC.WaitForPendingFinalizers();
        }
 /// <summary>
 /// 关闭Word程序
 /// </summary>
 public void Close(bool saveChanges = false)
 {
     try
     {
         bool result = _engine.IsObjectValid[_engine];
         if (result)
         {
             _wordDoc.Close(saveChanges, _missing, _missing);
             _engine.Quit();
         }
     }
     catch (Exception ex)
     {
         return;
     }
 }
Exemple #20
0
        // конструктор, создаем по шаблону, потом возможно расширение другими вариантами
        public word()
        {
            wordApplication = new Word.Application();

            //создаем ноый документ методом приложения Word по поути к шаблону документа
            try
            {
                  wordDocument = wordApplication.Documents.Add(ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing);

            }
            // если произошла ошибка, то приложение Word надо закрыть
            catch (Exception error)
            {
                wordApplication.Quit(ref wordMissing, ref  wordMissing, ref wordMissing);
                wordApplication = null;
                throw new Exception("Ошибка. Не удалось открыть шаблон документа MS Word. " + error.Message);
            }
            // завершение createFromTemplate(string templatePath)
        }
        /// <summary>
        /// 生产转仓单
        /// </summary>
        /// <param name="m"></param>

        private void PrintMoveStore(MoveStore m)
        {
            Log.Logs += $"正在打印生产转仓:{m.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintMoveStoreDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据
            WordReplace(wApp, "#Name#", m.Name);
            WordReplace(wApp, "#OutStoreName#", m.OutStoreName);
            WordReplace(wApp, "#OutPlan#", m.OutPlan.ToString("0.00"));
            WordReplace(wApp, "#OutDensity#", m.OutDensity.ToString("0.000"));
            WordReplace(wApp, "#OutTemperature#", m.OutTemperature.ToString());
            WordReplace(wApp, "#OutFact#", m.OutFact.ToString("0.00"));
            WordReplace(wApp, "#InStoreName#", m.InStoreName);
            WordReplace(wApp, "#InDensity#", m.InDensity.ToString("0.000"));
            WordReplace(wApp, "#InTemperature#", m.InTemperature.ToString());
            WordReplace(wApp, "#InFact#", m.InFact.ToString("0.00"));
            WordReplace(wApp, "#Manufacturer#", m.Worker);
            WordReplace(wApp, "#LastUpdatedAt#", m.LastUpdatedAt.ToString("yyyy-MM-dd HH:mm"));
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + m.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #22
0
        /// <summary>
        /// 打印word
        /// </summary>
        public static void PrintWord(string filePath, MacrosVerbCollection macrosCollection)
        {
            Word.Application app = new Word.Application();

            Word.Document doc     = null;
            object        collate = true;
            object        item    = Word.WdPrintOutItem.wdPrintDocumentContent;
            object        missing = System.Reflection.Missing.Value;

            try
            {
                object path = filePath;
                doc = app.Documents.Add(ref path, ref missing, ref missing, ref missing);
                Replace(doc, macrosCollection);

                app = new Microsoft.Office.Interop.Word.ApplicationClass();
                doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
                             ref missing, ref missing, ref item, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing);
            }
            catch (Exception exp)
            {
                Function.Alert(exp.Message, "提示");
            }
            finally
            {
                object         saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                Word._Document _doc       = doc as Word._Document;
                if (_doc != null)
                {
                    _doc.Close(ref saveChange, ref missing, ref missing);
                }
                Word._Application _app = app as Word._Application;
                if (_app != null)
                {
                    _app.Quit(ref missing, ref missing, ref missing);
                }
            }
        }
        private void PrintUnloadPond(Purchase p)
        {
            Log.Logs += $"正在打印卸车石化过磅单:{p.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintUnloadPondDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据

            WordReplace(wApp, "#Name#", p.Name);
            WordReplace(wApp, "#CarNo#", p.CarNo);
            WordReplace(wApp, "#ScaleWithCar#", p.ScaleWithCar.ToString());
            WordReplace(wApp, "#Scale#", p.Scale.ToString());
            WordReplace(wApp, "#DiffWeight#", p.DiffWeight.ToString());
            WordReplace(wApp, "#Worker#", p.Worker);
            WordReplace(wApp, "#StartTime#", p.StartTime.HasValue ? DateTime.Parse(p.StartTime.ToString()).ToString("yyyy-MM-dd HH:mm"): "");

            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + p.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #24
0
        public void Doc(IEnumerable <object> objects, Object path, string columns)
        {
            application = new Application();

            try
            {
                document = application.Documents.Add();
            }
            catch (Exception error)
            {
                document.Close(ref falseObj, ref missingObj, ref missingObj);
                application.Quit(ref missingObj, ref missingObj, ref missingObj);
                document    = null;
                application = null;
                throw error;
            }
            application.Visible = true;

            this.columns = columns.Split(',');
            MakeTable(objects);
            Save(document, path);
        }
        /// <summary>
        /// 关闭Word文档
        /// </summary>
        /// <param name="isSave">是否保存</param>
        public void CloseWord(bool isSave)
        {
            object IsSave = isSave as object;

            if (!ValidValue())
            {
                return;
            }
            try
            {
                _curDoc.Close(ref IsSave, ref oMissing, ref oMissing);
                Marshal.ReleaseComObject(_curDoc);
                _curDoc = null;
                _wordApp.Quit(ref IsSave, ref oMissing, ref oMissing);
                Marshal.ReleaseComObject(_wordApp);
                _wordApp = null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 船舶清污完工证
        /// </summary>
        /// <param name="m"></param>
        private void PrintBoatClean(BoatClean m)
        {
            Log.Logs += $"正在打印BoatClean:{m.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintBoatCleanDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据
            WordReplace(wApp, "#Name#", m.Name);
            WordReplace(wApp, "#CarNo#", m.CarNo);
            WordReplace(wApp, "#Company#", m.Company);
            WordReplace(wApp, "#Voyage#", m.Voyage.ToString());
            WordReplace(wApp, "#Tonnage#", m.Tonnage.ToString());
            WordReplace(wApp, "#ResponseId#", m.ResponseId);
            WordReplace(wApp, "#Address#", m.Address);
            WordReplace(wApp, "#CreatedAt#", m.CreatedAt.ToString("yyyy-MM-dd HH:mm"));
            WordReplace(wApp, "#EndTime#", m.EndTime.ToString());
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + m.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
        /// <summary>
        /// 出库石化过磅单
        /// </summary>
        /// <param name="p"></param>
        private void PrintPonderation(Order o)
        {
            Log.Logs += $"正在打印出库石化过磅单:{o.Name}\r";

            Word.Application thisApplication = new Word.ApplicationClass();
            wApp = thisApplication;
            string tmpDocFile   = AppDomain.CurrentDomain.BaseDirectory + folder + Properties.Settings.Default.PrintPonderationDocx;
            object templatefile = tmpDocFile;

            wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
            wDoc.Activate();                                                                    //当前文档置前

            //填充数据
            WordReplace(wApp, "#Name#", o.Name);
            WordReplace(wApp, "#CarNo#", o.CarNo);
            WordReplace(wApp, "#OilCarWeight#", o.OilCarWeight.ToString("0.00"));
            WordReplace(wApp, "#EmptyCarWeight#", o.EmptyCarWeight.ToString("0.00"));
            WordReplace(wApp, "#DiffWeight#", o.DiffWeight.ToString("0.00"));
            WordReplace(wApp, "#Worker#", o.Worker);
            WordReplace(wApp, "#LastUpdatedAt#", o.LastUpdatedAt.ToString());
            WordReplace(wApp, "#EndOilDateTime#", o.EndOilDateTime.ToString());
            WordReplace(wApp, "#StartOilDateTime#", o.StartOilDateTime.ToString());
            PrintTime(wApp);

            object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
            object filename   = AppDomain.CurrentDomain.BaseDirectory + createdfolder + o.Name + ".docx";

            wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                        missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
                          missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                          ref missing);
            object saveOption = Word.WdSaveOptions.wdSaveChanges;

            wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                        saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
        }
Exemple #28
0
        /// <summary>
        /// Close
        /// </summary>
        public void Close()
        {
            // close the repository and tidy up
            if (m_bCloseEAFileOnExit)
            {
                m_bCloseEAFileOnExit = false;
                m_Repository.CloseFile();
            }

            if (m_bCloseEAOnExit)
            {
                m_Repository.Exit();
                m_bCloseEAOnExit = false;
            }

            if (m_oWord != null)
            {
                m_oWord.Quit();
            }

            m_oWord = null;
        }
Exemple #29
0
        private void BtnExport_Click(object sender, EventArgs e)
        {
            wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            string question;

            if ((question = MakeQuestionStr()) != "没有选择")
            {
                Cursor.Current = Cursors.WaitCursor;
                ;
                if (GetDialogResult(question) == DialogResult.OK)
                {
                    MakeExport();
                }
                Cursor.Current = Cursors.Default;
            }
            else
            {
                MessageBox.Show(question);
            }

            wordApp.Quit();
        }
Exemple #30
0
        private void CreateWordDocument()
        {
            //создаем обьект приложения word
            m_app = new Microsoft.Office.Interop.Word.Application();
            // создаем путь к файлу
            Object templatePathObj = System.IO.Path.GetTempFileName();

            // если вылетим не этом этапе, приложение останется открытым
            try
            {
                m_doc = m_app.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj);
            }
            catch (Exception error)
            {
                m_doc.Close(ref falseObj, ref missingObj, ref missingObj);
                m_app.Quit(ref missingObj, ref missingObj, ref missingObj);
                m_doc = null;
                m_app = null;
                throw error;
            }
            //m_app.Visible = true;
        }
Exemple #31
0
        /// <summary>
        /// 保存word文档
        /// </summary>
        public void SaveWord()
        {
            object missing = System.Reflection.Missing.Value;
            Object Nothing = System.Reflection.Missing.Value;
            object Visible = false;

            try
            {
                object Save_FileName = _SaveFilePath;

                _MainDoc.SaveAs(ref Save_FileName, ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref Visible,
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                ref missing);
                _MainDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                _MainDoc = null;
                _WordApplicMain.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemple #32
0
        private void CreateWordDocument()
        {
            //создаем обьект приложения word
            m_app = new Microsoft.Office.Interop.Word.Application();
            // создаем путь к файлу
            Object templatePathObj = System.IO.Path.GetTempFileName();

            // если вылетим не этом этапе, приложение останется открытым
            try
            {
                m_doc = m_app.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj);
            }
            catch (Exception error)
            {
                m_doc.Close(ref falseObj, ref missingObj, ref missingObj);
                m_app.Quit(ref missingObj, ref missingObj, ref missingObj);
                m_doc = null;
                m_app = null;
                throw error;
            }
            //m_app.Visible = true;
        }
Exemple #33
0
        // создаем приложение Word и открывает новый документ по заданному файлу шаблона
        public void CreateFromTemplate(string templatePath)
        {
            //создаем обьект приложения word

                wordApplication = new Word.Application();
                wordApplication.Visible = false;

            // создаем путь к файлу используя имя файла
            templatePathObj = templatePath;

            //создаем ноый документ методом приложения Word по поути к шаблону документа
            try
            {
                wordDocument = wordApplication.Documents.Open(templatePath);
                //wordDocument = wordApplication.Documents.Add(ref templatePathObj,ref wordMissing, ref wordMissing, true);
            }
            // если произошла ошибка, то приложение Word надо закрыть
            catch (Exception error)
            {
                wordApplication.Quit(ref wordMissing, ref  wordMissing, ref wordMissing);
                wordApplication = null;
                throw new Exception("Ошибка. Не удалось открыть шаблон документа MS Word. " + error.Message);
            }
            // завершение createFromTemplate(string templatePath)
        }