none
Reading and Writing Excel Spreadsheets Using ADO.NET C# RRS feed

  • Question

  • Hi all, (apologies if this is in the wrong section)

    I am trying to read and write to an excel spreadsheet in c#. I want to put a web form on the front-end that will allow the user to read and write to the spreadsheet.

    This is a good tutorial i found: http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

    Though i don't know where abouts to put the code.

    Any help, any working examples please ?

    Thanks in advance.

    Monday, September 18, 2006 10:11 AM

All replies

  • Why are you trying to run a website from an Excel file? The example you gave is a standalone app to place data into a database...hence it doesn't fit in your scheme.

    You may want to look into Visual Studio Tools For Office (VSTO) and create a C# addin in a specific Excel file that will allow the user to modify the sheet...

    Here is information on VSTO tour and read this post, Can a VSTO 2005 Excel application be started from a Web site. Look at my response, which gives a design for usage which could apply to your situation and see if it interests you. If it does see the Capital Expenditure VSTO example.

    Otherwise check out some SQLStarter Kits which may be related. Good Luck.


    Thursday, September 21, 2006 9:49 PM
    Moderator
  • Hello,

    if you are using some ASP.NET data grid control to present the Excel data, then you could data bind it to DataTable and easily import / export DataTable to Excel with this C# / VB.NET Excel component.

    Here is a sample C# code:

    ExcelFile ef = new ExcelFile();
    DataTable dataTable = new DataTable();
     
    // Depending on the format of the input file, you need to change this:
    dataTable.Columns.Add("FirstName", typeof(string));
    dataTable.Columns.Add("LastName", typeof(string));
     
    // Load Excel file.
    ef.LoadXls("FileName.xls");
     
    // Select the first worksheet from the file.
    ExcelWorksheet ws = ef.Worksheets[0];
     
    // Extract the data from the worksheet to the DataTable.
    // Data is extracted starting at first row and first column for 10 rows or until the first empty row appears.
    ws.ExtractToDataTable(dataTable, 10, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[0], ws.Columns[0]);
     
    // Change the value of the first cell in the DataTable.
    dataTable.Rows[0][0] = "Hello world!";
     
    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
     
    // Save the file to XLS format.
    ef.SaveXls("DataTable.xls");

    Thursday, April 26, 2012 8:54 AM
  • This example is not using ADO.Net object.
    Wednesday, January 22, 2020 8:07 PM