基本信息
源码名称:EXCEL 上传下载操作
源码大小:5.82KB
文件格式:.rar
开发语言:C#
更新时间:2017-10-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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;
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;
}
}
}