2015-10-20 34 views
1

我在Winform C#中的DataGridView中製作了Shift RosterThis是我從DataGridView導出的excel表格截圖。導出DataGridVeiw在C#中使用單元格背景色優於

我需要導出DataGridView,因爲它是Excel工作表維護字體顏色和背景顏色也我不想導出的第一列DataGridView到Excel是不可見的。

我已經使用下面的代碼將DataGridView導出到Excel。

using (ExcelPackage pck = new ExcelPackage(file)) 
{ 
    // Add a new worksheet to the empty workbook 
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1"); 
    // Load data from DataTable to the worksheet 
    ws.Cells["A1"].LoadFromDataTable(((DataTable)gvShift.DataSource), true); 
    ws.Cells.AutoFitColumns(); 

    // Add some styling 
    using (ExcelRange rng = ws.Cells[1, 1, 1, gvShift.Columns.Count]) 
    { 
     rng.Style.Font.Bold = true; 
     rng.Style.Fill.PatternType = ExcelFillStyle.Solid; 
     rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189)); 
     rng.Style.Font.Color.SetColor(System.Drawing.Color.White); 
    } 

請幫助請....

+0

什麼庫或DLL用於Excel導出? – Kira

+0

我使用的Microsoft.Office.Interop.Excel –

+0

@MianSalmanNasir待辦事項看看EPPlus了。 – Yahya

回答

0

可以使用cells.interior.color

Range range = 
      oSheet.get_Range("A" + redRows.ToString(), "J" 
            + redRows.ToString()); 

    range.Cells.Interior.Color = System.Drawing.Color.Red; 

更多信息檢查Microsoft interop excel to format excel cells

+0

我需要導出DataGridView單元格顏色與數據一起出色。 –

+0

讓你需要的GridView行和閱讀背景色屬性像range.Cells.Interior.Color = Gridview1.Row [rowIndex位置] .Cells [cellindex] .BackColor – Laxmikant

0

我得到了解決,我只是去雖然我從中設置顏色的來源,但將每個單元格或DataGridView放入循環和範圍中,應用顏色。

感謝各位的幫助

相關問題