Hello there,

I'm creating a web site using C# in visual studdio. I want to import data from an excel file to a SQL Server database.

I'm using the following code to open the excel file and read the data from cell A1 into a label:


protected void btnUpload_Click(object sender, EventArgs e)
{
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook newWorkbook =excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

string workbookPath = "c:/test.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "", false, false, 0, true, false, false);

Excel.Sheets excelSheets = excelWorkbook.Worksheets;

string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet =(Excel.Worksheet)excelSheets.get_Item(currentSheet);

Excel.Range excelCell =(Excel.Range)excelWorksheet.get_Range("A1", "A1");

lblTitle.Text = excelCell.Value2.ToString();

}


However i want to read in multiple values from the workbook and write them to my database.

Any ideas as to how i go about this?

I was thinking of maybe reading the values into a gridview and writing them to the database from there. Not exactly sure how to do thiis though.

Any help would be much appreciated,

Thanks,

Adrian