ASP.net implements methods for importing data from a DataTable into an Excel file and creating tables-practical tips

Source: Internet
Author: User

This article describes how asp.net implements data to import from a DataTable into an Excel file and create a table. Share to everyone for your reference, specific as follows:

<summary>///Import data from DataTable into Excel file///</summary>///<param name= "dataTable" > Data source </param&
Gt <param name= "Absoluteexcelfilepath" >excel file absolute path </param>///<param name= "Tblcolname" > The corresponding column name </param> ColumnName <returns> operation in the TBL column name </param>///<param name= ">excel"///returns true successfully. Failure returns 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) {//build table 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) {//Import data foreach (DataRow row in datatable.rows) {strSQL = "ins
      ert 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;

 }

Invoke method:

 DataSet ds = (DataSet) session["Listmobile"];//gets the value of the table to be exported if (DS). Tables[0]. Rows.Count <= 0) {page.registerstartupscript ("", "<mce:script type=" Text/javascript "><!--alert (' No content cannot be exported!
')//--></mce:script> ");
 else {//excel page name 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 (); }
}

I hope this article will help you with ASP.net programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.