我正在使用一个Windows应用程序。我需要找出如何在Excel中以不同的颜色和样式突出显示数据。我正在使用C#将数据导出到Excel。
这是我用来将DataTable导出到Excel的代码:
private void btnExportexcel_Click(object sender, EventArgs e)
{
oxl = new Excel.Application();
oxl.Visible = true;
oxl.DisplayAlerts = false;
wbook = oxl.Workbooks.Add(Missing.Value);
wsheet = (Excel.Worksheet)wbook.ActiveSheet;
wsheet.Name = "Customers";
DataTable dt = clsobj.convert_datagrid_orderlist_to_datatable(dvgorderlist);
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
wsheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
range = wsheet.get_Range(wsheet.Cells[1, 1],
wsheet.Cells[rowCount, dt.Columns.Count]);
range.EntireColumn.AutoFit();
}
wsheet = null;
range = null;
}