Solved

how to export data from selected columns in datagridview to excel in windows application using c# 2008?

Posted on 2011-03-16
9
1,052 Views
Last Modified: 2013-12-17
how to export data from selected columns in datagridview to excel in windows application using c# 2008
0
Comment
Question by:NeetD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35154247
0
 

Author Comment

by:NeetD
ID: 35154851
I have been created windows application in Visual Studio 2008 using C# and MS Access for database.
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.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35155709
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.HtmlTextWriter(tw)
Dim frm As HtmlForm = new HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "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
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 500 total points
ID: 35162749
NeetD: Didn't the links that I gave above help you?

Sid
0
 

Author Closing Comment

by:NeetD
ID: 35165111
I have been refer those links and done some modification to get result.
0
 

Author Comment

by:NeetD
ID: 35165153
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#?
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35165223
Post your complete code please.

Sid
0
 

Author Comment

by:NeetD
ID: 35171296
DataGridView1.RowHeadersVisible = false;  
DataGridView1.SelectAll();
Clipboard.SetDataObject(DataGridView1.GetClipboardContent(), true);
StreamWriter sw = new StreamWriter("d:\\ReservationDetail.xls");
sw.Write(Clipboard.GetText());
sw.Flush();
sw.Close();
DataGridView1.ClearSelection();  
DataGridView1.RowHeadersVisible = true;
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35172061
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
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question