将 Repeater 导出到 MS Word 并将 Word 文件保存在服务器或数据库上 C#、ASP.NET

发布于 2024-11-25 00:13:00 字数 1832 浏览 0 评论 0原文

我遇到过这样的情况:我必须通过将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-12-02 00:13:00

我从以下链接获取了帮助

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#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文