473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access excel cells using csharp?

atksamy
91 New Member
Hi,

I am trying to write some data into excel sheets using c#.
so far i have written the following code. But i am not able to proceed further

Expand|Select|Wrap|Line Numbers
  1.  
  2. Microsoft.Office.Interop.Excel.ApplicationClass appExcel = 
  3. new Microsoft.Office.Interop.Excel.ApplicationClass();
  4. try
  5. {
  6. Workbook newWorkbook = null;
  7. // is there already such a file ?
  8. if (System.IO.File.Exists("C:\\csharp\\errorreport1.xls"))
  9. {
  10. // then go and load this into excel
  11. newWorkbook = appExcel.Workbooks.Open("C:\\csharp\\errorreport1.xls",
  12. true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  13. Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  14. Missing.Value, Missing.Value, Missing.Value);
  15. }
  16. else
  17. {
  18. // if not go and create a workbook:
  19. newWorkbook = appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
  20. }
  21.  
  22. objsheet.Cells[i, j].Value = value;
  23.  
but i am getting an error in the last statement how am i supposed to declare objsheet
Feb 4 '09 #1
1 5517
jg007
283 Contributor
try something like -

Expand|Select|Wrap|Line Numbers
  1.       _Worksheet objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
  2.  
  3.  
I think there is probably a better way to set data for a cell but this seems to work -
Expand|Select|Wrap|Line Numbers
  1. objsheet.get_Range("A1","A1").set_Value(10,"Test Data");
  2.  
Feb 4 '09 #2

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

Similar topics

11
by: Heather | last post by:
I need suggestions on possible ways to implement the following: 10 users on a network need to infrequently add data to a backend database on the network. The data would be a couple of records...
1
by: Ronny Sigo | last post by:
Hello all, I am trying to import the contents of an Excel sheet into my Access Database. When clicking the button excel opens allright and does what I programmed (the cells get updated with the...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
7
by: Alan Roberts | last post by:
Can someone please explain the following for me... I am trying to link to a .NET DLL from Excel. Excel needs to pass a reference to itself to the DLL and then the DLL needs to perform some work...
6
by: gumbystation | last post by:
I am looking to take data from an excel spreadsheet into a csharp application to manipulate the data in various ways. Currently, I am using VS2005 (self-taught C#) and Excel 2000. I have...
4
by: Melissa | last post by:
I currently have VBA written to export query results into an Excel file. That file is then formatted using the code below. The problem I'm having is that it keeps throwing Error 91 (Object...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.