DataTable到Excel导出
问题描述:
我在开发asp.net 3.5中的web项目DataTable到Excel导出
我想导出数据表到Excel。但是在数据表中有20.000行。有时超时问题发生..
protected string Worksheet97_Header()
{
string s = "<tr>";
foreach (ExcelColumn col in Columns)
{
s += "<th>" + col.Header_Text + "</th>";
}
s+="</tr>";
return s;
}
protected string Worksheet97_Data()
{
string s = "";
try
{
for (int i = 0; i < data.Rows.Count; i++)
{
s += "<tr>";
foreach (ExcelColumn col in Columns)
{
if (col.Column_Type == "System.String")
s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
if (col.Column_Type == "System.DateTime")
s += "<td>" + Convert.ToDateTime(data.Rows[i][col.Field_Name]).ToString("dd.MM.yyyy HH:mm:ss") + "</td>";
if (col.Column_Type == "System.Int32")
s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
if ((col.Column_Type == "System.Double") |
(col.Column_Type == "System.Decimal") |
(col.Column_Type == "System.Int16") |
(col.Column_Type == "System.Int32") |
(col.Column_Type == "System.Int64"))
s += "<td>" + Convert.ToDouble(data.Rows[i][col.Field_Name]).ToString("0.00") + "</td>";
}
}
}
catch (Exception ex)
{
string a = ex.ToString();
}
return s;
}
public string Export_Excel97()
{
string s = "";
s = "<table border=\"1\">";
s += Worksheet97_Header();
s += Worksheet97_Data();
s += "</table>";
return s;
}
谢谢。
答
恕我直言,我认为你应该页面查询,以便你不加载到内存中的一切。
要编写excel文件,您也可以尝试此解决方案,并比较性能结果:http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx这将使用Microsoft Excel对象库,因此您需要在运行您的计算机上安装Excel码。
HTH以某种方式。
问候!
请发布的详细信息(最好的代码是时序出)我们可能会帮助你。 – Codesleuth 2010-11-05 09:22:28
我已发布.. – Jack 2010-11-05 09:37:53