Oct 7th, 2009 | 1 Comment

Following code helps you to import grid values to excel,

//Import System.IO in your application for StreamWriter Object

//note: excel_file represents the complete physical address of excel file eg  C:/myexcel.xls
public  void export_datagridview_to_excel(DataGridView dgv, string excel_file)
{
int cols;
//open file
StreamWriter wr = new StreamWriter(excel_file);

//determine the number of columns and write columns to file
cols = dgv.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dgv.Columns[i].Name.ToString().ToUpper() + “\t”);
}

wr.WriteLine();

//write rows to excel file
for (int i = 0; i < (dgv.Rows.Count - 1); i++)
{
for (int j = 0; j < cols; j++)
{
if (dgv.RowsIdea.Cells[j].Value != null)
wr.Write(dgv.Rows[i].Cells[j].Value + “\t”);
else
{
wr.Write(”\t”);
}
}

wr.WriteLine();
}

//close file
wr.Close();
}

Written by Ajay Matharu

October 7th, 2009 at 1:19 pm

  • http://www.getbelle.com Fireebmatalia

    Hello,
    I have developed a new clean web 2.0 wordpress theme.

    Has 2 colours silver and blue, has custom header(colour or image).
    I am curently working on it, so if you have suggestions let me know.

    You can view live demo and download from here http://www.getbelle.com
    If you found bug reports or you have suggestions pm me.
    Wish you a happing using.

    many thanks to [url=http://www.usainstantpayday.com/]USAInstantPayDay.com[/url] for paying the hosting and developement of the theme

    Fireebmatalia