asp.net 导出数据到Excel
2010-11-28 23:14
302 查看
先导出到服务器,然后再下载。
protected void btnExcel_Click(object sender, EventArgs e)
{
System.IO.FileInfo file2 = new System.IO.FileInfo(urlconvertorlocal("/CheYou/Excel/导出.xls"));
file2.Delete();
string myStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/KePai.mdb";
//每个Sheet只能最多保存65536条记录。
string sql = @"select top 65535 Documents as 发货单号,AddTime as 发货日期,CName as 客户简称,BuMen as 销售部门,UName as 业务员,PName as 货物名称,PStandard as 规格型号,PCount as 数量,TaxPrice as 含税单价,SumPrice as 价税合计 into [Excel 8.0;database=" + Server.MapPath(".") + @"/Excel/导出.xls].[Sheet1] from T_TDay";
using (OleDbConnection conn = new OleDbConnection(myStr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException ex)
{
throw new Exception(ex.Message);
}
}
}
tryul.Controls.Add(new LiteralControl("<a href='Excel/ASPNET20Book.xls'>点击下载Excel</a>"));
}
/// <summary>
/// 转换为绝对路径
/// </summary>
/// <param name="imagesurl1"></param>
/// <returns></returns>
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"/"); //转换成绝对路径
return imagesurl2;
}
aspx:
<ul id="tryul" runat="server">
protected void btnExcel_Click(object sender, EventArgs e)
{
System.IO.FileInfo file2 = new System.IO.FileInfo(urlconvertorlocal("/CheYou/Excel/导出.xls"));
file2.Delete();
string myStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/KePai.mdb";
//每个Sheet只能最多保存65536条记录。
string sql = @"select top 65535 Documents as 发货单号,AddTime as 发货日期,CName as 客户简称,BuMen as 销售部门,UName as 业务员,PName as 货物名称,PStandard as 规格型号,PCount as 数量,TaxPrice as 含税单价,SumPrice as 价税合计 into [Excel 8.0;database=" + Server.MapPath(".") + @"/Excel/导出.xls].[Sheet1] from T_TDay";
using (OleDbConnection conn = new OleDbConnection(myStr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException ex)
{
throw new Exception(ex.Message);
}
}
}
tryul.Controls.Add(new LiteralControl("<a href='Excel/ASPNET20Book.xls'>点击下载Excel</a>"));
}
/// <summary>
/// 转换为绝对路径
/// </summary>
/// <param name="imagesurl1"></param>
/// <returns></returns>
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"/"); //转换成绝对路径
return imagesurl2;
}
aspx:
<ul id="tryul" runat="server">
相关文章推荐
- ASP.net 导出Excel数据攻略
- ASP.NET 将 DataView 数据导出到服务器上生成Excel文件
- Asp.Net中如何将数据导出到excel
- [整理]asp.net导出Excel/Csv格式数据方案
- asp.net实现Gradview绑定数据库数据并导出Excel的方法
- asp.net中将gridview数据导出成Excel
- 【译】Asp.Net 导出 Excel 数据的9种方案
- asp.net导出EXCEL的好方法!(好用,导出全部数据)
- asp.net导出数据到Excel的三种方法
- asp.net DataSet数据导出到Excel中
- asp.net导出数据到Excel的三种方法
- Asp.Net 导出数据格式为Excel 数据的9种方案
- 【导出EXCEL汇总】asp.net 将数据简单导出到excel中
- asp.net导出数据到Excel的三种方法
- C#数据库数据导入导出系列之一 ASP.NET Excel导入Sql Server数据库 .
- .Net中导出数据到Excel(asp.net和winform程序中)
- ASP.NET Excel 2010数据导入与导出
- Asp.net导出Excel/Csv文本格式数据
- ASP.NET WebApi 服务端导出数据到Excel
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;