10

我想问你们,如果单元格1不为空,如何在Excel表格中将一行的颜色更改为红色。

XX     YY     ZZ
-----------------
aa     bb     cc
aa1    bb1    cc1
aa2           cc2
aa3    bb3    cc3
aa4          

Excel.Application xlApp;
Excel. Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

我非常感谢

4

2 回答 2

18

试一试,我已经对其进行了测试,并且可以正常工作:

Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(@"C:\Test\Whatever.xlsx");
Excel.Worksheet worksheet = workbook.ActiveSheet;

Excel.Range usedRange = worksheet.UsedRange;

Excel.Range rows = usedRange.Rows;

int count = 0;

foreach (Excel.Range row in rows)
{
    if (count > 0)
    {
        Excel.Range firstCell = row.Cells[1];

        string firstCellValue = firstCell.Value as String;

        if (!string.IsNullOrEmpty(firstCellValue))
        {
            row.Interior.Color = System.Drawing.Color.Red;
        }
    }

    count++;
}

workbook.Save();
workbook.Close();

application.Quit();

Marshal.ReleaseComObject(application);
于 2012-10-04T13:06:13.723 回答
2
Excel.Application xlAppToExport = new Excel.Application();
xlAppToExport.Workbooks.Add("");
Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);
xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];

xlWorkSheetToExport.Range["A5:F5"].EntireRow.Interior.Color = System.Drawing.Color.Gray;
于 2017-11-11T04:39:45.660 回答