資源簡(jiǎn)介
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
/// <summary>
/// ExportToExcel 的摘要說(shuō)明
/// </summary>
public class ExportToExcel
{
public ExportToExcel()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private void KillExcelProcess()
{
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="dt">數(shù)據(jù)源</param>
/// <param name="tblName">表名(</param>
public void ToExcel(System.Data.DataTable dt,string tblName)
{
try
{
if (dt == null) return;
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel = dt.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="dt">數(shù)據(jù)源</param>
/// <param name="tblName">表名(</param>
public void ToExcel(System.Data.DataSet ds, string tblName)
{
try
{
if (ds == null) return;
System.Data.DataSet dsExcel = new System.Data.DataSet();
dsExcel = ds.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dsExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
public bool DataTableToExcel(System.Data.DataTable dt)
{
try
{
string FileName = HttpContext.Current.Server.MapPath("../Excel/") "催收案件記錄報(bào)表_" dt.Rows[0][2].ToString() ".xls";
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel = dt.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "催收案件記錄報(bào)表" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
return true;
}
catch
{
return false;
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
/// <summary>
/// ExportToExcel 的摘要說(shuō)明
/// </summary>
public class ExportToExcel
{
public ExportToExcel()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private void KillExcelProcess()
{
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="dt">數(shù)據(jù)源</param>
/// <param name="tblName">表名(</param>
public void ToExcel(System.Data.DataTable dt,string tblName)
{
try
{
if (dt == null) return;
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel = dt.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="dt">數(shù)據(jù)源</param>
/// <param name="tblName">表名(</param>
public void ToExcel(System.Data.DataSet ds, string tblName)
{
try
{
if (ds == null) return;
System.Data.DataSet dsExcel = new System.Data.DataSet();
dsExcel = ds.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dsExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
public bool DataTableToExcel(System.Data.DataTable dt)
{
try
{
string FileName = HttpContext.Current.Server.MapPath("../Excel/") "催收案件記錄報(bào)表_" dt.Rows[0][2].ToString() ".xls";
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel = dt.Copy();
string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "催收案件記錄報(bào)表" ".xls");
ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);
System.IO.FileInfo file = new System.IO.FileInfo(Path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流發(fā)送到客戶端
HttpContext.Current.Response.WriteFile(file.FullName);
return true;
}
catch
{
return false;
}
}
}
代碼片段和文件信息
//?#define?INCLUDE_WEB_FUNCTIONS
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Diagnostics;
using?System.Data;
using?System.Reflection;
using?DocumentFormat.Openxml.Packaging;
using?DocumentFormat.Openxml.Spreadsheet;
using?DocumentFormat.Openxml;
namespace?ExportToExcelFile
{
????//
????//??March?2014
????//??http://www.mikesknowledgebase.com
????//
????//??Note:?if?you?plan?to?use?this?in?an?ASP.Net?application?remember?to?add?a?reference?to?“System.Web“?and?to?uncomment
????//??the?“INCLUDE_WEB_FUNCTIONS“?definition?at?the?top?of?this?file.
????//
????//??Release?history
????//???-?Mar?2014:?
????//????????Now?writes?the?Excel?data?using?the?OpenxmlWriter?classes?which?are?much?more?memory?efficient.
????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????17487??2015-06-29?13:00??CreateExcelFile.cs
?????文件???????6034??2017-10-16?13:54??ExportToExcel.cs
-----------?---------??----------?-----??----
????????????????23521????????????????????2
- 上一篇:C# 文件加密工具源碼
- 下一篇:NPOI操作EXCEL
評(píng)論
共有 條評(píng)論