c# 将Datatable数据导出到Excel表格中
话不多说,请看代码:
publicFileResultGetExcelFile()
{
if(Session["beginDate"]!=null)
{
stringbdate=Session["beginDate"].ToString();
DateTimeld=Convert.ToDateTime(Session["lastDate"].ToString());
DateTimeldate=ld.AddDays(1);
stringlldate=ldate.ToString("yyyy-MM-dd");
intptkey=Convert.ToInt32(Session["Process_PTR"]);
stringproceSql="select*fromAssay_ProcesswhereRKEY="+ptkey;
DataTableproceDt=SqlHelper.QueryTable(proceSql);
IDataParameter[]iDataTitle=newSqlParameter[3];
iDataTitle[0]=newSqlParameter("@BeginDate",bdate);
iDataTitle[1]=newSqlParameter("@LastDate",lldate);
iDataTitle[2]=newSqlParameter("@RKEY",ptkey);
DataSetdtTitle=SqlHelper.RunProceduresByParameter("pro_GetAssalyInfoByExportPrint",iDataTitle);
DataTabledt=dtTitle.Tables[0];
stringoutStr=ExcelExport.outExcel(dt);
byte[]fileContents=Encoding.UTF8.GetBytes(outStr.ToString());
returnFile(fileContents,"application/ms-excel",""+proceDt.Rows[0]["ProcessName"].ToString().Trim()+"化验单据("+bdate.Trim()+"-"+lldate.Trim()+").xls");
}
else
{
stringqua_no=Session["quano"].ToString();
IDataParameter[]iDataTitle=newSqlParameter[1];
iDataTitle[0]=newSqlParameter("@Qua_no",qua_no);
DataSetdtTitle=SqlHelper.RunProceduresByParameter("pro_GetAssalyInfoByQua_No",iDataTitle);
DataTabledt=dtTitle.Tables[0];
stringoutStr=ExcelExport.outExcel(dt);
byte[]fileContents=Encoding.UTF8.GetBytes(outStr.ToString());
returnFile(fileContents,"application/ms-excel","化验单据("+qua_no.Trim()+").xls");
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!