if (dataGridView1.DataSource != null && dataGridView1.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//导出到execl
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "导出Excel (*.xls)|*.xls";
saveFileDialog.FileName = SelectCardTree.CardNodeName;
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
System.Reflection.Missing miss = System.Reflection.Missing.Value;
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = SelectCardTree.CardNodeName;
#region 向Excel中插入数据
//生成列名称 这里i是从1开始的 因为我第0列是个隐藏列ID 没必要写进去
excel.Cells[1, 1] = "序号";
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
excel.Cells[1, i + 2] = dataGridView1.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < dataGridView1.RowCount; i++)
{
excel.Cells[i + 2, 1] = (i + 1).ToString();
// 原因如上 每个人需求不一样
for (int j = 1; j <= dataGridView1.ColumnCount; j++)
{
if (dataGridView1[j - 1, i].Value == null)
{
excel.Cells[i + 2, j + 1] = "";
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView1[j - 1, i].Value.ToString();
}
}
}
#endregion
#region 设置Excel表格样式
Microsoft.Office.Interop.Excel.Range titleRange = sheet.get_Range("A1", "E1");
titleRange.Activate();
titleRange.Font.Bold = true;
Microsoft.Office.Interop.Excel.Range ContentRange = sheet.get_Range("A1", "E" + (dataGridView1.Rows.Count + 1));
ContentRange.Activate();
ContentRange.Font.Name = "宋体";
ContentRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平居中
ContentRange.VerticalAlignment = XlVAlign.xlVAlignCenter;//垂直居中
ContentRange.Borders.LineStyle = XlLineStyle.xlContinuous;//设置边框
ContentRange.Borders.Weight = XlBorderWeight.xlThin;//设置边框粗细
ContentRange.Borders.LineStyle = XlLineStyle.xlContinuous;
ContentRange.Borders.ColorIndex = 49;
ContentRange.EntireColumn.AutoFit();
ContentRange.EntireRow.AutoFit();
#endregion
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// System.Diagnostics.Process.Start(strName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示");
}
finally
{
excel.Quit();
excel = null;
}
}
else
{
MessageBox.Show("没有需要导出的数据信息!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}