有人会说,用gridview打印excel不是很方便吗?但是我发现数据量一大gridview就不行,速度慢不说,还会出错
首先件一个文件夹,放excel档
然后就可以弄一个按钮,按钮的代码如下
protected void btcx_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection constring = new SqlConnection("Server=192.168.42.94;uid=sa;pwd=omanlaolao;database=xianbiancang");
constring.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT * FROM gzkmk", constring);
da.Fill(ds, "tablename");
constring.Close();
string strFile = "";
string path = "";
System.Data.DataTable dt = ds.Tables[0];
//文件信息设置
strFile = strFile + "LogBackUp";
strFile = strFile + DateTime.Now.ToString("yyyyMMddhhmmss");
strFile = strFile + ".xls";
path = Server.MapPath("excel//" + strFile);
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
//画表头
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("/t");
}
sw.WriteLine("");
//画表体
for (int i = 0; i < dt.Rows.Count; i++)
{
sw.Write(dt.Rows[i]["flrq"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["gzkh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["glh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["xldh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["cks"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["lybz"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["bz"].ToString());
sw.Write("/t");
sw.WriteLine("");
}
sw.Flush();
sw.Close();
string url = "http://192.168.42.94/testaspexcel3/excel/" + strFile;
Response.Redirect(url);这句话会打开一个对话框,你可以点打开,也可以点保存,
}
最后要说明的是你还需要设置文件加的访问权限,然后再web.config中用你在文件夹中加的用户名,密码
首先件一个文件夹,放excel档
然后就可以弄一个按钮,按钮的代码如下
protected void btcx_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection constring = new SqlConnection("Server=192.168.42.94;uid=sa;pwd=omanlaolao;database=xianbiancang");
constring.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT * FROM gzkmk", constring);
da.Fill(ds, "tablename");
constring.Close();
string strFile = "";
string path = "";
System.Data.DataTable dt = ds.Tables[0];
//文件信息设置
strFile = strFile + "LogBackUp";
strFile = strFile + DateTime.Now.ToString("yyyyMMddhhmmss");
strFile = strFile + ".xls";
path = Server.MapPath("excel//" + strFile);
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
//画表头
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("/t");
}
sw.WriteLine("");
//画表体
for (int i = 0; i < dt.Rows.Count; i++)
{
sw.Write(dt.Rows[i]["flrq"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["gzkh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["glh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["xldh"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["cks"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["lybz"].ToString());
sw.Write("/t");
sw.Write(dt.Rows[i]["bz"].ToString());
sw.Write("/t");
sw.WriteLine("");
}
sw.Flush();
sw.Close();
string url = "http://192.168.42.94/testaspexcel3/excel/" + strFile;
Response.Redirect(url);这句话会打开一个对话框,你可以点打开,也可以点保存,
}
最后要说明的是你还需要设置文件加的访问权限,然后再web.config中用你在文件夹中加的用户名,密码