多个Datable导出Excel(多个sheet表)

tableName为sheet表的表名。

        public static void DataSetToExcel(DataSet ds, string[] tableName, string Path)
        {

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            try
            {
                excel.Visible = false;
                //设置禁止弹出保存和覆盖的询问提示框
                excel.DisplayAlerts = false;
                excel.AlertBeforeOverwriting = false;
                //增加一个工作簿
                Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Add(true);
                //添加工作表
                Worksheet sheets = (Worksheet)book.Worksheets.Add(Missing.Value, Missing.Value, 20, XlSheetType.xlWorksheet);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    System.Data.DataTable table = ds.Tables[i];
                    //获取一个工作表

                    Worksheet sheet = book.Worksheets[i + 1] as Worksheet;
                    int rowIndex = 1;
                    int colIndex = 0;
                    foreach (DataColumn col in table.Columns)
                    {
                        colIndex++;
                        sheet.Cells[1, colIndex] = col.ColumnName;
                    }
                    foreach (DataRow row in table.Rows)
                    {
                        rowIndex++;
                        colIndex = 0;
                        foreach (DataColumn col in table.Columns)
                        {
                            colIndex++;
                            sheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                        }
                    }
                    sheet.Name = tableName[i];

                }
                删除多余Sheet
                for (int g = 1; g <= book.Worksheets.Count; g++)
                {
                    Worksheet sheet = book.Worksheets[g] as Worksheet;

                    if (sheet.Name.Length > 5 && sheet.Name.Substring(0, 5) == "Sheet")
                    {
                        sheet.Delete();
                        g--;
                    }
                }
                book.SaveAs(strExcelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                excel.Quit();
                excel = null;
                GC.Collect();

            }
            catch (System.Exception e)
            {

                //throw;
            }
            finally
            {
                KillExcelProcess(excel);//结束Excel进程                
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

        /// 
        /// 结束Excel进程     
        /// 
        private static void KillExcelProcess(Microsoft.Office.Interop.Excel.Application _ApplicationClass)
        {
            try
            {
                if (_ApplicationClass != null)
                {
                    int lpdwProcessId;
                    GetWindowThreadProcessId(new IntPtr(_ApplicationClass.Hwnd), out lpdwProcessId);
                    System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
                }
            }
            catch (System.Exception ex)
            {
                //ErrorLogManager.Log("Kill Excel Process", ex.Source, ex.StackTrace);
            }
        }

你可能感兴趣的:(C#)