c#导出EXCEL

using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;

namespace RC.MSOffice
{
    /// <summary>
    /// Excel相关
    /// </summary>
    public class ExcelUtility
    {
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dataSet">需要导出Excel的DataSet</param>
        /// <param name="fileName">导出的路径</param>
        public static void ExportToExcel(DataSet dataSet, string fileName)
        {
            if (dataSet.Tables.Count == 0)
            {
                throw new Exception("DataSet中没有任何可导出的表。");
            }

            Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
            excelApplication.DisplayAlerts = false;

            Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);

            foreach (DataTable dt in dataSet.Tables)
            {
                Microsoft.Office.Interop.Excel.Worksheet lastWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
                Microsoft.Office.Interop.Excel.Worksheet newSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);

                newSheet.Name = dt.TableName;

                for (int col = 0; col < dt.Columns.Count; col++)
                {
                    newSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
                }

                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    for (int col = 0; col < dt.Columns.Count; col++)
                    {
                        newSheet.Cells[row + 2, col + 1] = dt.Rows[row][col].ToString();
                    }
                }
            }

            ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
            ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
            ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
            ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Activate();

            try
            {
                workbook.Close(true, fileName, System.Reflection.Missing.Value);
            }
            catch (Exception e)
            {
                throw e;
            }

            excelApplication.Quit();

            KillExcel();
        }

        private static void KillExcel()
        {
            Process[] excelProcesses = Process.GetProcessesByName("EXCEL");
            DateTime startTime = new DateTime();

            int processId = 0;
            for (int i = 0; i < excelProcesses.Length; i++)
            {
                if (startTime < excelProcesses[i].StartTime)
                {
                    startTime = excelProcesses[i].StartTime;
                    processId = i;
                }
            }

            if (excelProcesses[processId].HasExited == false)
            {
                excelProcesses[processId].Kill();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值