日期:2014-05-17 浏览次数:20419 次
using office = Microsoft.Office;
using Exl = Microsoft.Office.Interop.Excel;
#region ExportExecl
//票数详情统计
protected void btnDarenVotes_Click(object sender, EventArgs e)
{
string sql = "select ID as 编号,Name as 名字,IDNum as 身份证号 from DarenInfo order by Votes desc";
ExportExecl(sql, "VoteInfo", btnDarenVotes.ToolTip);
}
/// <summary>
///
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="tableName">表对象</param>
/// <param name="msg"></param>
protected void ExportExecl(string sql, string tableName,string msg)
{
//1.创建一个execl表格,命名
Random rm = new Random();
int rmNum = rm.Next(10000, 999999);
string saveName = string.Format("{0}-{1}-{2}.xls", tableName, DateTime.Now.ToString("yyyyMMddhhmmssms"), rmNum);
string savePath = Server.MapPath("xls/") + saveName;
//2.配合Sql查询语句,查出数据并且导入到对应的表格路径
int tempPoint = sql.IndexOf(" from");
string tempSql = string.Format(sql.Insert(tempPoint, " into [Excel 8.0;database={0}].[{1}]"), savePath, tableName);
DBHelperDaren.ExecuteCommand(tempSql);
string tempUrl = string.Format("http://{0}/admin/Daren/xls/{1}", Request.Url.Authority, saveName);
linkToExport.InnerText =string.Format("导出成功,单击鼠标右键“另存为”保存至本地");
linkToExport.HRef = tempUrl;
&nb