返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

实现.net操作Word 程序代码

时间:2011-05-24 14:07来源:知行网www.zhixing123.cn 编辑:麦田守望者

前提:导入COM库:Microsoft word 11.0 Object Library.
创建新Word
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7 ref oMissing, ref oMissing);
打开文档:
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 object fileName = @"E:CCCXCXXTestDoc.doc";
7 oDoc = oWord.Documents.Open(ref fileName,
8 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
9 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
10 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 object fileName = @"E:XXXCCXTest.doc";
7 oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
8 ref oMissing, ref oMissing);
.添加新表
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7 ref oMissing, ref oMissing);
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
.表插入行
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7 ref oMissing, ref oMissing);
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
.单元格合并
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7 ref oMissing, ref oMissing);
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
18 Word.Cell cell = newTable.Cell(1, 1);
19 cell.Merge(newTable.Cell(1, 2));
.单元格分离
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add( oMissing,
7 ref oMissing, ref oMissing);
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
18 Word.Cell cell = newTable.Cell(1, 1);
19 cell.Merge(newTable.Cell(1, 2));
21 object Rownum = 2;
22 object Columnnum = 2;
23 cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
1 object oMissing = System.Reflection.Missing.Value;
2 object oEndOfDoc = "endofdoc"; /**//* endofdoc is a predefined bookmark */
4 //Start Word and create a new document.
5 Word._Application oWord;
6 Word._Document oDoc;
7 oWord = new Word.Application();
8 oWord.Visible = true;
9 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
10 ref oMissing, ref oMissing);
12 //Insert a paragraph at the beginning of the document.
13 Word.Paragraph oPara1;
14 oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
15 oPara1.Range.Text = "Heading 1";
16 oPara1.Range.Font.Bold = 1;
17 oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
18 oPara1.Range.InsertParagraphAfter();

将数据存入Word示例1[引入Microsoft.Office.Interop.Word组件]
if(this.richTextBox1.Text=="")
return;
f(this.saveFileDialog1.ShowDialog()==DialogResult.Cancel)
return;
string FileName = this.saveFileDialog1.FileName;
if(FileName.Length<1)
return;
FileName+=".doc";
try
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc;
object nothing = System.Reflection.Missing.Value;
doc = word.Documents.Add(ref nothing,ref nothing,ref nothing,ref nothing);
doc.Paragraphs.Last.Range.Text = this.richTextBox1.Text;
object myfileName = FileName;
//将WordDoc文档对象的内容保存为doc文档
doc.SaveAs(ref myfileName,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing);
//关闭WordDoc文档对象
doc.Close(ref nothing,ref nothing,ref nothing);
//关闭WordApp组件对象
word.Quit(ref nothing,ref nothing,ref nothing);
MessageBox.Show("Word文件保存成功","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(System.Exception ex)
{
MessageBox.Show(this,ex.Message.ToString(),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
}
示例2
#region 将DataTable中的数据导到Word中,返回bool值
/// <summary>
/// 将DataTable中的数据导到Word中,返回bool值
/// </summary>
/// <param name="table">需要导出的DataTable</param>
/// <returns></returns>
public static bool DataTableToWord(System.Data.DataTable table)
{
try
{
Microsoft.Office.Interop.Word.Document mydoc = new Microsoft.Office.Interop.Word.Document();
Microsoft.Office.Interop.Word.Table mytable;
Microsoft.Office.Interop.Word.Selection mysel;
Object myobj;
if (table.Rows.Count == 0)
return false;
//建立Word对象
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
myobj = System.Reflection.Missing.Value;
mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
word.Visible = true;
mydoc.Select();
mysel = word.Selection;
//将数据生成Word表格文件
mytable = mydoc.Tables.Add(mysel.Range, table.Rows.Count, table.Columns.Count, ref myobj, ref myobj);
//设置列宽
mytable.Columns.SetWidth(50, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
//输出列标题数据
for (int i = 0; i < table.Columns.Count; i++)
{
mytable.Cell(1, i + 1).Range.InsertAfter(table.Columns[i].ColumnName);
}
//输出控件中的记录
for (int i = 0; i < table.Rows.Count - 1; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
mytable.Cell(i + 2, j + 1).Range.InsertAfter(table.Rows[i][j].ToString());
}
}
return true;
}catch(Exception ex)
{
exceptionMessage = ex.Message;

------分隔线----------------------------
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
推荐内容
猜你感兴趣