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.
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.
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>");
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
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.