今天做那个倒数excel的操作,我界面上有两个按钮button1和button2我想单击button显示数据,单击button2导出数据,但是我的代码是分开的,所以我有了这种写法
protected void Button1_Click(object sender, EventArgs e)
{
Button2_Click();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
这种写法一直有问题,不知道为什么,谁能说明一下
源代码是下面的
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Admin_daochushuju : System.Web.UI.Page
{
int control = 0;//控制导出数据
protected void Page_Load(object sender, EventArgs e)
{
}
public void LoadDataToExcel(DataTable dt)
{
//导出excle开始。
if (dt.Rows.Count != 0)
{
int rows = dt.Rows.Count;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("导出文件.xls"));
System.IO.StreamWriter objWriter = new System.IO.StreamWriter(Response.OutputStream, System.Text.Encoding.UTF8);
objWriter.WriteLine("<?xml version=\"1.0\"?>");
objWriter.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
objWriter.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
objWriter.WriteLine("<Worksheet ss:Name=\"Sheet1\">");
objWriter.WriteLine("<Table>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"33\"/>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"130\"/>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"130\"/>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"130\"/>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"130\"/>");
objWriter.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"130\"/>");
objWriter.WriteLine("<Row>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">编号</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">用户名</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">书名</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">数量</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">热销价</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">订单编号</Data></Cell>");
objWriter.WriteLine("</Row>");
for (int i = 0; i < rows; i++) //注:一直用WriteLine进行输出,因为CSV文件是以“,”为分割的简单文件格式。而EXCEL是记录行的
{
objWriter.WriteLine("<Row>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + (i + 1).ToString() + "</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + dt.Rows[i]["Sgq_uName"].ToString() + " " + "</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + dt.Rows[i]["Sgq_bName"].ToString() + " " + "</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + dt.Rows[i]["Sgq_cNumber"].ToString() + "</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + dt.Rows[i]["Sgq_bHotprice"].ToString() + "</Data></Cell>");
objWriter.WriteLine("<Cell><Data ss:Type=\"String\">" + dt.Rows[i]["Sgq_oNumber"].ToString() + "</Data></Cell>");
objWriter.WriteLine("</Row>");
}
objWriter.WriteLine("</Table>");
objWriter.WriteLine("</Worksheet>");
objWriter.WriteLine("</Workbook>");
objWriter.Close();
Response.End();
}
//导出excle结束。
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sgq_DBConnectionString"].ConnectionString.ToString()))
{
conn.Open();
string a = TextBox1.Text;
string b = TextBox2.Text;
string sqlstr = "select Sgq_uName,Sgq_bName,Sgq_cNumber,Sgq_bHotprice,Sgq_oNumber from Sgq_Car_Order where Sgq_oConfirmTime>='" + a + "' and Sgq_oConfirmTime<='" + b + "' ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();//DataSet相当于一个虚拟的数据库
da.Fill(ds);
DataTable dt = ds.Tables[0];//调用数据库集的一个表
//判断导出数据
if (control == 1)
{
LoadDataToExcel(dt);
control = 0;
return;
}
else
{
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
Response.Write(dt.Rows[i][j].ToString() + "\n");
}
Response.Write("</br>");
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
control = 1;
Button1_Click();
}
} |