快速導出Excel

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;


using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace mis.Prod_HR
{
    class IOExcel
    {
        public static void ExportExcel(object[,] dataArray, int startRow, int startCol)
        {
            Microsoft.Office.Interop.Excel.Application oXL;
            Microsoft.Office.Interop.Excel.Workbook oWB;
            Microsoft.Office.Interop.Excel.Worksheet oSheet;

            oXL = new Microsoft.Office.Interop.Excel.Application();
            oWB = (Microsoft.Office.Interop.Excel.Workbook)(oXL.Workbooks.Add(Missing.Value));
             //再加2個WorkSheet
            //oXL.Sheets.Add(Missing.Value, Missing.Value, 2, Missing.Value);

            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];

            ExportExcel(dataArray, startRow, startCol);

            oSheet.Cells.EntireColumn.AutoFit();
            oXL.Visible = true;
            oXL.UserControl = true;
        }

        public static void ExportExcel(System.Data.DataTable dt, int startRow, int startCol)
        {
            int rowCount = dt.Rows.Count;
            int colCount = dt.Columns.Count;

            object[,] arr = new object[rowCount, colCount];
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    arr[i, j] = dt.Rows[i][j];
                }
            }
            ExportExcel(arr,startRow, startCol);
        }

        public static void ExportExcel(object[,] dataArray, Microsoft.Office.Interop.Excel.Worksheet excelSheet, int startRow, int startCol)
        {
            int rowCount = dataArray.GetLength(0);
            int colCount = dataArray.GetLength(1);

            //"A1"
            excelSheet.get_Range(excelSheet.Cells[startRow,startCol], excelSheet.Cells[startRow+rowCount, startCol+colCount]).Value2 = dataArray;
        }

        public static void ExportExcel(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet excelSheet, int startRow, int startCol)
        {
            int rowCount = dt.Rows.Count;
            int colCount = dt.Columns.Count;

            object[,] arr = new object[rowCount, colCount];
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    arr[i, j] = dt.Rows[i][j];
                }
            }
            ExportExcel(arr, excelSheet,startRow,startCol);
        }
        public static void ExportExcel(DataRow[] dataArray, Microsoft.Office.Interop.Excel.Worksheet excelSheet, int startRow, int startCol)
        {
            int rowCount = dataArray.Length;
            int colCount = dataArray[0].Table.Columns.Count;

            object[,] arr = new object[rowCount, colCount];
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    arr[i, j] = dataArray[i][j];
                }
            }
            ExportExcel(arr, excelSheet, startRow, startCol);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值