C# 怎样往WORD文档中输入文本 C# 使用COM组件往word文档中写入不同格式和字体的文字,应该如何写代码?有没有什么象Paint绘制事件类似的方法?希望高手能给予代码提示,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //显示数据 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); } } 还是有点不太理解,比如我要是想往Word文档中的100,100位置上输入一个宋体字样的文本 和200,200位置上输入一个华文彩云的文本字样应该怎么样写代码?求高手指点。 楼主如果用的vs2005可以看看vsto.net2003下面可以看看Office的Interop相关 c# c/s系统如何上传5M以上的图片到服务器 如何合并DATAGRIDVIEW中同一列相同值的几个单元格? 请问如何让float和int表示一个空值 急~!怎样用C#修改XML写的节点文档数据!多谢指教 请问imagebutton控件能实现这些功能吗? 一个WINFORM的问题 ComboBox自动筛选功能, 你实现过吗? 怎样把一个字符串数组连接成一个新的字符串? Form中如何显示表格及数据 微软的同志在吗?高手在吗?请教如何用代码注册一个自定义的通道? C# 怎样往WORD文档中输入文本 问一个关于工作压力的话题!大家帮忙进来说说看!
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);
}
}
求高手指点。