• c# 操作word(经典)


    public Form1()
            {
                InitializeComponent();
            }
            object oMissing = System.Reflection.Missing.Value;

            private void button1_Click(object sender, EventArgs e)
            {
                //#region
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc";  

                //Start   Word   and   create    new   document.  
                //开始word 并且创建一个文本对象
                Word._Application oWord;
                Word._Document oDoc;
            
                oWord = new Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref  oMissing, ref   oMissing,
                ref   oMissing, ref   oMissing);

                //添加页眉
                oWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
                oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
                oWord.ActiveWindow.ActivePane.Selection.InsertAfter("[Nogika科技开发有限公司报告软件]");
                oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置居中对齐
                oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置

                oWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距


                //移动焦点并换行
                object count = 14;
                object WdLine = Word.WdUnits.wdLine;//换一行;
                oWord.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点
                oWord.Selection.TypeParagraph();//插入段落


                #region

                //Keep   inserting   text.   When   you   get   to    inches   from   top   of   the  
                //document,   insert    hard   page   break.

                //换页
                //object oPos;
                //double dPos = oWord.InchesToPoints(7);
                //oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range.InsertParagraphAfter();
                //int i = 0;
                //do
                //{
                    //wrdRng = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;

                    //wrdRng.ParagraphFormat.SpaceBefore = 1;
                    //wrdRng.InsertAfter("A   line   of   text");

                    ////wrdRng.InsertParagraphAfter();
                    ////oPos = wrdRng.get_Information
                    ////(Word.WdInformation.wdVerticalPositionRelativeToPage);
                    ////i++;
                //}
                //while (dPos >= Convert.ToDouble(oPos));

                //Word.Range wrdRng1 = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;//变量
                //object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                //object oPageBreak = Word.WdBreakType.wdPageBreak;
                //wrdRng1.Collapse(ref   oCollapseEnd);
                //wrdRng1.InsertBreak(ref   oPageBreak);
                //wrdRng1.Collapse(ref   oCollapseEnd);
                //wrdRng1.InsertAfter("We're   now   on   page   2.   Here's   my   chart:");
                //wrdRng1.InsertParagraphAfter();

                DataTable dt = new DataTable();


                 Graph.XlChartType xlcharttype = Microsoft.Office.Interop.Graph.XlChartType.xlLine;

                 //creatChart(oEndOfDoc, oDoc, oWord, xlcharttype);
                //InsertChart(dt,xlcharttype,oDoc,oEndOfDoc);
                 InsertHeadTitle(oEndOfDoc, oDoc);
                 InsertPragraph(oEndOfDoc, oDoc);
                 PageBreak(oEndOfDoc,oDoc,2);
                 InsertPragraph(oEndOfDoc, oDoc);
                 PageBreak(oEndOfDoc,oDoc,3);
                 creatChart(oEndOfDoc, oDoc, oWord, xlcharttype);

                 InsertPicture(oEndOfDoc,oWord,oDoc);

                //#endregion
                #endregion

            }

    /// <summary>
            /// 创建图表
            /// </summary>
            /// <param name="EndOfDoc"></param>
            /// <param name="Doc"></param>
            /// <param name="Word"></param>
            /// <param name="xlcharttype"></param>
            public void creatChart(object EndOfDoc, Word._Document Doc, Word._Application Word, Graph.XlChartType xlcharttype)
            {
                Graph.Axis axis;

                Word._Document oDoc = Doc;
                Word._Application oWord = Word;
                object oEndOfDoc = EndOfDoc;                  

                object oClassType = "MSGraph.Chart.8";//加入一张图表,系统自带office控件;
                //wrdRng = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;
                Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;

                //初始化一张图表
                Graph.Chart wrdChart;
                wrdChart = (Graph.Chart)wrdRng.InlineShapes.AddOLEObject(ref   oClassType, ref   oMissing,
                ref   oMissing, ref   oMissing, ref   oMissing,
                ref   oMissing, ref   oMissing, ref   oMissing).OLEFormat.Object;


                wrdChart.Application.PlotBy = Graph.XlRowCol.xlColumns;//根据Y轴来画图表


                //改变图表格式
                wrdChart.ChartType = xlcharttype;          ////可以设置为饼状图

                axis = (Graph.Axis)wrdChart.Axes(1, 1);      //设置X轴的属性

                wrdChart.Application.DataSheet.Cells.Clear();//清空表格的初始数据

                DataSet ds = getData();                    //获得表集合
                DataTable dtsheet = ds.Tables[0];          //给指定数据表
            
                //填充图表,起始的行号和列号都是1
                int i, j;

                for (i = 0; i < dtsheet.Columns.Count; i++)//初始化列名
                {
                    wrdChart.Application.DataSheet.Cells[1, i + 1] = dtsheet.Columns[i].ColumnName;
                }
                for (i = 0; i < dtsheet.Rows.Count; i++)//填充数据
                {
                    for (j = 0; j < dtsheet.Columns.Count; j++)
                    {
                        wrdChart.Application.DataSheet.Cells[i + 2, j + 1] = dtsheet.Rows[i][j].ToString().Replace("9999999 ", "100ys ");
                    }
                }

                //axis.MaximumScale = 1;//X轴最大刻度
                //axis.MajorUnit = 0.1;

                
                //wrdChart.Legend.Delete();
                wrdChart.Width = 1000;

                wrdChart.Height = 1332;
                wrdChart.Height = oWord.InchesToPoints(3.57f);

                //Add   text   after   the   chart.  
                //'在图之后加入文字。
                Word.Range wrdRng1 = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;
                wrdRng1 = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;
                wrdRng1.InsertParagraphAfter();
                wrdRng1.InsertAfter("THE   END.");

                //更新图表并保存退出
                wrdChart.Application.Update();
                wrdChart.Application.Quit();
                //this.Save();

    /// <summary>
            /// 插入段落
            /// </summary>
            /// <param name="EndOfDoc"></param>
            /// <param name="Doc"></param>
            public void InsertPragraph(object EndOfDoc, Word._Document Doc)
            {
                object oEndOfDoc = EndOfDoc;
                Word._Document oDoc = Doc;

                Word.Paragraph oPara4;
                object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
                oPara4.Range.Text = "段落内容";
                oPara4.Range.Font.Bold = 3;
                oPara4.Format.SpaceAfter = 5;
                oPara4.Range.InsertParagraphAfter();  //插入回车
     
            }

     


            /// <summary>
            /// 换页
            /// </summary>
            /// <param name="oEndOfDoc">需换页前一页的位置</param>
            /// <param name="doc">文档对象</param>
            public void PageBreak(object oEndOfDoc,Word._Document doc,int index)
            {
                Word.Table oTable;
                Word._Document oDoc;
                oDoc = doc;

                Word.Range wrdRng1 = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;//变量
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                object oPageBreak = Word.WdBreakType.wdPageBreak;
                wrdRng1.Collapse(ref   oCollapseEnd);
                wrdRng1.InsertBreak(ref   oPageBreak);
                wrdRng1.Collapse(ref   oCollapseEnd);
                wrdRng1.InsertAfter("We're   now   on   page  " +   index + "   Here's   my   chart:");
                wrdRng1.InsertParagraphAfter();

                Word.Range wrdRng2 = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;//变量
                oTable = oDoc.Tables.Add(wrdRng2, 5, 2, ref   oMissing, ref   oMissing);
                oTable.Range.ParagraphFormat.SpaceAfter = 6;
                int r, c;
                string strText;
                for (r = 1; r <= 5; r++)
                    for (c = 1; c <= 2; c++)
                    {
                        strText = "r" + r + "c" + c;
                        oTable.Cell(r, c).Range.Text = strText;
                    }
     
            }

    /// <summary>
            /// 插入图片
            /// </summary>
            /// <param name="EndOfDoc"></param>
            /// <param name="Word"></param>
            /// <param name="Doc"></param>
            public void InsertPicture(object EndOfDoc, Word._Application Word, Word._Document Doc)
            {
                object oEndOfDoc = EndOfDoc;
                Word._Application oWord = Word;
                Word._Document oDoc = Doc;

                object item = 1;
                object reftrue = true;
                object reffalse = false;
                object wrdRng = oDoc.Bookmarks.get_Item(ref   oEndOfDoc).Range;

                oWord.Selection.InlineShapes.AddPicture(@"E:\杂文件\analysis.jpg", ref reffalse, ref reftrue, ref wrdRng);

                Doc.Application.ActiveDocument.InlineShapes[1].Width = 200f;//图片宽度
                Doc.Application.ActiveDocument.InlineShapes[1].Height = 200f;//图片高度
                //将图片设置为四周环绕型
                //Word.Shape s = Doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                //s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
     
            }

     

     

    调试通过了.也能生成word

    把这5个方法 放在一个类下.就可以生成了.

  • 相关阅读:
    curl命令详解
    Linux 下 set env export declare浅浅 set和shopt命令详解--(shell定制) (转载)
    ps aux指令詳解
    smb设置参考手册 --详细参数
    Ajax
    JSON浅谈
    Date类型
    笔记本电脑不能上网的问题
    editplus 初步设置
    w10 系统升级
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/2036654.html
Copyright © 2020-2023  润新知