473,378 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Using C# to work in Excel

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
1 5161
nateraaaa
663 Expert 512MB
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

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

Similar topics

0
by: Jeff | last post by:
I will start off by giving you a background of the process I am taking. The nature of my DTS package is that I recieve an Excel Spreadsheet, run it through the DTS Package applying validation to...
3
by: Prakash | last post by:
Hi, We face problems uploading excel (with macros) documents using HTML File Upload. The file contents are corrupted while viewing the same. However, we are able to upload excel (w/o. macros)...
4
by: Chris | last post by:
Hi, everything works apart from the last line :-(( rng.Value2.ToString() An exception is thrown : "Old format or invalid type library" It gets compiled though (so he recognizes the property...
0
by: Rich Wallace | last post by:
Hello all, Looking for suggestions and tips if possible. I have an application running on a file server that utilizes the FileSystemWatcher to trap when any Excel files are saved by a user. I...
4
by: Lisa | last post by:
Hi - I'm able to open excel workbooks and word documents, but I can't seem to copy excel charts, named ranges, etc. to a word document. Anyone know of good reference material in this area? What...
5
by: Mike in Santa Rosa | last post by:
I'm trying to get a simple c# app built that can launch/manipulate an excel workbook, sheet. I've chased down several examples and can't any of them to work. So I must be doing somethnig obviouslt...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
4
by: somanyusernamesaretakenal | last post by:
What I am trying to achieve: Basically I have generated a report in access. This report needs to be updated using excel. (Updating the new data, not changing existing data) What I did was I...
5
by: Rajesh | last post by:
Hi, Please help me to do an EXCEL report generation using ASP.NET. I am building an website. This should work in all machine. I need do load the data from SQL tables to excel sheet and user can...
8
by: Bruce_Stough | last post by:
I am currently using Control.DoDragDrop to drop a text string into Excel. I use the following code DataObject dd = new DataObject();...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.