Try the code demonstrated here:

http://stackoverflow.com/questions/6196770/datagridview-export-to-excel


Regards,


Benj


On Jul 14, 7:10 pm, Harish Kumar <[email protected]> wrote:
> Hi,
> With below code, i am able to transfer data from DataGridView to an
> Excel sheet.
> But it only transfer the text data. If DataGridView has some cells
> with different colour and font, it does not transfer the colour and
> font.
>
> so how can i transfer the data with the same colour and font. please
> reply if you know
>
> thanks in advance
>
> my code is:----
>
> private void Excel_Click(object sender, EventArgs e)
>         {
>             if (dataGridView1.Rows.Count != 0)
>             {
>                 SaveFileDialog sfd = new SaveFileDialog();
>                 sfd.Filter = "Microsoft Excel 97/2000/XP(*.xls)|
> *.xls";
>                 sfd.FileName = "Report.xls";
>                 if (sfd.ShowDialog() == DialogResult.OK)
>                 {
>                     StreamWriter wr = new StreamWriter(sfd.FileName);
>
>                     int cols = dataGridView1.Columns.Count;
>                     for (int i = 0; i < cols; i++)
>                     {
>
> wr.Write(dataGridView1.Columns[i].Name.ToUpper() + "\t");
>                     }
>
>                     wr.WriteLine();
>
>                     for (int i = 0; i < (dataGridView1.Rows.Count -
> 1); i++)
>                     {
>                         for (int j = 0; j < cols; j++)
>                         {
>
> wr.Write(dataGridView1.Rows[i].Cells[j].Value + "\t");
>                         }
>                         wr.WriteLine();
>                     }
>                     wr.Close();
>                     label9.Text = "Your file has been successfully
> saved !";
>                 }
>             }
>             else
>             {
>                 MessageBox.Show("Error");
>             }
>         }

-- 
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Reply via email to