日期:2014-05-17  浏览次数:21025 次

C# 从服务器上下载文件

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using Aspose.Cells;
using DBUtility;
using System.IO;
using System.Data.OleDb;
using Model;
using System.Collections.Generic;
using SQLServerDAL;
using System.Text;


public partial class query_query : System.Web.UI.Page
{

??? public override void VerifyRenderingInServerForm(Control control)
??? {
??????? // Confirms that an HtmlForm control is rendered for
??? }

??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? if (!IsPostBack)
??????? {
??????????? this.GridView1.DataSource = PropertyBLL.GetPropertyInfo();
??????????? this.GridView1.DataBind();
??????? }
??? }

??? protected void bt_query_Click(object sender, EventArgs e)
??? {
??????? string column= this.DropDownList1.SelectedValue;
??????? string condition= this.DropDownList2.SelectedValue;
??????? string txtValue = this.TextBox1.Text.ToString().Trim();
??????? string sqlwhere = "";
??????? if (condition == "")
??????? {
??????????? this.GridView1.DataSource = PropertyBLL.GetPropertyInfo();
??????????? this.GridView1.DataBind();
??????? }

??????? switch (condition)
??????? {
??????????? case "in":
??????????????? sqlwhere = " where " + column + " in ('" + txtValue + "')";
??????????????? break;
??????????? case "like":
??????????????? sqlwhere = " where " + column + " like '%" + txtValue + "%'";
??????????????? break;
??????????? default:
??????????????? sqlwhere = " where " + column + condition + " '" + txtValue + "'";
??????????????? break;
??????? }

??????? string Sql;
??????? string sqlquery = "select * from BorrowApplicationInfo ";
??????? if (sqlwhere.ToString().Trim().Length > 0)
??????? {
??????????? Sql = sqlquery + sqlwhere;
??????? }
??????? else
??????? {
??????????? Sql = sqlquery;
??????? }

??????? this.GridView1.DataSource = PropertyBLL.GetPropertyBySql(Sql);
??????? this.GridView1.DataBind();
??????? this.HiddenField1.Value = Sql;
??? }

??? protected void bt_export_Click(object sender, EventArgs e)
??? {
??????? CreateExcelFile();
??? }

??? public void CreateExcelFile()
??? { //在服务器生成EXCEL文件

??????? //ls_search_sql = Request.
??????? string ls_search_sql;
??????? ls_search_sql = Request.Form["HiddenField1"].ToString().Trim();

??????? if (ls_search_sql.Length > 0)
??????? {
??????????? this.GridView1.DataSource = PropertyBLL.GetPropertyBySql(ls_search_sql);
??????????? this.GridView1.DataBind();
??????? }
??????? else
??????? {
??????????? this.GridView1.DataSource = PropertyBLL.GetPropertyInfo();
??????????? this.GridView1.DataBind();
??????? }

??????? string ls_filename = "AssetInfo";
??????? string ls_filepath = "";
??????? //创建一个表,并从数据窗口中转成DataTable;
??????? DataTable dt = new DataTable();
??????? dt = GridView2DataTable(this.GridView1);
??????? ls_filepath = ImportExportExcel.DatatableToExcel(dt, "设备信息", "Sheet1", ls_filename);
??????? //从服务器导出EXCEL文件,相当于在客户机打开另存窗口;
??????? SaveFile(ls_filename+".xls", ls_filepath);
??? }

??? public void SaveFile(string ls_filename, string ls_filepathe)
??? { //导出服务器的指定文件;
??????? if (!File.Exists(ls_filepathe))
??????? {?? //文件不存在则直接退出不进行导出
??????????? Response.Write("<script>alert('文件不存在,无法下载!')</script>");
??????????? return;
??????? }
??????? System.IO.FileInfo fileInfo =