gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:23 25 4
gpt4 key购买 nike

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

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

最佳答案

这里有一些代码可以帮助你们将 GridView 的数据放入剪贴板,这样您就可以粘贴到 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>");

关于c# - 如何将 ASP.NET GridView 复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4454829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com