DataGridView To Excel With Colored Cells

前端 未结 2 847
有刺的猬
有刺的猬 2021-01-16 14:38

I looked a lots of example and demo but I could not to it.

I\'m trying to convert datagridview to excel.

My Results http://i.imgur.com/ujvGiXX.png

Bu

2条回答
  •  没有蜡笔的小新
    2021-01-16 15:16

    update the internal loop to be:

    for (i = 0; i <= dataGridView1.RowCount - 2; i++)
                {
                    for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                    {
                        Range range = (Range)xlWorkSheet.Cells[i + 1, j + 1];
                        xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();
                        range.Interior.Color = System.Drawing.ColorTranslator.ToOle(dataGridView1.Rows[i].DefaultCellStyle.BackColor );
    
                    }
                }
    

    don't forget:

    using Microsoft.Office.Interop.Excel;
    

    see:
    Change the background of Cells with C#

提交回复
热议问题