使用 RadGrid 导出自定义 Excel 文件

发布于 2025-01-01 15:54:59 字数 456 浏览 0 评论 0原文

我可以将 radgrid 导出到 Excel 文件,但我想在工作表中添加更多信息。

如果可能的话,我将不胜感激用于生成自定义 Excel 文件的教程/示例代码。

                    <tel:radgrid runat="server" id="mygrid" skinid="RadGrid_Search_Standard">
                    <ExportSettings HideStructureColumns="true" />

                    </tel:radgrid>

网格与一些数据表进行数据绑定,我需要添加一些数据 在上面添加一些字符串

mygrid.MasterTableView.ExportToWord()

I can export my radgrid to an excel file but I want to add some more info into the sheet.

If it is possible, I would appreciate for a tutorial/sample code for doing a custom excel file generation.

                    <tel:radgrid runat="server" id="mygrid" skinid="RadGrid_Search_Standard">
                    <ExportSettings HideStructureColumns="true" />

                    </tel:radgrid>

Grid is databound with some datatable and I need to add some data
to add some strings above

mygrid.MasterTableView.ExportToWord()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

难得心□动 2025-01-08 15:54:59

这是我与 Telerik Grid 一起使用的一些代码,而不是使用他们提供的 ExportToExcel 函数,我创建了自己的按钮来触发它自己的导出事件。

我有一个名为 getDataSource 的函数(未包含),我用它来填充网格,您可以覆盖它或创建自己的函数以将数据提取到 DataTable 中,并添加您认为合适的任何行/列/数据。

        //export button calls this
        private void ExportReport()
        {
            SetPublicVariables();

            System.Data.DataTable dt = GetDataSource(false); 

            string exportData = buildCSVExportString(dt);

            string filename = string.Format("{0} - {1}.csv",
                (Master as MasterPages.Drilldown).Titlelbl.Text, CampaignTitle);
            if (filename.Length > 255) filename = filename.Substring(0, 255);

            ExportCSV(exportData, filename);
        }

//build a string CSV
public static string buildCSVExportString(DataTable exportDT)
        {
            StringBuilder exportData = new StringBuilder();
            // get headers.

            int iColCount = exportDT.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
               exportData.Append(exportDT.Columns[i].ToString());
                if (i < iColCount - 1)
                {
                    exportData.Append(",");
                }
            }
            exportData.Append(System.Environment.NewLine);            

            // get rows.
            foreach (DataRow dr in exportDT.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        //If the variable is a string it potentially has charaters that can't be parsed properly.
                        //this fixes the comma issue(which adds aditional columns).  Replace and escape " with "".
                        if (dr[i] is string)        
                            exportData.Append(String.Format(@"""{0}""", ((string)dr[i]).Replace("\"", @"""""")));
                        else
                            exportData.Append(dr[i].ToString());
                    }
                    if (i < iColCount - 1)
                    {
                        exportData.Append(",");
                    }
                }
                exportData.Append(System.Environment.NewLine);
            }
            return exportData.ToString();
        }



public void ExportCSV(string content, string filename)
        {
            filename = RemoveIllegalPathChars(filename);
            HttpResponse Response = HttpContext.Current.Response;
            string ext = System.IO.Path.GetExtension(filename);
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename));
            Response.ContentType = "text/csv; charset-UTF-8;";
            Response.Clear();
            Response.Write(content);
            Response.End();
        }

Here's some code that I use with a Telerik Grid, rather than using the ExportToExcel function they've provided I created my own button that fires it's own export event.

I have a function (not included) called getDataSource that I use to populate the grid, you could override this or create your own to fetch the data into a DataTable and add any rows/columns/data as you see fit.

        //export button calls this
        private void ExportReport()
        {
            SetPublicVariables();

            System.Data.DataTable dt = GetDataSource(false); 

            string exportData = buildCSVExportString(dt);

            string filename = string.Format("{0} - {1}.csv",
                (Master as MasterPages.Drilldown).Titlelbl.Text, CampaignTitle);
            if (filename.Length > 255) filename = filename.Substring(0, 255);

            ExportCSV(exportData, filename);
        }

//build a string CSV
public static string buildCSVExportString(DataTable exportDT)
        {
            StringBuilder exportData = new StringBuilder();
            // get headers.

            int iColCount = exportDT.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
               exportData.Append(exportDT.Columns[i].ToString());
                if (i < iColCount - 1)
                {
                    exportData.Append(",");
                }
            }
            exportData.Append(System.Environment.NewLine);            

            // get rows.
            foreach (DataRow dr in exportDT.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        //If the variable is a string it potentially has charaters that can't be parsed properly.
                        //this fixes the comma issue(which adds aditional columns).  Replace and escape " with "".
                        if (dr[i] is string)        
                            exportData.Append(String.Format(@"""{0}""", ((string)dr[i]).Replace("\"", @"""""")));
                        else
                            exportData.Append(dr[i].ToString());
                    }
                    if (i < iColCount - 1)
                    {
                        exportData.Append(",");
                    }
                }
                exportData.Append(System.Environment.NewLine);
            }
            return exportData.ToString();
        }



public void ExportCSV(string content, string filename)
        {
            filename = RemoveIllegalPathChars(filename);
            HttpResponse Response = HttpContext.Current.Response;
            string ext = System.IO.Path.GetExtension(filename);
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename));
            Response.ContentType = "text/csv; charset-UTF-8;";
            Response.Clear();
            Response.Write(content);
            Response.End();
        }
娇纵 2025-01-08 15:54:59

一种可能的方法是在导出之前修改 HTML 代码。方法如下。

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    RadGridName.GridExporting += (s, a) => 
    {
        string myHtmlCode = "<span>My HTML code goes here</span>";
        a.ExportOutput = a.ExportOutput.Replace("<body>", "<body>" + myHtmlCode);
    };
}

这应该适用于 Excel(不是 ExcelML)和 Word。

祝你好运

A possible way would be to modify the HTML code just before exporting. Here is how.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    RadGridName.GridExporting += (s, a) => 
    {
        string myHtmlCode = "<span>My HTML code goes here</span>";
        a.ExportOutput = a.ExportOutput.Replace("<body>", "<body>" + myHtmlCode);
    };
}

This should work for both Excel (not ExcelML) and Word.

Good luck

时光沙漏 2025-01-08 15:54:59

您唯一需要做的就是将附加页面信息添加到 arg 的 ExportOutput

void yourRadGridID_GridExporting(object sender, GridExportingArgs e)
{

 string additionalPageInfo= "your html code for the additional page info goes here";

 e.ExportOutput = e.ExportOutput.Replace("`<body>`", "`<body>`" + additionalPageInfo);

}

The only thing you need to do is add your additional page info to the ExportOutput of your arg

void yourRadGridID_GridExporting(object sender, GridExportingArgs e)
{

 string additionalPageInfo= "your html code for the additional page info goes here";

 e.ExportOutput = e.ExportOutput.Replace("`<body>`", "`<body>`" + additionalPageInfo);

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