如何通过.net C#生成word文档

w22432243 2008-04-09 08:58:03
本人想通过C#代码生成word文档,文档中包括一些word中的基本内容 如表格(表格的边框参数设置,位置等),插入页眉页脚,图片,文档的编辑等~!
希望右高手可以帮助我。。
谢谢了
最好是一个例子
本人邮箱:w22432243@163.com
急用 麻烦了。。。。。
...全文
1268 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunday_L 2008-04-09
  • 打赏
  • 举报
回复
受用~~

2楼你强的
zyjmib 2008-04-09
  • 打赏
  • 举报
回复
liuming
cdxkfc5 2008-04-09
  • 打赏
  • 举报
回复
谢谢2楼,帮我解决了我输入文字同时加下划线的问题,感谢
http://topic.csdn.net/u/20080409/21/64c8d7b4-d07a-4599-b096-c9b32b38c299.html

可是我还有一个问题
我在模板中定义很多 选择框,现在程序中数据分析后,要将一部分打沟(在方框中打对号,表示选中的意思),不知道如何实现?
希望帮忙~~~
badtank 2008-04-09
  • 打赏
  • 举报
回复
如果是office2003,可以通过建立office word Document工程来开发调试word文档。
changjiangzhibin 2008-04-09
  • 打赏
  • 举报
回复

//仅供参考
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using Sytem.Reflection;

string filePath;
string content;
MSWord.Application wordApp;
MSWord.Document wordDoc;
filePath = @"d:\testWord.docx";
wordApp = new MSWord.ApplicationClass();

if(File.Exists(filePath))
{
File.Delete(filePath);
}

Object nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref nothing,ref nothing,ref nothing,ref nothing);

/* 1、写入普通文本 */
content = "Hello!";
wordDoc.Paragraphs.Last.Range.Text = content;
object format = MSWord.WdSaveFormat.wdFormatDocmentDefault;

/* 2、 写入需要的特殊格式文本 */
//写入15号字体
content = "这一行是15号字体的文本";
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Text = content;

//写入斜体文本
content = "这一行是斜体文本";
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = content;

//写入红色下划线文本
content = "这一行是红色下划线的文本";
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorRed;
wordDoc.Paragraphs.Last.Range.Text = content;

/* 3、写入表格 */
//表格对象
MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,5,
5,ref nothing,ref nothing);
table.Border.Enable = 1;
for(int i=1;; i<6; i++)
{
for(int j=1; j<6; j++)
{
table.Cell(i,j).Range.Text = ""+i+"行","+j+"列";
}
}

/* 4、插入图片 */
string jpgName = @"d:\logo.jpg";
Object range = wordDoc.Paragraphs.Last.Range;
Object linkToFile = false;
Object saveWithDocument = true;
wordDoc.InlineShapes.AddPicture(jpgName,ref linkToFile,ref saveWithDocument,ref range);

wordDoc.SaveAs(ref path,ref format,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.Close(ref nothing ref nothing,ref nothing);
wordApp.Quit(ref nothing,ref nothing,ref nothing);

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧