日期:2014-05-18  浏览次数:20456 次

在 c# 里面 导数据到 EXCEl 速度慢是怎么回事 ?
语句如下总共110列 读到100行时要1分多钟 200行时要 3分多钟 1千行 就不动了是怎么回事 哪位高手看看是怎么回事 有没有什么好的方法可以当 让导数据到excel时速度会快一点

for(int i=0;i<dvQuery.Count;i++) {
int InsertIndex = 3;
for(int j=0;j<dvQuery.Table.Columns.Count;j++)
{

sqlExcelColumn = "UPDATE [{0}${1}{2}:{3}{2}] SET ";
sqlExcelColumn = string.Format(sqlExcelColumn,"Sheet1", GetExcelColumnName(InsertIndex), Convert.ToString(19+i),GetExcelColumnName(InsertIndex));
sqlExcelColumn += "F1='"+dvQuery[i][dvQuery.Table.Columns[j].Caption].ToString().Trim()+"'";
objCmd.CommandText = sqlExcelColumn;
objCmd.ExecuteNonQuery();
InsertIndex =InsertIndex+1;


}
}

------解决方案--------------------
public static void ExportExcel(string[] Title, string[] fileds, DataSet ds)
{

string savetopath = "C:\\export\\FileName.xls";
FileStream fs = new FileStream(savetopath, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(("gb2312")));
//写入表头
StringBuilder strTitle = new StringBuilder();
for (int i = 0; i < Title.Length; i++)
{
strTitle.Append("" + Title[i].ToString() + "");
strTitle.Append("\t");
}
sw.WriteLine(strTitle);

//导入数据
int s = ds.Tables[0].Rows.Count;

foreach (DataRow dr in ds.Tables[0].Rows)
{
StringBuilder strData = new StringBuilder();
for (int i = 0; i < fileds.Length; i++)
{
strData.Append("" + dr[fileds[i]] + "");
strData.Append("\t");
}
sw.WriteLine(strData);
}
sw.Close();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 指定返回的是一个不能被客户端读取的流,必须被下载 
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端 
HttpContext.Current.Response.WriteFile(savetopath);
HttpContext.Current.Response.End();
}
------解决方案--------------------
public static void CreateExcel(DataTable ds, string FileName, string colHeaders, string[] colName)
{
HttpResponse resp;
//resp = Page.Response;
resp = HttpContext.Current.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string ls_item = "";
int i = 0;
//定义表对象与行对像,同时用DataSet对其值进行初始化 
DataTable dt = ds;
DataRow[] myRow = dt.Select("");

//向HTTP输出流中写入取得的数据信息 
resp.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n 
for (i = 0; i < colName.Length - 1; i++)
ls_item += row[colName[i]].ToString() + "\t";
ls_item += row[colName[i]].ToString() + "\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item = "";
}
//写缓冲区中的数据到HTTP头文件中 
resp.End();
}
------解决方案--------------------
学习!