一、实现目标:
    由一个内存表DataTable,导出字段名及其内容的完整EXCEL表格
二、实施步骤:
1、添加引用:
   这是非常生要的一步,很多人调试不成都是因为这步没做好:
   需要在你的解决方案中添加COM引用,选择 "Microsoft EXCEL ...."(根据版本有所不同),这是为下面的 EXCEL相关命名空间的引用做铺垫的;
   我用的EXCEL 2003,添加引用:          
interop.Microsoft.office.core.dll
Interop.Microsoft.Office.Interop.Excel.dll
(在CSDN能下载到)

2、命名空间引用部分:
   增加下面的引用内容:
   using Microsoft.Office.Interop.Excel;
3、定义函数:
 public static void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
        {
            if (tmpDataTable == null)
                return;
            int rowNum = tmpDataTable.Rows.Count;
            int columnNum = tmpDataTable.Columns.Count;
            int rowIndex = 1;
            int columnIndex = 0;
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            xlApp.DefaultFilePath = "";
            xlApp.DisplayAlerts = true;
            xlApp.SheetsInNewWorkbook = 1;
            Workbook xlBook = xlApp.Workbooks.Add(true);
            //将DataTable的列名导入Excel表第一行
            foreach (DataColumn dc in tmpDataTable.Columns)
            {
                columnIndex++;
                xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
            }
            //将DataTable中的数据导入Excel中
            for (int i = 0; i < rowNum; i++)
            {
                rowIndex++;
                columnIndex = 0;
                for (int j = 0; j < columnNum; j++)
                {
                    columnIndex++;
                    xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
                }
            }
            //xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
            xlBook.SaveCopyAs(strFileName);
        }
4、 使用实例:
       System.Data.DataTable dt = ……;   //准备好你的DataTable
       DataTabletoExcel(dt, "C:\\\\中国.XLS");   //调用自定义的函数,当然输出文件你可以随便写
三、测试环境:
    VS2005,EXCEL 2003 
    VS2008,EXCEL 2007

 

======================================================================

 

private void btnExportData_Click(object sender, EventArgs e)
        {
            printAll(dt);
        }

        private void printAll(System.Data.DataTable dt)
        {
            if (txtPath.Text == "")
            {
                ShowTip("请选择保存路径!",txtPath);
                return;
            }
            if (chkboxPatent.CheckedItems.Count ==0)
            {
                MessageBox.Show("请选择要导出的数据项目!");
                return;
            }
            if (dt == null)
            {
                MessageBox.Show("对不起,当前没有数据要导出!");
                return;
            }

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            try
            {
               
                //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
                Workbook box =  excel.Application.Workbooks.Add(true);
                //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写  
                excel.Visible = false;
                //生成Excel中列头名称  
                for (int i = 0; i < chkboxPatent.CheckedItems.Count; i++ )
                {
                    excel.Cells[1, i + 1] = chkboxPatent.CheckedItems[i]; //输出选中的列头名  
                }

                //把DataGridView当前页的数据保存在Excel中  
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int k = 0; k < chkboxPatent.CheckedItems.Count; k++)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                    if (hashColumns[chkboxPatent.CheckedItems[k].ToString()].ToString() == dt.Columns[j].ColumnName )
                                    {
                                        string str = dt.Rows[i][j].ToString();
                                        excel.Cells[i + 2, k + 1] = "'" + str;//i控制行,从Excel中第2行开始输出第一行数据,k控制列,从Excel中第1列输出第1列数据,"'" +是以string形式保存,所以遇到数字不会转成16进制
                                        break;
                                    }
                             }
                        }
                    }
                }
                //设置禁止弹出保存和覆盖的询问提示框  
                excel.DisplayAlerts = false;
                excel.AlertBeforeOverwriting = false;

                //保存execl文件
                box.SaveCopyAs(txtPath.Text);

                //确保Excel进程关闭  
                excel.Quit();
                excel = null;
                MessageBox.Show("导出数据成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "导出数据失败");
            }
            finally
            {
                excel.Quit();
                excel = null;
            }