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;
}
}
C# 从服务器上下载文件
最新推荐文章于 2025-06-12 13:27:37 发布