如何将 ASP.NET GridView 复制到剪贴板

发布于 2024-10-07 15:35:45 字数 114 浏览 6 评论 0原文

有谁有将 GridView/Datatable(Web 应用程序 aspx)的内容复制到剪贴板的解决方案/链接,以便我可以粘贴到 Excel 中?

我尝试四处搜索,但只找到了表单应用程序的解决方案。

Does anyone have a solution/link for copying contents of a GridView/Datatable (web application aspx)to the clipboard, so that I can paste inside Excel?

I've tried searching around but only found solutions for a Forms application.

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

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

发布评论

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

评论(3

虐人心 2024-10-14 15:35:45

这里有一些代码可以帮助您将网格视图的数据放入剪贴板,以便您可以粘贴到 Excel 中。

ClientScriptManager csm;
StringBuilder sb;
string stringToAppend;
int currentPage;

csm = Page.ClientScript;
sb = new StringBuilder();

// headers
foreach (DataControlField dcf in gvClass.Columns)
{
    sb.Append(dcf.HeaderText + "\t");
}

sb.Append("\\n");
currentPage = gvClass.PageIndex;

for (int i = 0; i < gvClass.PageCount; i++)
{
    gvClass_PageIndexChanging(null, new GridViewPageEventArgs(i));

    foreach (GridViewRow gvr in gvClass.Rows)
    {
        foreach (TableCell tc in gvr.Cells)
        {
            if (tc.Controls.Count > 0)
            {
                stringToAppend = ((LinkButton)tc.Controls[0]).Text;
                sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");
            }
            else
            {
                stringToAppend = tc.Text;
                sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");
            }   
        }
        sb.Append("\\n");
    }
}

gvClass_PageIndexChanging(null, new GridViewPageEventArgs(currentPage));

// Javascript that sets the clipboard to the stringbuilders value
csm.RegisterClientScriptBlock(typeof(Page), "copy",
"<script language=\"javascript\" type=\"text/javascript\"> " +
"window.clipboardData.setData(\"Text\", \"" + sb.ToString() + "\"); " +
"</script>");

Here's some code to help you guys out with putting a grid view's data into the clipboard, which lets you paste into Excel.

ClientScriptManager csm;
StringBuilder sb;
string stringToAppend;
int currentPage;

csm = Page.ClientScript;
sb = new StringBuilder();

// headers
foreach (DataControlField dcf in gvClass.Columns)
{
    sb.Append(dcf.HeaderText + "\t");
}

sb.Append("\\n");
currentPage = gvClass.PageIndex;

for (int i = 0; i < gvClass.PageCount; i++)
{
    gvClass_PageIndexChanging(null, new GridViewPageEventArgs(i));

    foreach (GridViewRow gvr in gvClass.Rows)
    {
        foreach (TableCell tc in gvr.Cells)
        {
            if (tc.Controls.Count > 0)
            {
                stringToAppend = ((LinkButton)tc.Controls[0]).Text;
                sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");
            }
            else
            {
                stringToAppend = tc.Text;
                sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");
            }   
        }
        sb.Append("\\n");
    }
}

gvClass_PageIndexChanging(null, new GridViewPageEventArgs(currentPage));

// Javascript that sets the clipboard to the stringbuilders value
csm.RegisterClientScriptBlock(typeof(Page), "copy",
"<script language=\"javascript\" type=\"text/javascript\"> " +
"window.clipboardData.setData(\"Text\", \"" + sb.ToString() + "\"); " +
"</script>");
早乙女 2024-10-14 15:35:45

将内容复制到剪贴板的唯一可靠方法是使用 Flash Player。这是一个开始:

http://beckelman.net/post/2009/01/22/Copy-to-Clipboard-with-ZeroClipboard-Flash-10-and-jQuery.aspx

The only reliable way to copy stuff to the clipboard is to use the Flash Player. Here's a start:

http://beckelman.net/post/2009/01/22/Copy-to-Clipboard-with-ZeroClipboard-Flash-10-and-jQuery.aspx

七月上 2024-10-14 15:35:45

听起来您对 gridview 感到困惑。和你所描述的没有任何关系。您需要谷歌一下如何使用 C# 将数据导出到 Excel。您可以创建一个链接或按钮,显示下载 excel 文件,该按钮将绑定到您的 c# 方法。如果您在整个网站上使用此方法,您可以执行一次,并可能使其使用参数来确定要将哪个数据库表导出到 Excel。

Ps 如果您正在寻找比 gridview 更多的功能,我建议您看看我对这篇关于创建您自己的文章的回答。 如何在每个 gridview 行项目上显示 gridview 中数据库的弹出菜单?

您绝对需要将其复制到剪贴板吗?也许可以做到这一点,但需要大量编码。您正在谈论将数据粘贴到多个字段而不是一个字段中。我想你必须为 Excel 创建一个扩展,它知道剪贴板数据来自数据库表并弄清楚如何填充工作表。

It sounds like you are confused about the gridview. It has nothing to do with what you are describing. You need to google how to export data to excel with C#. You could create a link or button that says download excel file and the button would be tied to your c# method. If you use this method across your website you could make it once and possibly make it use a parameter to figure out which database table it is exporting to excel.

P.s. In case you are looking for more functionality than the gridview I suggest you take a look at my answer to this post about creating your own. How to show pop up menu from database in gridview on each gridview row items?

Do you absolutely need to copy this to the clipboard? It's probably possible to do this but would take a lot coding. You are talking about pasting data into multiple fields rather than in one field. I imagine you would have to create an extension for excel that would know that the clipboard data is from database table and figure out how to populate the sheet.

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