Work with an xlsx file without opening excel in c #?

2

I have this code to get the first cell of an arx of .xlsx

Excel.Application excelApp = new Excel.Application();
        excelApp.Visible = true;
        string workbookPath = "c:/Libro1.xlsx";
        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
        Excel.Sheets excelSheets = excelWorkbook.Worksheets;

        string currentSheet = "Hoja1";
        Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet); //en esta instruccion me sale la excepcion
        Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");




        string str = (excelCell.Cells as Excel.Range).Value2;

        MessageBox.Show(str);

but as soon as I execute it, I open the page with excel, is there any way to work with the file without opening the program?

    
asked by Edgar Diaz 20.07.2016 в 09:20
source

2 answers

2

I do not recommend that you use the COM libraries of Office to work the document, they are for problem. Use libraries based on open xml

ClosedXML - The easy way to OpenXML

in the documentation explains how to access the cells in a very simple way, you will not have to have an office installed to operate with the excel.

> > but as soon as I execute it, I open the excel sheet

You must define the property

excelApp.Visible = false;

If you define that it is visible, the application will be displayed

    
answered by 20.07.2016 / 13:23
source
1

I have worked with the EPPlus library with very good results. You can install it in Visual Studio from this link:

link

This library will not open the Excel application nor does it need to be installed.

You can find more information at link

In particular in this link you can see what it supports and what it does not support: link

I hope it serves you.

    
answered by 20.07.2016 в 13:09