本文实例讲述了asp.net实现数据从DataTable导入到Excel文件并创建表的方法。分享给大家供大家参考,具体如下:

/// <summary>
/// 把数据从DataTable导入到Excel文件里
/// </summary>
/// <param name="dataTable">数据源</param>
/// <param name="AbsoluteExcelFilePath">Excel文件的绝对路径</param>
/// <param name="TblColName">TBL里对应的列名</param>
/// <param name="ColumnName">Excel中对应的列名</param>
/// <returns>操作成功返回True,失败返回False</returns>
public static bool ExportDataToExcel(DataTable dataTable, string AbsoluteExcelFilePath, string[] TblColName, string[] ColumnName)
{
  int k = 0;
  if (dataTable == null) return false;
  OleDbConnection Conn = new OleDbConnection();
  try
  {
   string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AbsoluteExcelFilePath + ";Mode=Share Deny None;Extended Properties=Excel 8.0;Jet OLEDB:Create System Database=True";
   Conn = new OleDbConnection(strConn);
   Conn.Open();
   OleDbCommand command = Conn.CreateCommand();
   string strSQL = "";
   if (dataTable.Columns != null)
   {
    //建表
    strSQL = "CREATE TABLE " + dataTable.TableName + "(";
    for (int i = 0; i < ColumnName.Length; i++)
    {
     strSQL += ColumnName[i] + " TEXT,";
    }
    strSQL = strSQL.Substring(0, strSQL.Length - 1);
    strSQL += ")";
    command.CommandText += strSQL;
    command.ExecuteNonQuery();
    if (dataTable.Rows.Count > 0)
    {
     //导入数据
     foreach (DataRow row in dataTable.Rows)
     {
      strSQL = "insert into " + dataTable.TableName + "(";
      for (k = 0; k < TblColName.Length; k++)
      {
       strSQL += ColumnName[k] + ",";
      }
      strSQL = strSQL.Substring(0, strSQL.Length - 1);
      strSQL += ") values( ";
      for (k = 0; k < TblColName.Length; k++)
      {
       strSQL += "'" + row[TblColName[k]] + "',";
      }
      strSQL = strSQL.Substring(0, strSQL.Length - 1);
      strSQL += ")";
      command.CommandText = strSQL;
      command.ExecuteNonQuery();
     }
    }
   }
  }
  catch (Exception ex)
  {
   Conn.Close();
   throw new Exception(ex.Message);
   return false;
  }
  Conn.Close();
  return true;
}

调用方法:

DataSet ds = (DataSet)Session["listMobile"];//获得要导出的表格的值
if (ds.Tables[0].Rows.Count <= 0)
{
 Page.RegisterStartupScript("", "<mce:script type="text/javascript"><!--
alert('没有内容不能导出!')
// --></mce:script>");
}
else
{
 //EXCEL页面的名称
 string[] tableName = { "["+DateTime.Now.ToString("yyyyMMddhhmmss")+"]" };
 string fileName = tools.CreateID() + ".xls";
 string filePath = Server.MapPath("..//DownloadFiles//" + fileName);
 if (tools.ExportDataToExcel(ds, filePath, tableName)==true)
 {
  Response.Clear();
  Response.Buffer = true;
  Response.Charset = "GB2312";
  Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
  Response.ContentType = "application/vnd.ms-excel";
  this.EnableViewState = false;
  Response.WriteFile(filePath);
  Response.Flush();
  if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath);
  Response.Redirect(this.Request.UrlReferrer.AbsoluteUri, true);
  Response.End();
 }
}

希望本文所述对大家asp.net程序设计有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。