How to access excel cells using csharp?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atksamy
    New Member
    • Oct 2008
    • 91

    How to access excel cells using csharp?

    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

    Code:
     
    Microsoft.Office.Interop.Excel.ApplicationClass appExcel = 
    new Microsoft.Office.Interop.Excel.ApplicationClass();
    try
    {
    Workbook newWorkbook = null;
    // is there already such a file ?
    if (System.IO.File.Exists("C:\\csharp\\errorreport1.xls"))
    {
    // then go and load this into excel
    newWorkbook = appExcel.Workbooks.Open("C:\\csharp\\errorreport1.xls",
    true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value);
    }
    else
    {
    // if not go and create a workbook:
    newWorkbook = appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    }
     
    objsheet.Cells[i, j].Value = value;
    but i am getting an error in the last statement how am i supposed to declare objsheet
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    try something like -

    Code:
          _Worksheet objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
    I think there is probably a better way to set data for a cell but this seems to work -
    Code:
    objsheet.get_Range("A1","A1").set_Value(10,"Test Data");

    Comment

    Working...