.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

how to Export data Grid View values into Excel using C#

Posted By: Naim Khan     Posted Date: August 18, 2010    Points:2   Category :Windows Application
hello friends,
i have very big problem in C#... actually i have Export the Data Grid view into Excel.. but column name is not Exported so i don't know how can i export the Column name also... i m showing you code now you just tell me where is my Mistake.....
//code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using System.Reflection;

namespace PolicySoft
{
public partial class ExportDateWise : Form
{

private DataSet ds;

public ExportDateWise()
{
InitializeComponent();
}


public void DataGridViewExport(String filepath)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= gv_show_dateWiseDetails.RowCount - 1; i++)
{
for (j = 0; j <= gv_show_dateWiseDetails.ColumnCount - 1; j++)
{
DataGridViewCell cell = gv_show_dateWiseDetails[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
xlWorkBook.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Export SuccessFully");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;
}
catch (Exception ex)
{

obj = null;

MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

}
finally
{

GC.Collect();
}
}

private void btn2Excel_Click(object sender, EventArgs e)
{

saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (!saveFileDialog1.FileName.Equals(String.Empty))
{
FileInfo f = new FileInfo(saveFileDialog1.FileName);
if (f.Extension.Equals(".xls"))
{
DataGridViewExport(saveFileDialog1.FileName);
}
else
{
MessageBox.Show("Invalid file type");
}
}
else
{
MessageBox.Show("You did pick a location to save file to");
}
}
}

private void btn_ok_Click(object sender, EventArgs e)
{
try
{
ds = new DataSet();
MySqlConnection con1 = new MySqlConnection(StartUp.database);
con1.Open();
string str = "call ps_show_details_date_wise('" + dtp_fromDate.Value.ToString("yyyy-MM-dd") + "','" + dtp_toDate.Value.ToString("yyyy-MM-dd") + "')";
MySqlDataAdapter da = new MySqlDataAdapter(str, con1);
da.Fill(ds, "t_transaction");
gv_show_dateWiseDetails.DataSource = ds.Tables[0];
gv_show_dateWiseDetails.ReadOnly = true;
con1.Close();
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

thanks in Advance

Responses
Author: Naim Khan             
Posted Date: August 18, 2010     Points: 5   

thank you so much for Reply sir,

But i want to make this Export utility on Winodws Form not in ASP.NET... so please help me on Windows Application Using C#....


Naim Khan


Software Developer
khan.naim786@yahoo.com
khan.naeem1987@gmail.com
Author: Syed Shakeer Hussain             
Posted Date: August 18, 2010     Points: 5   

Hi,
check the below link
http://www.dotnetspark.com/kb/2100-export-gridviewdata-to-excelsheet-using.aspx

You can do in the same way in Widnows also

Author: Suderson Sarathy             
Posted Date: November 19, 2010     Points: 5   

I hope the above method will be horrible if you have 1 Lakh records to get\Store between Excel and C#.
So, I've given a link to the file which does the same thing in very fast manner.


This is in Windows Application. Definitely this will work with minor changes. You need to change the select query and update query based on your need. Nothing else is needed

Please have a look into it.


https://secure.filesanywhere.com/fs/v.aspx?v=8a696b885d6672a6716d


Thanks !!

Author: Narayanan             
Posted Date: April 11, 2012     Points: 20   


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend