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;
}
}
}
}