资源简介
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 的摘要说明
/// </summary>
public class ExportToExcel
{
public ExportToExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private void KillExcelProcess()
{
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">数据源</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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">数据源</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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
public bool DataTableToExcel(System.Data.DataTable dt)
{
try
{
string FileName = HttpContext.Current.Server.MapPath("../Excel/") "催收案件记录报表_" 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\" "催收案件记录报表" ".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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
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 的摘要说明
/// </summary>
public class ExportToExcel
{
public ExportToExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private void KillExcelProcess()
{
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">数据源</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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">数据源</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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
}
catch
{
// KillExcelProcess();
}
}
public bool DataTableToExcel(System.Data.DataTable dt)
{
try
{
string FileName = HttpContext.Current.Server.MapPath("../Excel/") "催收案件记录报表_" 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\" "催收案件记录报表" ".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;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
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.
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 17487 2015-06-29 13:00 CreateExcelFile.cs
文件 6034 2017-10-16 13:54 ExportToExcel.cs
----------- --------- ---------- ----- ----
23521 2
- 上一篇:C# 文件加密工具源码
- 下一篇:NPOI操作EXCEL
相关资源
- C#联通网络宽带测试 拨号
- C#百度指数抓取方法(2012年版本已失
- C# 隐藏某个磁盘分区
- wince引脚控制程序
- C# 读取并编辑window系统的右键菜单
- C#自定义屏保(不断滚动的文字)
- C#winform打印指定区域 -控件拖动 -设置
- C#使用Hook进行改键
- 提供C#调用系统API函数弹出或收起光驱
- 通过C#自带的头文件(类)获取Windo
- C#获取电脑CPU以及内存使用率
- Syndication实现读取、创建、订阅、更新
- 利用uu云打码平台的lib实现的c#打码平
- tf-idf一种计算方法
- C# pop3 邮件接收程序
- C# 邮件群发示例 源码下载18952
- httpclient source code by csharp
- 动态抓取IPC#实现
- NServiceKit.Redis 基础操作
- XXTEA算法的C#实现和JS实现,可以互相
- C# 飞行棋 游戏源码(面向对象入门)
- Socke传输 (wince6.0系统)
- UDP Messenger 1.0.unitypackage
- asp.net 网页静态化组件(shipingx-Stati
- SocketAsyncEventArgs完成断开编程
- 基于WinPcap的C# ARP欺骗软件().rar
- C#网络应用编程 矩阵并行计算练习
- 猜数小游戏WCF网络编程技术(附服务
- 《C#版Ftp软件源码》
- 基于com串口的文件发送和接收
评论
共有 条评论