//导出excel
public void CreateExcel(DataTable dt, string FileName, string InvType)
{
Response.Clear();
Response.Charset = "UTF-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
string colHeaders = string.Empty;
string ls_item = string.Empty;
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
//写表头
DataTable dtTitle = Datehelper.Question(InvType);
for (int Q = 0; Q < dtTitle.Rows.Count; Q++)
{
string Value = dtTitle.Rows[Q]["QuestionId"].ToString() + "." + dtTitle.Rows[Q]["QuestionTitle"].ToString();
if (Q == dtTitle.Rows.Count - 1)
{
ls_item += Value + "\n";
}
else
{
ls_item += Value + "\t";
}
}
Response.Output.Write(ls_item);
ls_item = string.Empty;
//写数据
foreach (DataRow row in myRow)
{
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
Response.Output.Write(ls_item);
ls_item = string.Empty;
}
Response.Output.Flush();
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}