C#Winform导出Excel

View Code 
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using Microsoft.Office.Interop.Excel;
 namespace SaleApplication.Class
 {
     class ExportExcelOfWinform
     {
         public static bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel)
         {
             int titleColumnSpan = 0;//标题的跨列数
             string fileName = "";//保存的excel文件名
             int columnIndex = 1;//列索引
             if (dgv.Rows.Count == 0)
                 return false;
             /*保存对话框*/
             SaveFileDialog sfd = new SaveFileDialog();
             sfd.Filter = "导出Excel(*.xls)|*.xls";
             sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");
             
             if (sfd.ShowDialog() == DialogResult.OK)
             {
                 fileName = sfd.FileName;
                 /*建立Excel对象*/
                 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                 if (excel == null)
                 {
                     MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
                     return false;
                 }
                 try
                 {
                     excel.Application.Workbooks.Add(true);
                     excel.Visible = isShowExcel;
                     /*分析标题的跨列数*/
                     foreach (DataGridViewColumn column in dgv.Columns)
                     {
                         if (column.Visible == true)
                             titleColumnSpan++;
                     }
                     /*合并标题单元格*/
                     Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                     //worksheet.get_Range("A1", "C10").Merge();            
                     worksheet.get_Range(worksheet.Cells[11as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
                     /*生成标题*/
                     excel.Cells[11] = title;
                     (excel.Cells[11as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//标题居中
 
//生成字段名称
                     columnIndex = 1;
                     for (int i = 0; i < dgv.ColumnCount; i++)
                     {
                         if (dgv.Columns[i].Visible == true)
                         {
                             excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
                             (excel.Cells[2, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//字段居中
                             columnIndex++;
                         }
                     }
                     //填充数据              
                     for (int i = 0; i < dgv.RowCount; i++)
                     {
                         columnIndex = 1;
                         for (int j = 0; j < dgv.ColumnCount; j++)
                         {
                             if (dgv.Columns[j].Visible == true)
                             {
                                 if (dgv[j, i].ValueType == typeof(string))
                                 {
                                     excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value.ToString();
                                 }
                                 else
                                 {
                                     excel.Cells[i + 3, columnIndex] = dgv[j, i].Value.ToString();
                                 }
                                 (excel.Cells[i + 3, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignLeft;//字段居中
                                 columnIndex++;
                             }
                         }
                     }
                     worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  
                 }
                 catch { }
                 finally
                 {  
                     excel.Quit(); 
                     excel = null;
                     GC.Collect();
                 }
                 //KillProcess("Excel");
                 return true;
             }
             else
             {
                 return false;
             }
         }
         private static void KillProcess(string processName)//杀死与Excel相关的进程
         {
             System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程
             try
             {
                 foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                 {
                     if (!thisproc.CloseMainWindow())
                     {
                         thisproc.Kill();
                     }
                 }
             }
             catch (Exception Exc)
             {
                 throw new Exception("", Exc);
             }
         }
 
     }
 }
原文地址:https://www.cnblogs.com/sydeveloper/p/2406558.html