opening Excel 2003 file by c#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: hesham_51 is an unknown quantity at this point 
Solved Threads: 0
hesham_51 hesham_51 is offline Offline
Newbie Poster

opening Excel 2003 file by c#

 
0
  #1
Jan 3rd, 2009
Hello everyone

First ,My name is Hashem I'm a beginner in c#, I'm trying to open Excel file(2003) but I get error at Open Function in the below code :


(Old Format Or Invalid Library Of kinds (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))


Is there anybody can help me? where I use the following code that i have collected from many reference.
The replies are valuable.



public static Excel.Application ExlApp;
public static Excel.Workbook ExlWrkBook;
public static Excel.Worksheet ExlWrkSheet;


Excel.Application ExcApp = new Excel.Application();
ExcApp.Visible = true;
Excel.Workbook excelWorkbook = ExcApp.Workbooks.Open("D:\\Salary Calc.xls", 0, false, 5, "", "", true,Excel.XlPlatform.xlWindows, "\t", true, false, 0, false, true, true);

Excel.Sheets ExcSheet = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)ExcSheet.get_Item(currentSheet);
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: opening Excel 2003 file by c#

 
0
  #2
Jan 3rd, 2009
What's your Office SDK version?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: opening Excel 2003 file by c#

 
0
  #3
Jan 3rd, 2009
And, more to the point, which version of excel did you add as a reference?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: hesham_51 is an unknown quantity at this point 
Solved Threads: 0
hesham_51 hesham_51 is offline Offline
Newbie Poster

Re: opening Excel 2003 file by c#

 
0
  #4
Jan 5th, 2009
Hi
I'm using

Microsoft.Office.Interop
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: pferian is an unknown quantity at this point 
Solved Threads: 0
pferian pferian is offline Offline
Newbie Poster

Re: opening Excel 2003 file by c#

 
0
  #5
May 20th, 2009
Hy,
I did something like what you want in VC++ .NET Framework language. Just use the same classes in C# and it should work (there are some peaces of code that are usefull in my application but do no refer to Excell handling - just not care of). Please note that you have to install Excel 2003 and Windows XP or higher in the system where the application runs.

private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ XlsFile = String::Format("Z:\\Documenti\\Lavoro\\Fatture\\2009\\Fattura_02_AEM_marzo_09.xls");
String^ XlsSheet = String::Format("Milano, {0}/{1}/{2}",
dateTimePicker1->Value.Day.ToString(),
dateTimePicker1->Value.Month.ToString(),
dateTimePicker1->Value.Year.ToString());

Excel::Application^ oXLApp;
Excel::Workbook^ oXLWBook;
//Excel::Sheets^ oXLSheet;
//Excel::Worksheet^ oXLWSheet;

Excel::Range^ rng;

//starts Excel application
oXLApp = (gcnew Excel::Application());
oXLApp->Visible = true;
//opens the workbook of the file "XlsFile"
oXLWBook = oXLApp->Workbooks->Open(XlsFile, 0, false, 5, "", "",
true,Excel::XlPlatform::xlWindows, "\t", true, false, 0, false, true, true);
//updates the range of sheet1, cell D4
rng = oXLApp->Range::get("D4","D4");
rng->Value2 = XlsSheet;

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,552
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 451
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: opening Excel 2003 file by c#

 
0
  #6
May 20th, 2009
Hi!
Hope this code will resolve your problem.

  1. using System.Collections;
  2. using Microsoft.Office.Interop.Excel;
  3. using System;
  4. using System.Reflection;
  5. class Sample
  6. {
  7. static void Main()
  8. {
  9. string file = @"c:\csnet\jap\ex1\sample1.xls";
  10. Microsoft.Office.Interop.Excel.ApplicationClass ap = new ApplicationClass();
  11. Missing m=Missing.Value;
  12. Workbook wb=ap.Workbooks.Open(file, m, m, m, m, m, m, m, m, m, m, m, m, m, m);
  13. Worksheet sh =(Worksheet) wb.Sheets[1];
  14. for (int i = 1; i < 10; i++)
  15. {
  16.  
  17. string[] p = new string[10];
  18. for (int j = 1; j <= 10; j++)
  19. {
  20. Range r = (Range)sh.Cells[i, j];
  21. p[j - 1] = r.Value2;
  22. }
  23.  
  24. }
  25. ap.Quit();
  26.  
  27. }
  28. }
Last edited by adatapost; May 20th, 2009 at 10:32 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 13
Reputation: Poojasrivastava is an unknown quantity at this point 
Solved Threads: 0
Poojasrivastava Poojasrivastava is offline Offline
Newbie Poster

Re: opening Excel 2003 file by c#

 
0
  #7
May 21st, 2009
i did the same thing using javascript..may be that helps you..
here is the javascript code:
  1. <html>
  2.  
  3. <head>
  4.  
  5. <title></title>
  6.  
  7. </head>
  8.  
  9. <body>
  10. <script type="text/javascript">
  11. function read()
  12. {
  13.  
  14. var myApp = new ActiveXObject("Excel.Application");
  15. if (myApp != null)
  16. {
  17. myApp.visible = true;
  18. myApp.workbooks.open("C:\\Book2.xls");
  19. }
  20. }
  21.  
  22. </script>
  23. <button onclick="read();">READ</button>
  24. </body>
  25.  
  26. </html>
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,552
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 451
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: opening Excel 2003 file by c#

 
0
  #8
May 21st, 2009
Originally Posted by Poojasrivastava View Post
i did the same thing using javascript..may be that helps you..
here is the javascript code:
  1. <html>
  2.  
  3. <head>
  4.  
  5. <title></title>
  6.  
  7. </head>
  8.  
  9. <body>
  10. <script type="text/javascript">
  11. function read()
  12. {
  13.  
  14. var myApp = new ActiveXObject("Excel.Application");
  15. if (myApp != null)
  16. {
  17. myApp.visible = true;
  18. myApp.workbooks.open("C:\\Book2.xls");
  19. }
  20. }
  21.  
  22. </script>
  23. <button onclick="read();">READ</button>
  24. </body>
  25.  
  26. </html>

Nice code. You should post this one on Web forum.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 13
Reputation: Poojasrivastava is an unknown quantity at this point 
Solved Threads: 0
Poojasrivastava Poojasrivastava is offline Offline
Newbie Poster

Re: opening Excel 2003 file by c#

 
0
  #9
May 21st, 2009
i hope it helped you,if it did then mark it as solved...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC