我在grid
页面中关注了.aspx
。
我需要做什么:
当用户点击它时,应从特定行获取Company Name
Product Name
No. of records
并运行以下过程并下载excel文件。
以下是我的代码:
存储过程:
CREATE proc [dbo].[proc_getHistoryProduct]
(
@CompName nvarchar(max) = '',
@ProdName nvarchar(max) = '',
@noOfRecords int
)
as
begin
if @ProdName = '' and @CompName = 'NULL'
begin
select TOP (@noOfRecords) * from ProductDetail where (Product_Description like '%' + @ProdName + '%' )
end
else if @CompName = '' and @ProdName = 'NULL'
begin
select TOP (@noOfRecords) * from ProductDetail where (Shipper_Name like '%' + @CompName + '%' )
end
end
要在Excel中下载的代码:
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridData.SelectedRow;
int noOfRecords = Convert.ToInt32(row.Cells[3].Text);
string CompName = row.Cells[2].Text;
string ProdName = row.Cells[1].Text;
DataTable dt = (DataTable)Session["UserHistoryProduct"];
con.Open();
SqlCommand cmd = new SqlCommand("proc_getHistoryProduct", con);
cmd.Parameters.Add("@CompName", SqlDbType.VarChar, 1000).Value = CompName;
cmd.Parameters.Add("@ProdName", SqlDbType.VarChar, 1000).Value = ProdName;
cmd.Parameters.Add("@noOfRecords", SqlDbType.Int).Value = noOfRecords;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
dr.Close();
con.Close();
Session["UserHistoryProduct"] = dt;
DataTable Searchtable = (DataTable)Session["UserHistoryProduct"];
ExportToExcel(dt);
}
有谁能告诉我哪里出错了?
编辑:
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "HistoryProductDetail.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView dgGrid = new GridView();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
答案 0 :(得分:2)
尝试使用SqlDataAdapter
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
而不是SqlDataReader
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
dr.Close();
我不确定,但可能有所帮助
答案 1 :(得分:0)
终于解决了我的问题!
我需要做的是甚至在OnClick="DownloadExcel_Click"
即<asp:LinkButton>
Download
public void DownloadExcel_Click(object sender, EventArgs e)
{
var downloadLink = (Control)sender;
GridViewRow row = (GridViewRow)downloadLink.NamingContainer;
Label lblProduct = (Label)row.FindControl("lblprodname");
string prodName = lblProduct.Text;
Label lblCompany = (Label)row.FindControl("lblcompname");
string compName = lblCompany.Text;
Label lblRecords = (Label)row.FindControl("lblrecords");
int noOfRecords = Convert.ToInt32(lblRecords.Text);
DataTable dt = (DataTable)Session["UserHistoryProduct"];
con.Open();
SqlCommand cmd = new SqlCommand("proc_getProductHistory", con);
cmd.Parameters.Add("@ProdName", SqlDbType.VarChar, 1000).Value = prodName;
cmd.Parameters.Add("@CompName", SqlDbType.VarChar, 1000).Value = compName;
cmd.Parameters.Add("@noOfRecords", SqlDbType.Int).Value = noOfRecords;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
dt = new DataTable();
da.Fill(dt);
}
//dt.Load(dr);
//dr.Close();
con.Close();
Session["UserHistoryProduct"] = dt;
//DataTable Searchtable = (DataTable)Session["UserHistoryProduct"];
ExportToExcel(dt);
}