您的位置:首页 > 编程语言 > ASP

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">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: