2013年10月3日 星期四

C#取得excel sheet個數和名稱

OleDbConnection objConn = null;
System.Data.DataTable dt = null;
 // Connection String. Change the excel file to the file you
// will search.
String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpContext.Current.Server.MapPath("C://a.xls") + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
// Create connection object by using the preceding connection string.
 objConn = new OleDbConnection(connString);
 // Open connection with the database.
 objConn.Open();
 // Get the data table containg the schema guid.
  dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

String[] excelSheets = new String[dt.Rows.Count];
int i = 0;

// Add the sheet name to the string array.
 foreach (DataRow row in dt.Rows)
{
        excelSheets[i] = row["TABLE_NAME"].ToString();
        i++;
 }

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁