C # Operations Excel (Import Export)

Source: Internet
Author: User
Excel

Some days ago, a lot of friends said that need C # Export to Excel code, now share to everyone

///<summary>
  ///Read the Excel document
  ///</summary>
  ///< param name= "Path" > file name </param>
  ///<returns> Returns a dataset </returns>
   public DataSet exceltods (string Path)
  {
   string strconn = "provider= Microsoft.Jet.OLEDB.4.0 "+" "Data source=" + Path + ";" + "Extended Properties=excel 8.0;";
   oledbconnection conn = new OleDbConnection (strconn);
   conn. Open (); 
   string strexcel = ";  
   oledbdataadapter mycommand = null;
   dataset ds = null;
   strexcel= "SELECT * from [sheet1$]";
   mycommand = new OleDbDataAdapter (Strexcel, strconn);
   ds = new DataSet ();
   mycommand.fill (ds, "table1");  
   return ds;
  }

<summary>
Writing to Excel documents
</summary>
<param name= "Path" > File name </param>
public bool Savefp2toexcel (string Path)
{
Try
{
String strconn = "Provider=Microsoft.Jet.OLEDB.4.0 +" + "Data source=" + Path + ";" + "Extended Properties=excel 8.0;";
OleDbConnection conn = new OleDbConnection (strconn);
Conn. Open ();
System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
Cmd. Connection =conn;
Cmd.commandtext = "Update [sheet1$] SET name = ' 2005-01-01 ' WHERE work number = ' Date '";
Cmd. ExecuteNonQuery ();
for (int i=0;i<fp2. Sheets [0]. rowcount-1;i++)
{
if (FP2. Sheets [0]. cells[i,0]. Text!= "")
{
Cmd.commandtext = "INSERT into [sheet1$] (work number, name, department, title, date, time) VALUES ('" +FP2). Sheets [0]. cells[i,0]. text+ "', '" +
FP2. Sheets [0]. cells[i,1]. text+ "', '" +FP2. Sheets [0]. cells[i,2]. text+ "', '" +FP2. Sheets [0]. cells[i,3]. text+
"', '" +FP2. Sheets [0]. cells[i,4]. text+ "', '" +FP2. Sheets [0]. cells[i,5]. text+ "')";
Cmd. ExecuteNonQuery ();
}
}
Conn. Close ();
return true;
}
catch (System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine ("Error writing to Excel:" +ex.) message);
}
return false;
}

This method is currently the most effective, if you do not understand where you can write communication

A



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.