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);
}
}
}