I have this code to get the first cell of a .xlsx file
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 run it, it opens the sheet with excel, is there any way to work with the file without opening the program?
I do not recommend that you use the Office COM libraries to work with the document, they are a problem. Use libraries based on
open xml
ClosedXML - The easy way to OpenXML
in the documentation it explains how to access the cells in a very simple way, what's more you won't have to have office installed to operate with excel.
>> but as soon as I run it, it opens the sheet with excel
You must define the property
if you define that it is visible the application will be shown
I have worked with the EPPlus library with very good results. You can install it in Visual Studio from this link:
https://www.nuget.org/packages/EPPlus/
This library will not open the Excel application nor does it need to be installed.
You can find more information at http://epplus.codeplex.com/
In particular, in this link you can see what it supports and what it does not support: http://epplus.codeplex.com/documentation
Hope this can help you.