2010-08-26 106 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(); 
      } 


     } 

    } 
相关问题