如何使用C#将数据从DataGridView传输到具有匹配的颜色和字体的Excel工作表? [英] How to transfer data from DataGridView to an Excel sheet with matching Colour and Font using C#?

查看:102
本文介绍了如何使用C#将数据从DataGridView传输到具有匹配的颜色和字体的Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用以下代码,我能够将数据从DataGridView传输到
Excel工作表.
但是它仅传输文本数据.如果DataGridView有一些单元格
颜色字体具有不同的颜色,则不会转移颜色和
字体.
因此,如何传输具有相同颜色字体的数据.请
如果知道,请回复
在此先感谢

我的代码是:----

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");
           }
       }

推荐答案

您在这里

导出为带有网格颜色的Excel使用C#的Winforms [ ^ ]
Here you go

Export to Excel with Grid Color in Winforms using C#[^]


这篇关于如何使用C#将数据从DataGridView传输到具有匹配的颜色和字体的Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆