C# Office 2010 自动化

发布于 2024-09-15 00:35:37 字数 148 浏览 2 评论 0原文

我正在尝试制作一个程序,将数据插入到现有Word文档中的特定位置并保存它的副本。 我不知道如何做到这一点,而且我找不到任何关于 Office 2010 自动化的好资源。 谁能指出我正确的方向和/或给我一些例子。

提前致谢。

找到解决方案稍后添加答案

i'm trying to make a program that inserts data into specific places in existing word document and saves a copy of it.
and i have no clue how to do it , and i cant find any good resource on office 2010 automating.
can anyone point me in the right direction and/or give me some examples.

thanks in advance.

found a solution will add an answer later on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-09-22 00:35:37

这是我的做法,它可能不是最好的方法,但它有效!

添加对 Office 互操作的引用

using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;


          //defines new excel and workd apps
            var ap = new Word.Application();
            var excelApp = new Excel.Application();
            // defines new excel worksheet & workbooks
            Excel.Workbook exBook;
            Excel.Worksheet xlWorkSheet;
            // should the excell/word apps be visible ? 
            excelApp.Visible = false;
            ap.Visible = false;

            //defining the index numbers of our content controls that are in the word template
            // index numbers start from 1 and are numbered by order of creation
            object Price, Name, address;
            Price = 1;
            Name = 2;
            address = 3;


            // here we open the excell file
            exBook = excelApp.Workbooks.Open(@"C:\test.xls");
            // and we open the first worksheet
            xlWorkSheet = exBook.Worksheets.get_Item(1);
            Excel.Range range ;
            //here we select the first worksheet and make it active
            Excel._Worksheet workSheet = (Excel.Worksheet) excelApp.ActiveSheet;
            //we open the word document
            var doc = ap.Documents.Open(@"C:\test.dotx", ReadOnly: false, Visible: false);
            // and we assign the content controls 
            var dPrice = doc.ContentControls.get_Item(ref Price);
            var dName = doc.ContentControls.get_Item(ref Name);
            var dAddress = doc.ContentControls.get_Item(ref address);
            doc.Activate();
            range = xlWorkSheet.UsedRange;
            // here we define the columns that we are going to select
            object t, P , E , K, N,M,J;

            P = "P";
            E = "E";
            K = "K";
            J  = "J";
            N = "N";
            M = "M";

            // and here we loop trought the rows of the excell worksheet
            // IMPORTANT! excell rows count starts from 1 and not from 0 !
         for (int i =1; i< Convert.ToInt16(Settings1.Default.copies) ;i++)

            {

                t = i;
                // here we get the value if cell t(1..2..3..etc), P
                var dummy = (range.Cells[t, P] as Excel.Range).Value2;
                // here we insert the content of the cell to the content control

                dPrice.Range.Text = ": " + Convert.ToString(dummy) + " лв";
                dName.Range.Text = ": " + (string)(range.Cells[t, E] as Excel.Range).Value2;

                // same thing here
                var city = (string) (range.Cells[t, J] as Excel.Range).Value2;
                var address1 = (string) (range.Cells[t, K] as Excel.Range).Value2;
                var city2 = (string) (range.Cells[t, M] as Excel.Range).Value2;
                var address2 = (string) (range.Cells[t,N] as Excel.Range).Value2;
                if (!string.IsNullOrEmpty(city2) && city2 != " " && !string.IsNullOrEmpty(address2) && address2 != " ")
                {
                    dAddress.Range.Text = ": " +city.Normalize() + " " + address1.Normalize() + " , " + city2.Normalize() + " " + address2.Normalize() ;

                }
                else
                {
                    dAddress.Range.Text = ": " + city.Normalize() + " " + address1.Normalize();
                }


                try
                {
                    //here we try to save the word document as a pdf file
                    object name = @"C:\t\test"+i+".pdf";
                    object FileFormat = WdSaveFormat.wdFormatPDF;
                    doc.SaveAs(ref name, ref FileFormat);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message +" source "+ ex.Source.ToString());

                }

            }
            // here quit word without saving the changes to the template  
            ap.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
            excelApp.Quit();
            // and we release the objects 
            System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

我希望这对某人有帮助:)

Here is how i did it , it may not be the best way , but its working !

add references to the office interop

using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;


          //defines new excel and workd apps
            var ap = new Word.Application();
            var excelApp = new Excel.Application();
            // defines new excel worksheet & workbooks
            Excel.Workbook exBook;
            Excel.Worksheet xlWorkSheet;
            // should the excell/word apps be visible ? 
            excelApp.Visible = false;
            ap.Visible = false;

            //defining the index numbers of our content controls that are in the word template
            // index numbers start from 1 and are numbered by order of creation
            object Price, Name, address;
            Price = 1;
            Name = 2;
            address = 3;


            // here we open the excell file
            exBook = excelApp.Workbooks.Open(@"C:\test.xls");
            // and we open the first worksheet
            xlWorkSheet = exBook.Worksheets.get_Item(1);
            Excel.Range range ;
            //here we select the first worksheet and make it active
            Excel._Worksheet workSheet = (Excel.Worksheet) excelApp.ActiveSheet;
            //we open the word document
            var doc = ap.Documents.Open(@"C:\test.dotx", ReadOnly: false, Visible: false);
            // and we assign the content controls 
            var dPrice = doc.ContentControls.get_Item(ref Price);
            var dName = doc.ContentControls.get_Item(ref Name);
            var dAddress = doc.ContentControls.get_Item(ref address);
            doc.Activate();
            range = xlWorkSheet.UsedRange;
            // here we define the columns that we are going to select
            object t, P , E , K, N,M,J;

            P = "P";
            E = "E";
            K = "K";
            J  = "J";
            N = "N";
            M = "M";

            // and here we loop trought the rows of the excell worksheet
            // IMPORTANT! excell rows count starts from 1 and not from 0 !
         for (int i =1; i< Convert.ToInt16(Settings1.Default.copies) ;i++)

            {

                t = i;
                // here we get the value if cell t(1..2..3..etc), P
                var dummy = (range.Cells[t, P] as Excel.Range).Value2;
                // here we insert the content of the cell to the content control

                dPrice.Range.Text = ": " + Convert.ToString(dummy) + " лв";
                dName.Range.Text = ": " + (string)(range.Cells[t, E] as Excel.Range).Value2;

                // same thing here
                var city = (string) (range.Cells[t, J] as Excel.Range).Value2;
                var address1 = (string) (range.Cells[t, K] as Excel.Range).Value2;
                var city2 = (string) (range.Cells[t, M] as Excel.Range).Value2;
                var address2 = (string) (range.Cells[t,N] as Excel.Range).Value2;
                if (!string.IsNullOrEmpty(city2) && city2 != " " && !string.IsNullOrEmpty(address2) && address2 != " ")
                {
                    dAddress.Range.Text = ": " +city.Normalize() + " " + address1.Normalize() + " , " + city2.Normalize() + " " + address2.Normalize() ;

                }
                else
                {
                    dAddress.Range.Text = ": " + city.Normalize() + " " + address1.Normalize();
                }


                try
                {
                    //here we try to save the word document as a pdf file
                    object name = @"C:\t\test"+i+".pdf";
                    object FileFormat = WdSaveFormat.wdFormatPDF;
                    doc.SaveAs(ref name, ref FileFormat);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message +" source "+ ex.Source.ToString());

                }

            }
            // here quit word without saving the changes to the template  
            ap.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
            excelApp.Quit();
            // and we release the objects 
            System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

i hope this was helpful to someone :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文