【发布时间】:2015-10-20 09:29:56
【问题描述】:
我在 Winform C# 的 DataGridView 中创建了一个Shift Roster。 This 是我从 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);
}
请帮帮忙....
【问题讨论】:
-
您使用什么库或 dll 进行 excel 导出?
-
我正在使用 Microsoft.Office.Interop.Excel
-
@MianSalmanNasir 也要看看 EPPlus。
-
那是EPPlus。
标签: c# excel winforms datagridview export-to-excel