C# 从服务器上下载文件

本文介绍了一个基于 ASP.NET 的网页应用,该应用实现了数据查询功能并支持将查询结果导出为 Excel 文件。文中详细展示了如何使用 C# 语言进行数据库操作、条件筛选以及如何利用 Aspose.Cells 库来生成 Excel 文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = 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 < ll_count; i++)
            {
                ls_column = gv.Columns[i].HeaderText;
                examtable.Columns.Add(ls_column, typeof(string));
            }

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


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值