日期:2014-05-17 浏览次数:20405 次
/// <summary>
/// 将dt数据导出到Excel文件
/// </summary>
/// <param name="dt">数据集</param>
/// <param name="filename">文件名</param>
/// <param name="tabName">表名</param>
/// <param name="reMsg">返回消息</param>
/// <returns>bool</returns>
public bool DataTableExportToExcel(DataTable dt, string filename, String tabName, ref String reMsg)
{
#region 引用Interop.ADOX.dll
if (dt.Rows.Count <= 0)
{
reMsg = "目前无数据不需要导出";
return false;
}
int rows = dt.Rows.Count;
int cols = dt.Columns.Count;
StringBuilder sb = new StringBuilder();
string connString = String.Empty;
connString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", filename);
//创建表
sb.Append("CREATE TABLE " + tabName + " (");
String colName = String.Empty;
String colNames = String.Empty;
String colNamePramas = String.Empty;
String colType = String.Empty;
for (int i = 0; i < cols; i++)
{
colName = dt.Columns[i].ColumnName.ToString();
colType = dt.Columns[i].DataType.ToString();
&nb