tags:

views:

44

answers:

1

Excel 2003 = literature.xls
Sheets: LineCards, Data, Brochures, and Tips

    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();
}

}for your assistance.

+2  A: 

You're going to need to perform this code once for each file. Essentially, just create a function to take care of it, and just pass the function your connectionString (or whatever element of it changes). This assumes that all of the files you are querying have the correct data you are looking for.

Here is an example of the function, and how to call it.

//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);

And the function.... I assume your first function worked, so I am just re-using your code. If your original function worked for one grid, this code should work for any number.

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

function now takes the file path instead of the entire connection string.

Dutchie432
I figured I would need to create a function for each, and was afraid of this. Can you point me in the direction for this? I am new to C#.Thanks
Gene
You only need one function to handle any number of files. You simply need to make 1 call to that function for each Sheet/Grid Combo. See updated post above with sample. There is also some room for condensing the code, so if you want to post your final code, I will help you out with that.
Dutchie432
thanks..will play around with this. My problem is where to put the"assignExcelSheetToGrid" in the .cs file and such. I would be happy to post my entire .cs file on here but it messes it up when I do.
Gene
Just posted all the code....sorry for being stupid on this
Gene
That's OK.. Just replace your LoadGrid function with the function I gave you (updated, see above), then make the multiple calls to it in the same place you call loadGrid now.
Dutchie432
wow...now I feel lost. I did as you stated but get this error now:CS0103: The name 'assignExcelSheetToGrid' does not exist in the current contextYou have literature2.xls and literature3.xls so I am making more workbooks? Will this pull from the sheets within the existing literature.xls with the 4 sheets? I have them in the order listed in the opening description.Then what do the 5,6,a and 7 represent in the assignExcelSheetToGrid
Gene
Literature2 and Literature3 other the other excel sheets you want to put into other grids. You need to have one grid for each sheet, according to your accordion layout. Each one of those 3 calls assigns, opens, and populates a grid base on the location of the excel sheet path you send it. 5, 6, and 7 refer to the "LineCards" variable. I don't see where it's declared, unless I am missing something so I don't know what you were using it for before. I simply functionized the code you originally posted. Frankly, if you can't figure out how to declare and call a function, read the basic MSDN docs.
Dutchie432