C#vs的windows窗体的datagridview数据的导出
效果
代码
1.在导出事件(daochu_Click)中编辑代码:
private void daochu_Click(object sender, EventArgs e)
{
string fileName = "";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xlsx";
saveDialog.Filter = "Excel文件|*.xlsx";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel", "提示");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
string yinhao = "'";
//写入标题
worksheet.Cells[1, 1] = yinhao + dataGridView1.Columns[1].HeaderText.ToString().Trim();
worksheet.Cells[1, 2] = yinhao + dataGridView1.Columns[2].HeaderText.ToString().Trim();
worksheet.Cells[1, 3] = yinhao + dataGridView1.Columns[3].HeaderText.ToString().Trim();
worksheet.Cells[1, 4] = yinhao + dataGridView1.Columns[4].HeaderText.ToString().Trim();
worksheet.Cells[1, 5] = yinhao + dataGridView1.Columns[5].HeaderText.ToString().Trim();
worksheet.Cells[1, 6] = yinhao + dataGridView1.Columns[6].HeaderText.ToString().Trim();
worksheet.Cells[1, 7] = yinhao + dataGridView1.Columns[7].HeaderText.ToString().Trim();
worksheet.Cells[1, 8] = yinhao + dataGridView1.Columns[8].HeaderText.ToString().Trim();
worksheet.Cells[1, 9] = yinhao + dataGridView1.Columns[9].HeaderText.ToString().Trim();
worksheet.Cells[1, 10] = yinhao + dataGridView1.Columns[10].HeaderText.ToString().Trim();
//写入数值
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
worksheet.Cells[r + 2, 1] = yinhao + dataGridView1.Rows[r].Cells[1].Value.ToString().Trim();
worksheet.Cells[r + 2, 2] = yinhao + dataGridView1.Rows[r].Cells[2].Value.ToString().Trim();
worksheet.Cells[r + 2, 3] = yinhao + dataGridView1.Rows[r].Cells[3].Value.ToString().Trim();
worksheet.Cells[r + 2, 4] = yinhao + dataGridView1.Rows[r].Cells[4].Value.ToString().Trim();
worksheet.Cells[r + 2, 5] = yinhao + dataGridView1.Rows[r].Cells[5].Value.ToString().Trim();
worksheet.Cells[r + 2, 6] = yinhao + dataGridView1.Rows[r].Cells[6].Value.ToString().Trim();
worksheet.Cells[r + 2, 7] = yinhao + dataGridView1.Rows[r].Cells[7].Value.ToString().Trim();
worksheet.Cells[r + 2, 8] = yinhao + dataGridView1.Rows[r].Cells[8].Value.ToString().Trim();
worksheet.Cells[r + 2, 9] = yinhao + dataGridView1.Rows[r].Cells[9].Value.ToString().Trim();
worksheet.Cells[r + 2, 10] = dataGridView1.Rows[r].Cells[10].Value;
//库存<=3,导出后在Excel中也变红
for (int c = 1; c < dataGridView1.Columns.Count; c++)
{
if (Convert.ToInt32(dataGridView1.Rows[r].Cells[9].Value) <= 3)
{
worksheet.Rows[r + 2].Interior.Color = ColorTranslator.ToOle(Color.Red);
}
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
MessageBox.Show(fileName + "导出成功", "提示", MessageBoxButtons.OK);
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName); //fileSaved = true;
}
catch (Exception ex)
{//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();//强行销毁 }
}
2.完成。