2012-10-04 14 views
8

セル1がnullでない場合、Excelテーブルで行の色を赤に変更する方法をお聞きしたいと思います。c#特定の行の色を変更するにはどうすればいいですか?

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

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

私は非常にthankfull

+0

? –

+0

Microsoft.Office.Interop.Excel –

+0

xlWorkSheet.Cells ["A2"、 "B2"]を試しました。Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);それは私にエラーCOMExceptionを与える –

答えて

14

よこの打撃を与える、私はそれをテストしているし、それが動作します:

使用しているライブラリ
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); 
+0

私はここでエラーがあります:文字列firstCellValue = firstCell.Value; RuntimeBinderException:ダブルから文字列に変換できません –

+2

私は答えを改訂しました – JMK

+0

ありがとう、私はconvert.toStringでそれをしました –

0
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; 
+1

これがどのようにしてこの問題を解決するかを説明できる方が良いです。 – Suren

関連する問題