2010-08-26 70 views
0

Excel 2003中= literature.xls
表:線路卡,數據手冊,並提示讀取多個Excel工作表到C#

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.OleDb; 

public partial class literature : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    LoadGrid(0); 
} 
protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    LoadGrid(e.NewPageIndex); 
} 

void LoadGrid(int LineCards) 
{ 
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
"Data Source=" + Server.MapPath("literature\\literature.xls") + ";" + 
"Extended Properties=Excel 8.0;"; 

    // Create connection object by using the preceding connection string. 
    OleDbConnection objConn = new OleDbConnection(sConnectionString); 

    // Open connection with the database. 
    objConn.Open(); 

    // The code to follow uses a SQL SELECT command to display the data from the worksheet. 

    // Create new OleDbCommand to return data from worksheet. 
    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [LineCards$]", objConn); 

    // Create new OleDbDataAdapter that is used to build a DataSet 
    // based on the preceding SQL SELECT statement. 
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

    // Pass the Select command to the adapter. 
    objAdapter1.SelectCommand = objCmdSelect; 

    // Create new DataSet to hold information from the worksheet. 
    DataSet objDataset1 = new DataSet(); 

    // Fill the DataSet with the information from the worksheet. 
    objAdapter1.Fill(objDataset1, "XLData"); 

    // Bind data to DataGrid control. 
    grd.DataSource = objDataset1.Tables[0].DefaultView; 
    grd.PageIndex = LineCards; 
    grd.DataBind(); 

    // Clean up objects. 
    objConn.Close(); 
} 

} 

回答

2

你將需要一次爲每個文件執行這個代碼。從本質上講,只需創建一個函數來處理它,並只傳遞你的connectionString函數(或者它的任何元素改變)。這假設您正在查詢的所有文件都有您正在查找的正確數據。

這裏是一個函數的例子,以及如何調用它。

//I don't know what values LineCards is supposed to be, so I am just passing 5, 6, and 7. 
//Put these calls where your LoadGrid() call is currently. 

assignExcelSheetToGrid (Server.MapPath("literature\\literature.xls"), grd, 5); 
assignExcelSheetToGrid (Server.MapPath("literature\\literature2.xls"), grd2, 6); 
assignExcelSheetToGrid (Server.MapPath("literature\\literature3.xls"), grd3, 7); 

,功能....我假設你的第一個功能的工作,所以我只是重新使用您的代碼。如果您的原始函數適用於一個網格,則此代碼應適用於任何數字。

function assignExcelSheetToGrid(string thePath, YOURGRIDTYPE theGrid, int LineCards){ 
    ///This replaces LoadGrid function 
    //Make sure you change YOURGRIDTYPE (Above) to the type of grid you are passing 

    String theConnString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + thePath + ";Extended Properties=Excel 8.0;"; 

    OleDbConnection objConn = new OleDbConnection(theConnString); 
    objConn.Open(); 

    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [LineCards$]", objConn); 
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

    objAdapter1.SelectCommand = objCmdSelect; 
    DataSet objDataset1 = new DataSet(); 
    objAdapter1.Fill(objDataset1, "XLData"); 

    theGrid.DataSource = objDataset1.Tables[0].DefaultView; 
    theGrid.PageIndex = LineCards; 
    theGrid.DataBind(); 

    objConn.Close(); 
} 

UPDATE

函數現在需要的文件路徑,而不是整個連接字符串。

+0

我想我會需要爲每一個功能,並害怕這一點。你能指出我的方向嗎?我是C#的新手。 謝謝 – Gene 2010-08-26 17:59:06

+0

你只需要一個函數來處理任意數量的文件。您只需要爲每個圖紙/網格組合打1個電話即可。以示例查看上面更新的帖子。還有一些空間可以使代碼凝結,所以如果你想發佈你的最終代碼,我會幫你解決這個問題。 – Dutchie432 2010-08-26 18:17:46

+0

謝謝..會玩這個。我的問題是將「assignExcelSheetToGrid」放在.cs文件中的位置等。我很樂意在這裏發佈我的整個.cs文件,但是當我這樣做的時候它會把它弄糟。 – Gene 2010-08-26 18:29:49

1

這是正確的代碼.. 其成功地運行..

保護無效bttnUpload_Click(對象發件人,EventArgs的){

 if (fupUploadData.HasFile) 
     { 
      try 
      { 
       ///Your connectionstrings here... 
       string path = string.Concat(Server.MapPath("~/Files/" + fupUploadData.FileName)); 

       fupUploadData.SaveAs(path); 

       txtUploadData.Text = Server.MapPath(fupUploadData.FileName); 

       string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path); 

       OleDbConnection connection = new OleDbConnection(); 

       connection.ConnectionString = excelConnectionString; 

       OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection); 

       connection.Open(); 



       DbDataReader dr = command.ExecuteReader(); 



       string consString = ConfigurationManager.ConnectionStrings["mRetailerEntities"].ConnectionString; 


       SqlBulkCopy bulkInsert = new SqlBulkCopy(consString); 

       bulkInsert.DestinationTableName = "offer_master"; 

       bulkInsert.WriteToServer(dr); 

       lblMsg.Text = "File uploaded Successfully"; 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
      finally 
      { 
       con.Close(); 
       con.Dispose(); 
      } 


     } 

    }