今天编写了一个c#的windows应用程序,主窗口是一个form,现在有一些计算出来的数据,想做成word格式输出来,
在该应用程序中生成一个word文档
Word._Application app = new Word.ApplicationClass();
app.Visible = false;
object nothing = System.Reflection.Missing.Value;
object temp = Environment.CurrentDirectory + @"/resume.doc";
Word.Document doc
= app.Documents.Open(ref temp,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);
for (i = 0; i < ds.Tables[3].Rows.Count; i ++)
{
int intTableID = ""; //需要添加内容的tableID
intRow = doc.Tables[intTable].Rows.Count;
doc.Tables[intTable].Cell(intRow,1).Range.Text
= ds.Tables[3].Rows[i]["ExperenceDate"].ToString();
doc.Tables[intTable].Cell(intRow,2).Range.Text
= ds.Tables[3].Rows[i]["Company"].ToString();
doc.Tables[intTable].Cell(intRow,3).Range.Text
= ds.Tables[3].Rows[i]["Department"].ToString();
doc.Tables[intTable].Cell(intRow,4).Range.Text
= ds.Tables[3].Rows[i]["Station"].ToString();
doc.Tables[intTable].Rows.Add(ref nothing);
}
在CreateWord.cs文件里面添加:
using System.Reflection;
using Microsoft.Office.Interop.Word;
然后在按钮事件里面添加如下代码:
ApplicationClass wordApp = new ApplicationClass();
object missing = System.Reflection.Missing.Value;
object tempName = @"d:/Temp.dot"; // 模板名称,本例中的模板如后面的图
object docName = @"D:/WebSite/a.doc"; // 生成的word文件,可以不放在web目录下,当然,实际应用中可能会涉及到动态生成文件名,相信这点难不倒你吧 ^_^
// 生成新文档,这里使用了上面提到的模板,如果不想使用指定的模板,可使用missing,这是会使用默认的模板。关于生成word文件的几个方法,参阅MSDN
Document MyDoc = wordApp.Documents.Add(ref tempName, ref missing, ref missing, ref missing);
wordApp.Visible = true;
MyDoc.Activate();
wordApp.Selection.Font.Size = 30; // 字体大小
wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; // 居中
wordApp.Selection.Font.Bold = (int)WdConstants.wdToggle; // 黑体
// 还有很多很多的格式可以在这里设置,发挥你的想像力尽情研究吧,吼吼~~~~~~~~
wordApp.Selection.TypeText("hello"); // 文字内容,这里我没有从数据库里面读取,只是测试了一下写入的内容
// 保存word文档
MyDoc.SaveAs(ref docName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// 关闭,释放
MyDoc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
MyDoc = null;
wordApp = null;
Response.Redirect("DownloadWord.aspx?id=" + "a.doc");
下载页面的代码(DownloadWord.aspx.CS):
string FullFileName = Request.QueryString["id"];
FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + FullFileName); // 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。
// 下面到就是读取文件,通过数据流的方式下载了。
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
又一方法:
private void WriteFile()
{
strFileName=System.Windows.Forms.Application.StartupPath+"//试题库【"+GetRandomString()+"】.doc";
Object Nothing=System.Reflection.Missing.Value;
myWordDoc=myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
#region 将数据库中读取得数据写入到word文件中
strContent="d://试题库/n/n/r";
Response.WriteFile(strContent);
strContent="试题库";
Response.WriteFile(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);
}
string title="C#生成文档";
object titleLengh=title.Length;
string first="/n 公司最近需要利用C#对项目进行编程,其"+
"中存在一个功能就是可自动生成WORD文档,但一直以来都"+
"找不到什么好办法,无奈之下,只有自已学着写一个了.";
object firstLengh=first.Length;
string second="/n 如果能真正生成WORD文档的好处有:";
object secondLengh=second.Length;
string third="/n1、根据数据库信息自动生成文档;";
object thirdLengh=third.Length;
string forth="/n2、免去书写文档之苦;";
object forthLengh=forth.Length;
string fifth="/n3、可以通过邮件方式传出文档。";
object fifthLengh=fifth.Length;
object missing;
object zero=0;
object one=1;
object two=2;
object tree=3;
object four=4;
object five=5;
object six=6;
object seven=7;
object eight=8;
object nine=9;
object ten=10;
Word.ApplicationClass wa=new Word.ApplicationClass();
missing=System.Reflection.Missing.Value;
wa.Visible=true;
wa.Documents.Add(ref missing,ref missing,ref missing,ref missing);
Word.Range myRange=wa.ActiveDocument.Range(ref zero,ref zero);
object r=myRange;
Word.Paragraph p=wa.ActiveDocument.Paragraphs.Add(ref r);
p.Range.InsertBefore(title);
Word.Range titleRange=wa.ActiveDocument.Range(ref zero,ref titleLengh);
titleRange.Font.Size=24;
titleRange.Font.Name="隶书";
titleRange.Font.Color=Word.WdColor.wdColorBlue;
//MessageBox.Show("NO.1");
titleRange.Paragraphs.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
Word.Range firstR=wa.ActiveDocument.Paragraphs.Item(2).Range;
r=firstR;
p=wa.ActiveDocument.Paragraphs.Add(ref r);
firstR.Font.Size=12;
firstR.Paragraphs.Alignment=Word.WdParagraphAlignment.wdAlignParagraphJustify;
firstR.InsertAfter(first);
firstR.InsertParagraphAfter();
//firstR=wa.ActiveDocument.Paragraphs.Item(3).Range;
firstR.InsertAfter(second);
firstR.InsertAfter(third);
firstR.InsertAfter(forth);
firstR.InsertAfter(fifth);