You must Sign In to post a response.
  • Category: .NET

    Read and Write excel file from C#

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    Hi,
    Please if somebody can help me,

    I am working project in C#, and i am in need to Open an existing xls file, read, write and save the xls file from C# .

    so if somebody can help me
    Thank u a lot
  • #924
    Hi

    1. In your project, Add reference to COM object library named under COM tab - Microsoft Excel 9.0 Object Library.

    2. After you add reference, (Interop.Excel.dll and Interop.Office.dll) files are copied to your bin directory.

    3. Excel.Application ExcelObj = = new Excel.Application();

    4. Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
    <Your File Name>, 0, true, 5,
    "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
    0, true);
    (Look in Object Browser for this method parameters)

    5. Excel.Sheets sheets = theWorkbook.Worksheets; (To get the worksheets)

    6. Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); (Get the first work sheet)

    7. You can read / write the values in worksheet using WorkSheet object created above.

    Use Object Browser to view the properties / methods of WorkSheet object.

    -SatheeshKarthik

  • #935
    Excel workbook can be opened just like any other OleDB like Access DB
    Connection string
    urlPath=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;HRD=1;IMEX=1";
    All Sql statemnets like select,update,delete etc. function on Excel DB
    Select Name from [Sheet1$]
    Name is column header
    Instead of fieldnames use column names.
    Instead of table use Sheet Names

  • #952
    thank u Satheeshkarthik

    I was doing the same as u are saying, the problem that i ad is exactly in the fourth step, when i write the following:

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
    , 0, true, 5,
    "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
    0, true);

    so i put the name of the file after Open as the first argument, for example the file name is test, i tried ......Open(test,0,true......);
    so the compiler gave me an error "the name file does not exist in the class or namespace
    i also tried to put test.xls and i tried to put the path before, but always i have an error.

    Thank u for your help

  • #205071
    There are better ways to Read/Write XLS/CSV/HTML/XLSX files than Excel Automation (http://www.gemboxsoftware.com/GBSpreadsheet.htm). You can use our Free Spreadsheet .NET Component (http://www.gemboxsoftware.com/GBSpreadsheetFree.htm) even in your commercial applications.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.