将 Repeater 导出到 MS Word 并将 Word 文件保存在服务器或数据库上 C#、ASP.NET
我遇到过这样的情况:我必须通过将 Repeater 导出到 Word 文档来生成 MS Word 报告。我正在这样做,但我的要求是将 MS Word 文件保存在服务器/数据库上。生成Word文档后如何将该文件保存在服务器/数据库上?
要求是在服务器/数据库上生成并保存 Word 文件,并且生成或保存的文件应该是只读文件。我该怎么办?
这是我用来生成Word文件的代码..
protected void DisplayRepeater()
{
if (ddlDivRAHQ.SelectedValue == "DIV")
{
Repeater_PrintFinalReport.DataSource = new ReportItems().Get_Rpt_Items_Div_Chief(Convert.ToInt32(Session["UserNumber"]), DateTime.Parse(Session["ReportStartDate"].ToString()), DateTime.Parse(Session["ReportEndDate"].ToString()), ddlDivRAHQ.SelectedValue.ToString()).OrderBy(x => x.OrgNum).ThenBy(x => x.Office_Location).ThenBy(x => x.Site_Desc).ToList();
}
else
{
Repeater_PrintFinalReport.DataSource = new ReportItems().Get_Rpt_Items_Div_Chief(Convert.ToInt32(Session["UserNumber"]), DateTime.Parse(Session["ReportStartDate"].ToString()), DateTime.Parse(Session["ReportEndDate"].ToString()), ddlDivRAHQ.SelectedValue.ToString()).Where(x=>x.Finalized == 1).OrderBy(x => x.Site_Desc).ToList();
}
Repeater_PrintFinalReport.DataBind();
Repeater_PrintFinalReport.Visible = true;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + ddlDivRAHQ.SelectedValue + "_WeeklyReport_" + DateTime.Now.ToShortDateString() + ".doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Repeater_PrintFinalReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
I have a situation where I have to generate a MS Word report by exporting the Repeater to Word Document. I am doing it but my requirement is to Save the MS Word file either on Server / Database. How can I save that file on Server/Database after generating the Word Document?
Requirement is to Generate and Save the Word file on either Server / Database and also the generated or saved file should be a Read-Only file. How can I do it ?
Here is the Code I am using to generate Word file..
protected void DisplayRepeater()
{
if (ddlDivRAHQ.SelectedValue == "DIV")
{
Repeater_PrintFinalReport.DataSource = new ReportItems().Get_Rpt_Items_Div_Chief(Convert.ToInt32(Session["UserNumber"]), DateTime.Parse(Session["ReportStartDate"].ToString()), DateTime.Parse(Session["ReportEndDate"].ToString()), ddlDivRAHQ.SelectedValue.ToString()).OrderBy(x => x.OrgNum).ThenBy(x => x.Office_Location).ThenBy(x => x.Site_Desc).ToList();
}
else
{
Repeater_PrintFinalReport.DataSource = new ReportItems().Get_Rpt_Items_Div_Chief(Convert.ToInt32(Session["UserNumber"]), DateTime.Parse(Session["ReportStartDate"].ToString()), DateTime.Parse(Session["ReportEndDate"].ToString()), ddlDivRAHQ.SelectedValue.ToString()).Where(x=>x.Finalized == 1).OrderBy(x => x.Site_Desc).ToList();
}
Repeater_PrintFinalReport.DataBind();
Repeater_PrintFinalReport.Visible = true;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + ddlDivRAHQ.SelectedValue + "_WeeklyReport_" + DateTime.Now.ToShortDateString() + ".doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Repeater_PrintFinalReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从以下链接获取了帮助
http://support.microsoft.com/kb/316384
使用 Microsoft.Office.Interop.Word 的 MS.Word
如何使用 Visual C# 自动执行 Microsoft Word 创建新文档
I have taken the help from the below link
http://support.microsoft.com/kb/316384
MS.Word using Microsoft.Office.Interop.Word
How to automate Microsoft Word to create a new document by using Visual C#