Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
           }
       }
Posted
Updated 14-Jul-11 1:14am
v3

1 solution

 
Share this answer
 
Comments
Espen Harlinn 17-Jul-11 17:08pm    
Spot on, my 5 :)
Harish Kumar Bansal 18-Jul-11 3:54am    
Hi thatraja
the page provided you works on Microsoft Excel.
But how to work on Open Office Excel?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900