By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

Using C# to work in Excel

P: 1
Hi, my application needs to open up an excel workbook and store data there. I'm able to write into the cells (I see them written there as the program excutes) but as soon as it's over and I open the excel file nothing is there. I really don't understand it. Here is my code

Expand|Select|Wrap|Line Numbers
  1. Excel.Workbook theWorkbook;    
  2. Excel.Application ExcelObj = new Excel.Application();
  3. theWorkbook = ExcelObj.Workbooks.Open(path + "\\Results.xls",0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
  4.  
  5. Excel.Worksheet worksheet = (Excel.WorksheettheWorkbook.Worksheets.get_Item(1);
  6.  
  7. worksheet.Activate();
  8. ExcelObj.Visible = true;
  9.  
  10. int j = 1;
  11.  
  12. foreach(DictionaryEntry de in htMasterTable)
  13. {
  14.      ((Excel.Range)worksheet.Cells[j,1]).Interior.Color = 0x00ff0000;
  15.      j++;
  16. }
  17.  
  18.  
  19. heWorkbook.Save();
  20.  
  21. theWorkbook.Close((System.Boolean)false,System.Reflection.Missing.Value,System.Reflection.Missing.Value );
  22.  
  23. ExcelObj.Quit();
  24.  
Thanks. Any assitance would be great cause I'm dieing here.
Jun 13 '07 #1
Share this Question
Share on Google+
1 Reply


nateraaaa
Expert 100+
P: 663
Hi, my application needs to open up an excel workbook and store data there. I'm able to write into the cells (I see them written there as the program excutes) but as soon as it's over and I open the excel file nothing is there. I really don't understand it. Here is my code

Expand|Select|Wrap|Line Numbers
  1. Excel.Workbook theWorkbook;    
  2. Excel.Application ExcelObj = new Excel.Application();
  3. theWorkbook = ExcelObj.Workbooks.Open(path + "\\Results.xls",0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
  4.  
  5. Excel.Worksheet worksheet = (Excel.WorksheettheWorkbook.Worksheets.get_Item(1);
  6.  
  7. worksheet.Activate();
  8. ExcelObj.Visible = true;
  9.  
  10. int j = 1;
  11.  
  12. foreach(DictionaryEntry de in htMasterTable)
  13. {
  14.      ((Excel.Range)worksheet.Cells[j,1]).Interior.Color = 0x00ff0000;
  15.      j++;
  16. }
  17.  
  18.  
  19. heWorkbook.Save();
  20.  
  21. theWorkbook.Close((System.Boolean)false,System.Reflection.Missing.Value,System.Reflection.Missing.Value );
  22.  
  23. ExcelObj.Quit();
  24.  
Thanks. Any assitance would be great cause I'm dieing here.
Try this line of code
Expand|Select|Wrap|Line Numbers
  1. theWorkBook.SaveCopyAs(@"C:\Book1.xls");
Nathan
Jun 13 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.