dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersRakesh Chaubey
sony
Varun Bansal
naveensanagasetti
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




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.



Previous : Is there any expert to solve this?
Return to Discussion Forum
Post New Message
Category:



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.