using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Web;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
/*
* 开发人员:Hisen
* 时间:2008年11月24日
* 功能:将数据导出Excel
*
*/
namespace XT.LiTree.Logic
{
public class ExcelExport
{
private ExcelExport()
{ }
private static ExcelExport _instance = null;
public static ExcelExport Instance
{
get
{
if (_instance == null) _instance = new ExcelExport();
return _instance;
}
}
/// <summary>
/// DataTable直接导出Excel,此方法会把DataTable的数据用Excel打开,再自己手动去保存到确切的位置
/// </summary>
/// <param name="dt">要导出Excel的DataTable</param>
/// <returns></returns>
public bool DoExport(DataTable dt)
{
Application app = new ApplicationClass();
if (app == null)
{
throw new Exception("Excel无法启动");
}
app.Visible = true;
Workbooks wbs = app.Workbooks;
Workbook wb = wbs.Add(Missing.Value);
Worksheet ws = (Worksheet)wb.Worksheets[1];
int cnt = dt.Rows.Count;
int columncnt = dt.Columns.Count;
// *****************获取数据********************
object[,] objData = new Object[cnt + 1, columncnt]; // 创建缓存数据
// 获取列标题
for (int i = 0; i < columncnt; i++)
{
objData[0, i] = dt.Columns[i].ColumnName;
}
// 获取具体数据
for (int i = 0; i < cnt; i++)
{
System.Data.DataRow dr = dt.Rows[i];
for (int j = 0; j < columncnt; j++)
{
C#导出EXCEL(DataTable导出EXCEL)
最新推荐文章于 2023-11-18 20:24:06 发布