C# 使用COM组件往word文档中写入不同格式和字体的文字,应该如何写代码?
有没有什么象Paint绘制事件类似的方法?
希望高手能给予代码提示,谢谢。

解决方案 »

  1.   

    //显示数据
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.dataSet1 = new DataSet();
                    if (this.oleDbConnection1.State == ConnectionState.Open)
                        this.oleDbConnection1.Close();
                    this.oleDbConnection1.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb";
                    this.oleDbConnection1.Open();
                    string MySQL = this.textBox1.Text;
                    this.oleDbCommand1.CommandText=MySQL;
                    this.oleDbCommand1.Connection=this.oleDbConnection1;
                    this.oleDbDataAdapter1.SelectCommand=this.oleDbCommand1;
                    this.oleDbDataAdapter1.Fill(this.dataSet1);
                    this.dataGridView1.DataSource =this.dataSet1.Tables[0];
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            //输出Word表
            private void button2_Click(object sender, EventArgs e)
            {
                ApplicationClass MyWord;
                _Document MyDoc;
                Selection MySelection;
                Table MyTable;
                Object[,] MyData = new Object[5000, 30];
                System.Data.OleDb.OleDbDataReader MyReader;
                Object MyObj;
                int MyRows, MyColumns, i, j;
                try
                {
                    MyObj = System.Reflection.Missing.Value;
                    MyWord = new ApplicationClass();
                    MyWord.Visible = true;                
                    MyDoc=MyWord.Documents.Add(ref MyObj, ref MyObj, ref MyObj, ref MyObj);
                    MyDoc.Select();
                    MySelection = MyWord.Selection;
                    MyRows = this.dataSet1.Tables[0].Rows.Count;
                    MyColumns = this.dataSet1.Tables[0].Columns.Count;
                    MyTable = MyDoc.Tables.Add(MySelection.Range, MyRows + 1, MyColumns,ref  MyObj, ref MyObj);
                    //设置列宽
                    for (i = 1; i < MyColumns + 1; i++)
                    {
                        MyTable.Columns[i].SetWidth(110, WdRulerStyle.wdAdjustNone);
                    }
                    //设置第一行的背景颜色
                    MyTable.Rows[1].Cells.Shading.BackgroundPatternColorIndex = WdColorIndex.wdGray25;
                    //设置第一行的字体
                    MyTable.Rows[1].Range.Bold = 1;
                    MyReader = this.oleDbCommand1.ExecuteReader();
                    //输出列标题数据
                    i=1;
                    foreach(DataColumn MyColumn in this.dataSet1.Tables[0].Columns )
                    {
                       MyDoc.Tables[1].Cell(1,i).Range.InsertAfter(MyColumn.ColumnName);
                       i=i+1;
                    }
                    //输出数据库记录
                    j=2;
                    while(MyReader.Read())
                    {
                        for(i=0;i<MyReader.FieldCount;i++)
                        {
                            MyDoc.Tables[1].Cell(j,i+1).Range.InsertAfter(MyReader[i].ToString());
                        }
                        j=j+1;
                    }
                    this.oleDbConnection1.Close();                
                }
                catch (Exception MyEx)
                {
                    MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
      

  2.   

    还是有点不太理解,比如我要是想往Word文档中的100,100位置上输入一个宋体字样的文本     和200,200位置上输入一个华文彩云的文本字样应该怎么样写代码?
    求高手指点。
      

  3.   

    楼主如果用的vs2005可以看看vsto.net2003下面可以看看Office的Interop相关