您的位置:首页 > 编程语言 > C#

C# 在模态窗体生成Word文档,并直接下载!

2011-02-24 15:39 393 查看
模态窗体Web代码:

<asp:LinkButton ID="wordLinkBtn" runat="server" onclick="wordLinkBtn_Click"><img src="../res/img/download.gif" mce_src="res/img/download.gif" />导出Word文档</asp:LinkButton>
<iframe id="download" runat="server" style="display:none;" mce_style="display:none;"></iframe>


模态窗体CS代码:

引用:
using MsWord = Microsoft.Office.Interop.Word;
//导出word文档
protected void wordLinkBtn_Click(object sender, EventArgs e)
{
this.loadingImage.Visible = true;
object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
//创建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.Visible = true;
//标题
wordApp.Application.Selection.Font.Size = 17;
wordApp.Selection.Font.Bold = (int)MsWord.WdConstants.wdToggle; //黑体
wordApp.Selection.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Application.Selection.TypeText(this.Label2.Text);
wordApp.Application.Selection.TypeParagraph();
wordApp.Selection.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphLeft;
//插入段落
MsWord.Paragraph oPara1;
oPara1 = wordDoc.Content.Paragraphs.Add(ref Nothing);
oPara1.Range.Font.Size = 14;
oPara1.Range.Font.Bold = 0;
oPara1.Format.SpaceAfter = 5;
oPara1.Range.Text = this.Label1.Text;
oPara1.Range.InsertParagraphAfter();
MsWord.Paragraph oPara2;
oPara2 = wordDoc.Content.Paragraphs.Add(ref Nothing);
oPara2.Range.Font.Size = 14;
oPara2.Range.Font.Bold = 0;
oPara2.Format.SpaceAfter = 5;
string l3Text = "    参加会议人员名单:/r/n    ";
l3Text += this.Label3.Text;
l3Text = l3Text.Replace("<br>", "/r/n    ");
oPara2.Range.Text = l3Text;
oPara2.Range.InsertParagraphAfter();
object fileName = @"c://" + this.Label2.Text.Trim() + ".doc";
//将wordDoc文档对象的内同保存为DOC文档
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.Save();
//关闭wordDoc文档对象
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
this.loadingImage.Visible = false;
download.Attributes["src"] = "DownloadWord.aspx?filePath=c://&fileName=" + this.Label2.Text.Trim() + ".doc";
}


DownloadWord.aspx页面的CS代码:

protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.QueryString["filePath"];
string FullFileName = Request.QueryString["fileName"];
FileInfo DownloadFile = new FileInfo(filePath + FullFileName); // 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。
// 下面到就是读取文件,通过数据流的方式下载了。
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
//删除文件
if(File.Exists(filePath+"//"+FullFileName))
{
File.Delete(filePath + "//" + FullFileName);
}
Response.Close();
Response.End();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: