我的图片上似乎有一个datagridview。我正在使用Windows窗体应用程序。我的某些行背景是红色的,所以我想将datagridview导出到excel。
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
object Missing = Type.Missing;
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Missing);
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
int StartCol = 1;
int StartRow = 1;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow, StartCol + j];
myRange.Value2 = dataGridView1.Columns[j].HeaderText;
}
StartRow++;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
try
{
DataGridViewCell cell = dataGridView1[j, i];
sheet1.Cells[i + 2, j + 1] = cell.Value;
}
catch
{
MessageBox.Show("error");
}
}
}
此代码导出正确,但是我不知道如何在红色背景下导出