NeetD
asked on
how to export data from selected columns in datagridview to excel in windows application using c# 2008?
how to export data from selected columns in datagridview to excel in windows application using c# 2008
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi, convert this code to C# then try it.
Sub doExcel(Source as Object, E as EventArgs)
If gv.Rows.Count.ToString + 1 < 65536 Then
gv.AllowPaging="False"
gv.DataBind
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWrit er(tw)
Dim frm As HtmlForm = new HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("conten t-disposit ion", "attachment;filename=" & "export.xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString ())
Response.End()
gv.AllowPaging="True"
gv.Databind
Else
LblError.Text="Too many rows - Export to Excel not possible"
End If
End Sub
Sub doExcel(Source as Object, E as EventArgs)
If gv.Rows.Count.ToString + 1 < 65536 Then
gv.AllowPaging="False"
gv.DataBind
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWrit
Dim frm As HtmlForm = new HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("conten
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString
Response.End()
gv.AllowPaging="True"
gv.Databind
Else
LblError.Text="Too many rows - Export to Excel not possible"
End If
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have been refer those links and done some modification to get result.
ASKER
now i am transferring selected column from datagridview and then export to excel but while doing this those unselected columns are also created with empty value in excel.
Now i want to know, how to delete null columns in excel using c#?
Now i want to know, how to delete null columns in excel using c#?
Post your complete code please.
Sid
Sid
ASKER
DataGridView1.RowHeadersVi sible = false;
DataGridView1.SelectAll();
Clipboard.SetDataObject(Da taGridView 1.GetClipb oardConten t(), true);
StreamWriter sw = new StreamWriter("d:\\Reservat ionDetail. xls");
sw.Write(Clipboard.GetText ());
sw.Flush();
sw.Close();
DataGridView1.ClearSelecti on();
DataGridView1.RowHeadersVi sible = true;
DataGridView1.SelectAll();
Clipboard.SetDataObject(Da
StreamWriter sw = new StreamWriter("d:\\Reservat
sw.Write(Clipboard.GetText
sw.Flush();
sw.Close();
DataGridView1.ClearSelecti
DataGridView1.RowHeadersVi
NeetD: I don't know C# as I code in vb.net but what I can see from your code is that this line
DataGridView1.SelectAll();
is selecting all the contents of the datagrid. I believe there is a click event of the grid where you can transfer the current column to Excel?
Sid
DataGridView1.SelectAll();
is selecting all the contents of the datagrid. I believe there is a click event of the grid where you can transfer the current column to Excel?
Sid
ASKER
I want to transfer selected column of datagidview to Excel with Header at runtime means client will select any column as per his choice and then export to excel.
and date should be in dateformat ,it sholudnot include time.