user10400888
user10400888

Reputation: 1

VS C# - Microsoft Excel Object library reference

EDIT:

I am trying to use Microsoft Excel in Visual Studio C#.

Code:

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

namespace write_data_to_excel
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application xlApp = new Excel.Application();

            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 1] = "ID";
            xlWorkSheet.Cells[1, 2] = "Name";
            xlWorkSheet.Cells[2, 1] = "1";
            xlWorkSheet.Cells[3, 1] = "2";
            xlWorkSheet.Cells[3, 2] = "Two";

            xlWorkBook.SaveAs("C:\\examplewrite.xlsx", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();


        }
    }
}

Error:

System.Runtime.InteropServices.COMException: 'Cannot access 'examplewrite.xlsx'.'

I am using VS Community 2017.

Upvotes: 0

Views: 1059

Answers (1)

TsarTanner
TsarTanner

Reputation: 21

Yeah, it requires that all client machines have the same version of Microsoft Excel installed unfortunately.

more info found here: https://www.gemboxsoftware.com/spreadsheet/articles/c-sharp-microsoft-office-interop-excel-automation

Upvotes: 2

Related Questions