Function to write cells in excel

228 views Asked by At

I get a bit of code from here: http://forum.codecall.net/topic/71788-reading-excel-files-in-c/ which works fine and is easy to understand for me. It can only read excel cells tho, and I want to write some too. So I want to do this in my main: excel_setValue("C10", "520");

This is what I tried as function:

    private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
    private static Workbook newWorkbook = null;
    private static _Worksheet objsheet = null;


static string excel_setValue(string cellname, string value)  
        {
            if (objsheet.get_Range(cellname).get_Value().ToString() == string.Empty) // Here is error
            {
                Console.WriteLine("watch out u trying to overwrite files");
            }
            else
            {
                objsheet.get_Range(cellname).set_Value(value);
            }         
        }

It tells me: NullReferenceExpection was unhandled - Object reference not set to an instance of an object.

Another error I got when I only put this:

static void excel_setValue(string cellname, string value)
        {
            //if (objsheet.get_Range(cellname).get_Value().ToString() == string.Empty)
            //{
            //    Console.WriteLine("kijk uit je probeerd cellen te overschrijven");
            //}
            //else
            //{
                objsheet.get_Range(cellname).set_Value(value);
            //}         
        }

Error: COMException was unhandled: Exception from HRESULT: 0x800A03EC

How I call my excel_setValue:

excel_init("C:\\");
excel_setValue("B10","520");

excel_init:

static void excel_init(String path)
        {
            appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

            if (System.IO.File.Exists(path))
            {
                // then go and load this into excel
                newWorkbook = appExcel.Workbooks.Open(path, true, true);
                objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
            }
            else
            {
                Console.WriteLine("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
            }

        }
1

There are 1 answers

5
JWiley On BEST ANSWER

As shown in the code you linked in the method excel_init, you need to initialize objsheet. It is done on this line in the link:

objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;

For this edit in your question:

How I call my excel_setValue:

excel_setValue("B10","520");
excel_init("C:\\");

Is that the order of execution you're using? You have to call excel_init first and initialize objsheet before using it in excel_setValue.

You may be using get_Range incorrectly, see this question for further examples.

You are also using Range.set_Value incorrectly, try using objsheet.get_Range(cellname).set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault,value);