Web导出大数据excel最快速方法
StringWriter sw = new StringWriter();
sw.WriteLine("分公司\t营业厅\t成本中心\t收款日期\t单据编号\t用户号码\t交款单位\t帐面日期\t收款金额\t收款项目\t收款项目摘要\t会计科目\t项目段\t收款经办人\t稽核员\t确认人\t稽核\t确认\t交款总金额\t退款总金额\t结余金额");
if(dt != null)
{
foreach(DataRow dr indt.Rows)
{
sw.WriteLine(
dr["d_county_name"].ToString() + "\t" +
dr["d_entity_name"].ToString() + "\t" +
dr["cost_center"].ToString() + "\t" +
Convert.ToDateTime(dr["reg_date"].ToString()).ToShortDateString()+ "\t" +
dr["reg_billcode"].ToString().Replace("\t", "").Replace(" ","") + "\t"+
dr["reg_usercode"].ToString().Replace("\t", "").Replace(" ","") + "\t"+
dr["reg_company"].ToString().Replace("\t", "").Replace(" ","") + "\t"+
dr["book_date"].ToString() + "\t" +
dr["reg_money"].ToString() + "\t" +
dr["reg_item"].ToString() + "\t" +
dr["reg_content"].ToString() + "\t" +
dr["account_course"].ToString() + "\t" +
dr["project_para"].ToString() + "\t" +
dr["d_op_name"].ToString() + "\t" +
dr["d_au_name"].ToString() + "\t" +
dr["d_fi_name"].ToString() + "\t" +
dr["d_audite_complete1"].ToString() + "\t" +
dr["d_finan_complete1"].ToString() + "\t" +
dr["reg_money"].ToString() + "\t" +
dr["refund_money"].ToString() + "\t" +
dr["remain_money"].ToString());
}
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=report.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
WinForm导出大数据excel最快速方法
///<summary>
///导出Excel
///</summary>
///<paramname="dt">需要导的数据</param>
///<paramname="tile">excel中列的标题</param>
publicvoid doExport(DataTable dt, string[] tile)
{
SaveFileDialog saveFileDialog= new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.Title = "Export Excel File";
saveFileDialog.ShowDialog();
if(saveFileDialog.FileName == "")
{
return;
}
StreammyStream;
myStream =saveFileDialog.OpenFile();
StreamWritersw = new StreamWriter(myStream,System.Text.Encoding.GetEncoding(-0));
stringstr = "";
try
{
//写标题
for (int i = 0; i< tile.Length; i++)
{
if (i > 0)
{
str += "\t";
}
str +=tile[i].ToString().Trim();
}
sw.WriteLine(str);
//写内容
for (int j = 0; j< dt.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k< dt.Columns.Count; k++)
{
if (k > 0)
{
tempStr+= "\t";
}
tempStr +=dt.Rows[j][k].ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch(Exception ex)
{
sw.Close();
myStream.Close();
Log.WriteLog(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
///<summary>
///导出Excel
///</summary>
///<paramname="dt">需要导的数据</param>
///<paramname="tile">excel中列的标题</param>
publicvoid doExport(DataTabledt, Dictionary<string,string> tile,stringstrAbsPath)
{
StreammyStream;
myStream = File.Create(strAbsPath);
StreamWritersw = new StreamWriter(myStream,System.Text.Encoding.GetEncoding("GB2312"));
stringstr = "";
try
{
//写标题
foreach(string strKey intile.Keys)
{
if (string.IsNullOrEmpty(str))
{
str =tile[strKey].Replace("\t", "").Replace(" ", "");
}
else
{
str += "\t"+ tile[strKey].Replace("\t", "").Replace(" ","");
}
}
sw.WriteLine(str);
//写内容
for(int j = 0; j < dt.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k< dt.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr +=dt.Rows[j][k].ToString().Replace("\t","").Replace(" ", "");
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch(Exception ex)
{
sw.Close();
myStream.Close();
throwex;
}
finally
{
sw.Close();
myStream.Close();
}
}