Some steps to export DataGridView to Excel:
1) Download and install the free O2003PIA.exe file
2) In Visual Studio, click on Add Reference (in the Solution Explorer pane)
3) On the COM tab, locate Microsoft Excel 11.0 Object Library and click Select.
4) Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click “Yes”.
Write the class:
using System;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// create a simple DataSet
public DataTable mydt()
{
System.IO.StringWriter sw;
string output;
DataTable dt = new DataTable();
DataColumn dtcol = new DataColumn();
dtcol.ColumnName = "name";
dtcol.DataType = Type.GetType("System.Int32");
dt.Columns.Add(dtcol);
DataColumn dtcol2 = new DataColumn();
dtcol2.ColumnName = "random";
dtcol2.DataType = Type.GetType("System.Int32");
dt.Columns.Add(dtcol2);
DataColumn dtcol3 = new DataColumn();
dtcol3.ColumnName = "id";
dtcol3.DataType = Type.GetType("System.Int32");
dtcol3.AutoIncrement = true;
dt.Columns.Add(dtcol3);
for (int i = 100; i <= 200; i++)
{
dt.Rows.Add(i, i + 1000);
}
foreach (DataRow row in dt.Rows)
{
sw = new System.IO.StringWriter();
foreach (DataColumn col in dt.Columns)
sw.Write(row[col].ToString() + "\t");
output = sw.ToString();
Console.WriteLine(output);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds.Tables[0];
}
// add some action to the first button
private void button1_Click(object sender, EventArgs e)
{
WindowsApplication1.Form1 lp = new WindowsApplication1.Form1();
mydt();
dataGridView1.DataSource = mydt();
}
// add another button in order to parse the contents of the DataGridView to Excel.
// This method saves the Excel file in the "Desktop" folder of the current user. The Excel file will have the current timestamp in it's filename.
private void button2_Click(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string dtVariable = DateTime.Now.ToString("_yyyy-MM-dd-hhmm");
string fileName = path + "\\" + "ExportedData"+ dtVariable + ".xls";
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 <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
xlWorkBook.SaveAs(fileName, 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("Excel file created , you can find the file @ " + fileName + "");
}
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();
}
}
}
}
// That's all !
Recent Comments