Forums » .NET » .NET »

Read and Write excel file from C#


Posted Date: 16 Jun 2004      Posted By:: Johnny Yammine     Member Level: Bronze    Member Rank: 0     Points: 2   Responses: 4



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




Responses

#924    Author: Satheesh Karthikeyan      Member Level: Bronze      Member Rank: 0     Date: 16/Jun/2004   Rating: 2 out of 52 out of 5     Points: 2

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    Author: S S      Member Level: Bronze      Member Rank: 0     Date: 17/Jun/2004   Rating: 2 out of 52 out of 5     Points: 2

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    Author: Johnny Yammine      Member Level: Bronze      Member Rank: 0     Date: 17/Jun/2004   Rating: 2 out of 52 out of 5     Points: 2

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    Author: Filip      Member Level: Silver      Member Rank: 0     Date: 21/Mar/2008   Rating: 2 out of 52 out of 5     Points: 2

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.

 
Post Reply

 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.



Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Awards & Gifts
Talk to Webmaster Tony John

Online Members

Priya jain
More...
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India