예제 #1
0
        /// <summary>
        /// 表格处理
        /// </summary>
        /// <param name="filePath">word文件名</param>
        /// <returns></returns>
        public static bool AddTable(string filePath, System.Data.DataTable dt)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document WordDoc;
                if (File.Exists(filePath))
                {
                    WordDoc = WordApp.Documents.Open(ref 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);
                }
                else
                {
                    WordDoc = WordApp.Documents.Add();
                }



                //插入表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
                ////设置表格
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle  = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Columns[1].Width         = 100f;
                newTable.Columns[2].Width         = 220f;
                newTable.Columns[3].Width         = 105f;

                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "我的简历";
                //设置单元格中字体为粗体
                newTable.Cell(1, 1).Range.Bold = 2;

                //合并单元格
                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));

                //垂直居中
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //水平居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

                //填充表格内容
                newTable.Cell(2, 1).Range.Text = "座右铭:...";
                //设置单元格内字体颜色
                newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
                //合并单元格
                newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //填充表格内容
                newTable.Cell(3, 1).Range.Text = "姓名:";
                newTable.Cell(3, 2).Range.Text = "雷鑫";
                //纵向合并单元格
                newTable.Cell(3, 3).Select();
                //选中一行
                object moveUnit   = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount  = 3;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                WordApp.Selection.Cells.Merge();

                //表格中插入图片
                string pictureFileName  = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg";
                object LinkToFile       = false;
                object SaveWithDocument = true;
                object Anchor           = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //图片宽度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;
                //图片高度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;
                //将图片设置为四周环绕型
                Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                newTable.Cell(12, 1).Range.Text = "备注:";
                newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                //在表格中增加行
                WordDoc.Content.Tables[1].Rows.Add(ref oMissing);

                //保存
                WordDoc.SaveAs2(filePath);
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
        public async Task Save_And_Close(string customer)
        {
            try
            {
                //app.Visible = true;
                // get path to directory of current work order number and determine if it exists
                string letterFolder, fullDir, filePath;
                DateReceivedYear = await new AccessDb().GetJobYear(WorkOrderNumber);
                if (string.IsNullOrEmpty(DateReceivedYear))
                {
                    MessageBox.Show("Invalid date received for current job. Please check and make sure job information is correct.", "Job Date Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    WordApp.Visible = true;
                    return;
                }

                Regex letterFolderRegex = new Regex(@"^[a-mA-M0-9]");
                letterFolder = letterFolderRegex.IsMatch(customer) ? "_A to M" : "_N to Z";

                fullDir = $@"test_report_complete_location";
                //DirectoryInfo job_path = new DirectoryInfo(full_dir);
                if (!Directory.Exists(fullDir))
                {
                    MessageBox.Show("The current job folder does not exist. Please check and make sure this work order number is correct and has been logged.", "No Job Folder", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    WordApp.Visible = true;
                    return;
                }
                filePath = $@"{fullDir}\{WorkOrderNumber}_{TestConditionAbr}_{TestPerformedOn}_Metallographic_Examination_test_report_data_only.docx";
                // determine if file already exists and ask before overwriting
                if (File.Exists(filePath))
                {
                    if (MessageBox.Show("Data only test report already exists. Saving current file will override existing test report. Would you like to continue and save anyway?", "File Already Exists", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    {
                        WordApp.Visible = true;
                        return;
                    }
                }
                WordDoc.SaveAs2(filePath);
                if (MessageBox.Show("Data only test report has been created and saved in job folder. Would you like to open it now?", "Open Test Report", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    try
                    {
                        // open directoruy containing data report
                        Process.Start(fullDir);
                    }
                    catch (Exception) { }
                    // open data report
                    WordApp.Visible = true;
                }
                else
                {
                    WordDoc.Close();
                    WordApp.Quit();
                }
            }
            catch (Exception ex)
            {
                sw = new StreamWriter(ErrorLogFilePath, true);
                sw.WriteLine($"{DateTime.Now.ToShortTimeString()}\nReport Save_And_Close -- {ex.Source}; {ex.TargetSite}\n{ex.Message}\n");
                sw.Close();

                if (WordApp != null)
                {
                    WordApp.Visible = true;
                }
                //if (WordDoc != null)
                //    WordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, Word.WdOriginalFormat.wdOriginalDocumentFormat, false);
                //if (WordApp != null)
                //    WordApp.Quit();
                throw;
            }
        }