C#生成word文档

tjut2008 2010-12-26 11:56:28
最近有这样的一个需求:用C#把一些数据写到word中,生成一个报告,报告中多数内容是已知的只是某些地方需要从数据库中获取.查一些资料说可以使用模板,使用域什么的,请各位帮忙!
...全文
2286 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
KBY_1987 2011-11-08
  • 打赏
  • 举报
回复
请问一下为什么我的程序中这样写老报错呢
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

ApplicationClass不让用,说让我选用适当的接口,只能用Application,这是怎么回事


还有就是我保存word文档时也出现异常,保存失败为什么啊,我是这样写的
WordDoc.SaveAs(ref filename, 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.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
tjut2008 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lewisli 的回复:]
引用 10 楼 tjut2008 的回复:
先谢谢了...

引用 4 楼 lewisli 的回复:
我有完整的Demo,明天上班拷回来给你。

我已经把导出的方法给你粘贴出来了,
想要Demo的话明天给你,这个方法在我在项目中弄出来的,跟我这个方法逻辑思路是一样的
[/Quote]
研究一下...
tjut2008 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
WORD模板中使用书签
object nothing = System.Reflection.Missing.Value;
object optional = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Wo……
[/Quote]
这个好像可以,就是那个Open方法里面这么多参数都是做什么用的,MSDN上没找到答案,请指教!
Lewisli 2010-12-26
  • 打赏
  • 举报
回复
newTable = WordDoc.Tables.Add(wrdRng, 5, 4, ref Nothing, ref Nothing);
这个代表(5,4)5行4列
hinada99 2010-12-26
  • 打赏
  • 举报
回复
hinada99 2010-12-26
  • 打赏
  • 举报
回复
Lewisli 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tjut2008 的回复:]
先谢谢了...

引用 4 楼 lewisli 的回复:
我有完整的Demo,明天上班拷回来给你。
[/Quote]
我已经把导出的方法给你粘贴出来了,
想要Demo的话明天给你,这个方法在我在项目中弄出来的,跟我这个方法逻辑思路是一样的
tjut2008 2010-12-26
  • 打赏
  • 举报
回复
先谢谢了...[Quote=引用 4 楼 lewisli 的回复:]
我有完整的Demo,明天上班拷回来给你。
[/Quote]
Lewisli 2010-12-26
  • 打赏
  • 举报
回复
我这个没有图片,你可以参考6楼怎么插入图片,我这个导出日志的模板,你修改成你自己想要的模板
Lewisli 2010-12-26
  • 打赏
  • 举报
回复

public bool CreateWordFile(string _filename, "数据List或者你C#要写的数据")
{
#region 开始生成Word
try
{

string strtitle = "任务导出";
object oEndOfDoc = "\\endofdoc";
Object Nothing = System.Reflection.Missing.Value;
Object filename = _filename;
//创建Word文档

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);


//设置页眉
WordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("WPFOA任务导出");
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置
//任务导出------名字
Word.Paragraph oPara1;
oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);
oPara1.Range.Text = strtitle;
oPara1.Range.Font.Bold = 1;
oPara1.Range.Font.Name = "宋体";
oPara1.Range.Font.Size = 20;
oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara1.Format.SpaceAfter = 5; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
#region 循环每个表

foreach (var v in lst_task)
{
#region 循环每一个列,产生一行数据
//描述信息
Word.Paragraph oPara3;
object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = WordDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "";
oPara3.Range.Font.Bold = 0;
oPara3.Range.Font.Name = "宋体";
oPara3.Range.Font.Size = 9;
oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara3.Format.SpaceBefore = 1;
oPara3.Format.SpaceAfter = 1;
oPara3.Range.InsertParagraphAfter();

//插入表格
Word.Table newTable;
Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
newTable = WordDoc.Tables.Add(wrdRng, 5, 4, ref Nothing, ref Nothing);
newTable.Columns[1].Width = 60;
newTable.Columns[2].Width = 145;
newTable.Columns[3].Width = 80;
newTable.Columns[4].Width = 145;
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

//填充表格内容
newTable.Cell(1, 1).Range.Text = "项目名称";
newTable.Cell(1, 2).Range.Text = "1";
newTable.Cell(1, 3).Range.Text = "模块名称";
newTable.Cell(1, 4).Range.Text = "2";
newTable.Cell(2, 1).Range.Text = "负责人名";
newTable.Cell(2, 2).Range.Text = "3";
newTable.Cell(2, 3).Range.Text = "状态";
newTable.Cell(2, 4).Range.Text = "4";
newTable.Cell(3, 1).Range.Text = "发布时间";
newTable.Cell(3, 2).Range.Text = "5";
newTable.Cell(3, 3).Range.Text = "要求完成时间";
newTable.Cell(3, 4).Range.Text = "6";
newTable.Cell(4, 1).Range.Text = "完成时间";
newTable.Cell(4, 2).Range.Text = "7";
newTable.Cell(4, 3).Range.Text = "分配人";
newTable.Cell(4, 4).Range.Text = "8";
newTable.Cell(5, 1).Range.Text = "任务描述";
newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));
newTable.Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
newTable.Cell(5, 2).Range.Text = "12313213123213";
#endregion
}


WordDoc.SaveAs(ref filename, 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.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
#endregion
//关闭WinWord进程
System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
MyProcess[0].Kill();
return true;

}
catch
{

System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
MyProcess[0].Kill();
return false;
}
#endregion
}
xin36933 2010-12-26
  • 打赏
  • 举报
回复
word做书签
hinada99 2010-12-26
  • 打赏
  • 举报
回复
c#生成的word文档的方法
生成word文档 public class BiultReportForm
   {
   ///
   /// word 应用对象
   ///
   private Microsoft.Office.Interop.Word.Application _wordApplication;
  
   ///
   /// word 文件对象
   ///
   private Microsoft.Office.Interop.Word.Document _wordDocument;
   ///
   /// 创建文档
   ///
   public void CreateAWord()
   {
   //实例化word应用对象
   this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
   Object myNothing = System.Reflection.Missing.Value;
  
   this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
   }
   ///
   /// 添加页眉
   ///
   ///
   public void SetPageHeader(string pPageHeader)
   {
   //添加页眉
   this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;
   this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
   this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
   //设置中间对齐
   this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
   //跳出页眉设置
   this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
   }
   ///
   /// 插入文字
   ///
   /// 文本信息
   /// 字体打小
   /// 字体颜色
   /// 字体粗体
   /// 方向
   public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
   {
   //设置字体样式以及方向
   this._wordApplication.Application.Selection.Font.Size = pFontSize;
   this._wordApplication.Application.Selection.Font.Bold = pFontBold;
   this._wordApplication.Application.Selection.Font.Color= pFontColor;
   this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
   this._wordApplication.Application.Selection.TypeText(pText);
   }
  
  
   ///
   /// 换行
   ///
   public void NewLine()
   {
   //换行汽车配件网www.qichepeijian.com
   this._wordApplication.Application.Selection.TypeParagraph();
   }
   ///
   /// 插入一个图片
   ///
   ///
   public void InsertPicture(string pPictureFileName)
   {
   object myNothing = System.Reflection.Missing.Value;
   //图片居中显示
   this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
   this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
   }
   ///
   /// 保存文件
   ///
   /// 保存的文件名
   public void SaveWord(string pFileName)
   {
   object myNothing = System.Reflection.Missing.Value;
   object myFileName = pFileName;
   object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;
   object myLockd = false;
   object myPassword = "";
   object myAddto = true;
   try
   {
   this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
   ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
   ref myNothing, ref myNothing, ref myNothing);
   }
   catch
   {
   throw new Exception("导出word文档失败!");
   }
   }
   }
  
tjut2008 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hinada99 的回复:]
http://wenku.baidu.com/view/0a8a6f11f18583d0496459b8.html
[/Quote]
这个我看过,问题在于能不能使用word模板,生成报告时只需要用数据填充某些固定的域,这样会更方便.比如有个简历的模板,里面包含许多域如,姓名,性别,年龄等等,这些信息因人而异如果这些信息都存在数据库中,操作的时候可以选择这个模板,选择不同的人,就能生成对应的简历了...
Lewisli 2010-12-26
  • 打赏
  • 举报
回复
我有完整的Demo,明天上班拷回来给你。
wuyq11 2010-12-26
  • 打赏
  • 举报
回复
添加COM组件引用
wuyq11 2010-12-26
  • 打赏
  • 举报
回复
WORD模板中使用书签
object nothing = System.Reflection.Missing.Value;
object optional = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word._Document Doc = app.Documents.Open(ref sFile, ref optional, ref docreadonly, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref visible, ref optional, ref optional, ref optional, ref optional);
if (Doc.Bookmarks.Exists("Name"))
{
object b1 = "Name";
Word.Bookmark bk1 = Doc.Bookmarks.get_Item(ref b1);
bk1.Range.Text = p.Title;";
}
hinada99 2010-12-26
  • 打赏
  • 举报
回复
http://wenku.baidu.com/view/0a8a6f11f18583d0496459b8.html
honkerhero 2010-12-26
  • 打赏
  • 举报
回复
竟然都是COM操作
ggw128 2010-12-26
  • 打赏
  • 举报
回复
6楼和8楼的答案都很具有参考价值了。

110,545

社区成员

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

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

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