---
title: "C# 从服务器上下载文件"
author: "cab0605"
date: "2011-04-12T21:50:47Z"
tags: []
categories: ["Web前端"]
slug: "c"
draft: false
lastmod: "2011-04-12T21:50:47Z"
id: 1000397
url: "https://cab0605.iteye.com/blog/1000397"
---

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 &gt; 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 &gt; 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("&lt;script&gt;alert('文件不存在，无法下载!')&lt;/script&gt;");  
            return;  
        }  
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(ls\_filepathe);  
        Response.Clear();  
        Response.ClearContent();  
        Response.ClearHeaders();  
        Response.AddHeader("Content-Disposition", "attachment;filename=" + ls\_filename);  
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
        Response.AddHeader("Content-Transfer-Encoding", "binary");  
        Response.ContentType = "application/ms-excel";  
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
        Response.WriteFile(fileInfo.FullName);  
        Response.Flush();  
        Response.End();  
    }

    public DataTable GridView2DataTable( GridView gv)  
        {  
            DataSet ds = new DataSet();  
            DataTable examtable = new DataTable();  
            long ll\_count = 0;

            string ls\_column = "";  
            ll\_count = gv.Columns.Count;

            for (int i = 0; i &lt; ll\_count; i++)  
            {  
                ls\_column = gv.Columns\[i].HeaderText;  
                examtable.Columns.Add(ls\_column, typeof(string));  
            }

            for (int i = 0; i &lt; gv.Rows.Count; i++)  
            {  
                DataRow dr = examtable.NewRow();  
                for (int j = 0; j &lt; ll\_count; j++)  
                {  
                    dr\[j] = gv.Rows\[i].Cells\[j].Text.Trim().ToString();  
                }  
                examtable.Rows.Add(dr);  
            }  
            return examtable;  
        }

}