将DataGridVeiw导出为使用单元格背景颜色的C#优秀

我已经在Winform C#中的DataGridView中做了一个轮class名单 。 这是我从DataGridView导出的excel表格截图。

我需要导出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); } 

请帮助请….

你可以使用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来格式化Excel单元格

我得到了解决scheme,我只是从我设置颜色的地方去源,把每个单元格或DataGridView循环和范围,应用颜色。

感谢大家的帮助