All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
AJAX的跨域名访问_[AJAX教程]
Delphi过程和函数的重载
ASP.NET中实现多文件上传的实例程序代码_.net资料_编程技术
高级自定义查询、分页、多表联合存储过程_[SQL Server教程]
ASP.NET技巧:Session丢失原因与解决方案小结_.net资料_编程技术
Xml学习笔记(二)Javascript篇_[XML教程]
Linux操作系统中用PHP构建网站详解_php资料_编程技术
利用MD5加密数据库中的密码_[Asp.Net教程]
手把手教你写Ajax驱动的DataGrid控件
TreeView服务器控件的功能和属性
Reading number is top 10 pictures
yy365网站上的美女3
一万二一支的万珂,用得真心肉疼。
Very beautiful interior decoration
怀春少女-石一伊
泳装名模阿尔贝特妮写真
一千块钱如何创业
Chinese paper-cut grilles art appreciation2
NeedWallpaper13
移民小国也实惠1
Sell the barbecue as says father du breul4
Download software ranking
Tram sex maniac 2 (H) rar bag1
Tram sex maniac 2 (H) rar bag10
Boxer Classic video2
C#程序员参考手册
The Bermuda triangle1
Call Of Duty2
C#与.NET技术平台实战演练
1400篇各类破解文章
Unix video tutorial18
美女写真2
delv published in(发表于) 2014/1/23 3:12:41 Edit(编辑)
C#.net操作Word文档——以Office,2007为例_[Asp.Net教程]

C#.net操作Word文档——以Office,2007为例_[Asp.Net教程]

C#.net操作Word文档:以Office 2007为例_[Asp.Net教程]

首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;


namespace WordTest
{
public partial class Form1 : Form
{
object strFileName;
Object Nothing;
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Document myWordDoc;
string strContent = "";


public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{
createWord();
//openWord();
}


private void createWord()
{
strFileName = System.Windows.Forms.Application.StartupPath + "test.doc";
if (System.IO.File.Exists((string)strFileName))
System.IO.File.Delete((string)strFileName);
Object Nothing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);


#region 将数据库中读取得数据写入到word文件中


strContent = "你好\n\n\r";
myWordDoc.Paragraphs.Last.Range.Text = strContent;


strContent = "这是测试程序";
myWordDoc.Paragraphs.Last.Range.Text = strContent;



#endregion


//将WordDoc文档对象的内容保存为DOC文档
myWordDoc.SaveAs(ref strFileName, 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文档对象
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);


this.richTextBox1.Text = strFileName + "\r\n" + "创建成功";


}
private void openWord()
{
fontDialog1.ShowDialog();
System.Drawing.Font font = fontDialog1.Font;
object filepath = "D:\\asp.docx";
object oMissing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
myWordDoc.Content.Font.Size = font.Size;
myWordDoc.Content.Font.Name = font.Name;
myWordDoc.Save();
richTextBox1.Text = myWordDoc.Content.Text;



myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}


}








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.