将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循环和范围,应用颜色。
感谢大家的帮助
- System.OutOfMemoryException将具有单个大型DataGrid的页面导入到Excel中(快速修复)
- 是否有可能编写一个将通过Excel文档运行数据的服务?
- 两个不同的expression式导致c#和excel
- 如何使用Microsoft.Office.Interop.Excel从Excel导入到DataSet?
- 我想在Excel中将2000个数据单元与3000个其他数据单元进行比较,但这需要很长时间
- 在Excel中将数据写入单元格,C#
- 如何通过其列名获取Excel中的列号
- 当有166,110行时,Microsoft ACE OLEDB连接创build空的Excel
- asp.net检查一个来自Response.OutputStream的文件是否被保存/打开