TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Naeem Khan
971
802
799.2k
Problem with Exporting data into Excel using C#
Aug 18 2010 3:37 AM
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);
}
}
}
}
Reply
Answers (
1
)
Transferring values between parent and child forms
Performance of the GridView control