彩色Excel标题

时间:2012-08-06 08:59:10

标签: c# excel excel-interop

我正在写一个工作正常的Excel电子表格,但我需要将标题颜色设置为深蓝色并使字体颜色为白色,但是我有一些问题,想弄清楚如何实现这一点....这是我到目前为止的代码:

foreach (DataColumn c in DT.Columns)
{
    iColumnCount++;
    if(iRowCount == 0)
        Worksheet.Cells[1, iColumnCount] = c.ColumnName;
    else
        Worksheet.Cells[iRowCount, iColumnCount] = c.ColumnName;

    Worksheet.Columns.AutoFit(); //Correct the width of the columns
    //THIS IS WHERE I WANT TO COLOR THE HEADERS
}

foreach (DataRow r in DT.Rows)
{
    iRowCount++;
    iColumnCount = 0;
    foreach (DataColumn c in DT.Columns)
    {
        iColumnCount++;
        if(iRowCount == 1)
            Worksheet.Cells[iRowCount + 1, iColumnCount] = r[c.ColumnName].ToString();
        else
            Worksheet.Cells[iRowCount, iColumnCount] = r[c.ColumnName].ToString();

        Worksheet.Columns.AutoFit(); //Correct the width of the columns
    }
}

任何人都可以帮助我实现这个目标吗?

1 个答案:

答案 0 :(得分:2)

试试这个,它的工作

Worksheet.Range["A1","G1"].Interior.Color = Excel.XlRgbColor.rgbDarkBlue;
Worksheet.Range["A1","G1"].Font.Color = Excel.XlRgbColor.rgbWhite;
// where "A1" to "G1" is your header range