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

How to access excel cells using csharp?

atksamy
P: 91
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
Share this Question
Share on Google+
1 Reply


100+
P: 283
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

Post your reply

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