2012-08-02 33 views
0

我試圖導出數據從MS Excel表格數據表使用C#VS 2010 WinForms應用程序中的數據表。從excel導出數據到C#中的文本顏色的數據表格#

我想要根據文本forecolor從Excel中導出一些行,例如文本顏色是黑色,綠色和紅色。如果文本顏色是綠色,那麼我想排除該行被導出。

請讓我知道如何做到這一點。

回答

0

您應該遍歷Excel中的行並在導出前檢查文本顏色。首先打開Excel文件。

  Microsoft.Office.Interop.Excel.Application App = new Microsoft.Office.Interop.Excel.Application(); 
      App.Visible = false; 
      App.WindowState = Excel.XlWindowState.xlMinimized; 
      App.ShowStartupDialog = false; 

      Excel.Workbook WorkBook = App.Workbooks.Open(File, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 

這是好事,讓正在使用第一區域:

Excel.Range Area = worksheet.get_Range("A1", worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing)); 

迭代:

   for (int R = 1; R <= Area.Rows.Count; R++) 
       { 
       Excel.Range Row = ((Excel.Range)Area[R, "A"]); 
       if(Row.Font.Color != Excel.XlRgbColor.rgbGreen) 
       { 
        // Get data from the cells and include into a 
        // collection of items that represents data to be exported. 
       } 
       } 
+0

我無法找到枚舉Excel.XlRgbColor情況下,即使我已經添加了Interop.Excel dll參考和命名空間。 – 2012-08-03 03:40:21

+0

請嘗試: 使用Excel = Microsoft.Office.Interop.Excel; – Demir 2012-08-06 10:05:59

相關問題