-2

我正在写一个工作正常的 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
    }
}

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

4

1 回答 1

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
于 2012-08-06T09:13:49.343 回答