0

I have this code that is supposed to create a excelfile and save it to the folder that my app is in. Though the code below is apart of what i tried i have also tried .value2 = "" and i have tried to change arount to only .Cells[]= but i keep on getting the error Objectreference has not been given to an instance of an object. I hvae no clue why this is happening i have looked through stackoverflow to and seen that there is another question that gives an answer on this ut that answer does not work in my app would be gratefull for whatever help you can give me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;

namespace ExcelConverter
{
    class ExcellCreation
    {
        private int WhatExcellFile;
        private string name;


        public ExcellCreation(int WhatExcellFile)
        {
            this.WhatExcellFile = WhatExcellFile;



            switch (WhatExcellFile)
            {
                case 1:
                    this.name = "Categories1";
                    break;
                case 2:
                    this.name = "Categories2";
                    break;
                case 3:
                    this.name = "Categories3";
                    break;
            }

        }
        public string createExcellFile(List<String> excelCell, int rowAmmount)
        {
            Application xlApp = new Application();
            Workbook xlBook = null;
            Worksheet xlSheet = null;



            xlBook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            try
            {


                xlSheet.Cells[1, 1].Value2 = "hejsan hoppsan";



                xlBook.Worksheets[1].Name = "Kategoriids";
                xlBook.SaveAs(@"..\" + name + ".xlsx");
                xlBook.Close();
                xlApp.Quit();

                return "Fungerar";
            }
            catch (Exception e)
            {
                return e.Message;

            }







        }

    }
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • A `NullReferenceException` is easy to fix - find which variable or property is null, and make sure it has a value. The duplicate explains how to debug this, how to find *which* property or variable is null etc. You don't need to use Excel Interop at all to generate Excelfiles though. `xlsx` files are ZIP packages containing XML files. You can use the Open XML SDK or a library like [Epplus](https://github.com/JanKallman/EPPlus/wiki/Getting-Started) to create real `xlsx` files without installing Excel – Panagiotis Kanavos Feb 06 '20 at 10:17
  • But im trying to add a cell value to the excel file to and im getting an error??? – Jens Svensson Feb 06 '20 at 11:54
  • Where? What line? Did you check the variables or properties used there? One of them is clearly `null`. Again, the duplicate explains how to debug this, how to check the variables and properties. Although, just by looking at this, where is `xlSheet` set? – Panagiotis Kanavos Feb 06 '20 at 11:56
  • BTW that's why variables should be declared where they are used, not at the top of a method. In this case, `xlSheet` is useless outside the `try`. Even `xlBook` shouldn't be declared before the `Workbooks.Add` call, it should be `var xlBook=xlApp.Workbooks.Add(...)`. As for `xlSheet`, a new workbook has some sheets already, so *inside* the `try`, you could use `var xlSheet=xlBook.ActiveSheet` to get the first one. It's still *FAR* better to use Epplus though. For example, on a web server you won't find Excel – Panagiotis Kanavos Feb 06 '20 at 12:02

0 Answers0