方法如下(原文的代码):
private
void
button1_Click(
object
sender, EventArgs e)
{
saveFileDialog1.Title =
"保存的excel文件"
;
saveFileDialog1.InitialDirectory =
"c:\\"
;
saveFileDialog1.Filter =
"Excel97-2003 (*.xls)|*.xls|All Files (*.*)|*.*"
;
saveFileDialog1.ShowDialog();
if
(saveFileDialog1.FileName ==
""
|| saveFileDialog1.FileName ==
null
)
{
MessageBox.Show(
"文件名不能为空!"
);
return
;
}
string
path = saveFileDialog1.FileName;
string
constr =
"Data Source=.;Initial Catalog=Exhibition;User ID=sa;Password="
;
string
sql = GetStrSql();
DataSet ds=
new
DataSet();
using
(SqlConnection con =
new
SqlConnection(constr))
{
SqlDataAdapter da =
new
SqlDataAdapter(sql, con);
da.Fill(ds);
}
if
(ds ==
null
)
{
MessageBox.Show(
"数据获取有误!"
);
return
;
}
WriteExcel(ds, path);
}
public
void
WriteExcel(DataSet ds,
string
path)
{
try
{
long
totalCount = ds.Tables[0].Rows.Count;
lblTip.Text =
"共有"
+ totalCount +
"条数据。"
;
Thread.Sleep(1000);
long
rowRead = 0;
float
percent = 0;
StreamWriter sw =
new
StreamWriter(path,
false
,Encoding.GetEncoding(
"gb2312"
));
StringBuilder sb =
new
StringBuilder();
for
(
int
k = 0; k < ds.Tables[0].Columns.Count; k++)
{
sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString() +
"\t"
);
}
sb.Append(Environment.NewLine);
for
(
int
i = 0; i < ds.Tables[0].Rows.Count; i++)
{
rowRead++;
percent = ((
float
)(100 * rowRead)) / totalCount;
Pbar.Maximum = (
int
)totalCount;
Pbar.Value = (
int
)rowRead;
lblTip.Text =
"正在写入["
+ percent.ToString(
"0.00"
) +
"%]...的数据"
;
System.Windows.Forms.Application.DoEvents();
for
(
int
j = 0; j < ds.Tables[0].Columns.Count; j++)
{
sb.Append(ds.Tables[0].Rows[i][j].ToString() +
"\t"
);
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
MessageBox.Show(
"已经生成指定Excel文件!"
);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public
string
GetStrSql()
{
string
strSql =
"select d.* from ( select cBarcode ,max(case halldoorid When '36' then '1' else '0' end ) [1.1号馆],max(case halldoorid When '37' then '1' else '0' end ) [2.1号馆],max(case halldoorid When '38' then '1' else '0' end ) [3.1号馆],max(case halldoorid When '39' then '1' else '0' end ) [4.1号馆],max(case halldoorid When '40' then '1' else '0' end ) [5.1号馆],max(case halldoorid When '41' then '1' else '0' end ) [6.1号馆],max(case halldoorid When '42' then '1' else '0' end ) [8.1号馆],max(case halldoorid When '43' then '1' else '0' end ) [1.2号馆],max(case halldoorid When '44' then '1' else '0' end ) [2.2号馆],max(case halldoorid When '45' then '1' else '0' end ) [3.2号馆],max(case halldoorid When '46' then '1' else '0' end ) [4.2号馆],max(case halldoorid When '47' then '1' else '0' end ) [5.2号馆],max(case halldoorid When '48' then '1' else '0' end ) [9.1号馆],max(case halldoorid When '49' then '1' else '0' end ) [10.1号馆],max(case halldoorid When '50' then '1' else '0' end ) [11.1号馆],max(case halldoorid When '51' then '1' else '0' end ) [12.1号馆],max(case halldoorid When '52' then '1' else '0' end ) [13.1号馆],max(case halldoorid When '53' then '1' else '0' end ) [9.2号馆],max(case halldoorid When '54' then '1' else '0' end ) [10.2号馆],max(case halldoorid When '55' then '1' else '0' end ) [11.2号馆],max(case halldoorid When '56' then '1' else '0' end ) [12.2号馆],max(case halldoorid When '57' then '1' else '0' end ) [13.2号馆],max(case halldoorid When '58' then '1' else '0' end ) [9.3号馆],max(case halldoorid When '59' then '1' else '0' end ) [10.3号馆],max(case halldoorid When '60' then '1' else '0' end ) [11.3号馆],max(case halldoorid When '61' then '1' else '0' end ) [VIP3.2号馆],max(case halldoorid When '62' then '1' else '0' end ) [VIP11.3号馆],max(case halldoorid When '63' then '1' else '0' end ) [VIP14.3号馆]from tblReadCard group by cBarcode ) d left join ( select cBarcode 观众条码 from (select distinct cBarcode,cReadDate from tblReadCard where cReadDate between '2011-07-08' and '2011-07-11') a group by cBarcode ) c on d.cBarcode=c.观众条码 group by cBarcode,[1.1号馆],[2.1号馆],[3.1号馆],[4.1号馆],[5.1号馆],[6.1号馆],[8.1号馆],[1.2号馆],[2.2号馆],[3.2号馆],[4.2号馆],[5.2号馆],[9.1号馆],[10.1号馆],[11.1号馆],[12.1号馆],[13.1号馆],[9.2号馆],[10.2号馆],[11.2号馆],[12.2号馆],[13.2号馆],[9.3号馆],[10.3号馆],[11.3号馆],[VIP3.2号馆],[VIP11.3号馆],[VIP14.3号馆]"
;
return
strSql;
}
---------------------------以下是用到自己项目中的代码片段--------------------------------------------
string fileName= "XXXX导出"+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";//此时没有文件 后面会自动创建
string path = Server.MapPath("~/") + @"ExcelTemp\" + fileName;
string strResult = string.Empty;
string strWhere = "XXXXXX";
string orderby = " XXXXX ";
DataSet ds = bll.GetListOnExportExcel( strWhere, orderby);//此处获取DataSet(注意DataTable中的列名字就是表头,所以要提前在sql语句中写好中文列名)
StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
{
sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString().Replace("\n", " ") + "\t");
}
sb.Append(Environment.NewLine);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
sb.Append(ds.Tables[0].Rows[i][j].ToString().Replace("\n", " ") + "\t");
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
//此时文件已经有了,传地址下载即可