I'm trying to figure out how to work with Excel through c #, I can't figure out how to add a sheet to an existing book. Sheets are in different files, I need to put them in one book. While doing so, but it is too expensive and clumsy:

using System; using System.IO; using Microsoft.Office.Interop.Excel; internal static class Program { private static void Main ( string[] args ) { var dir = Directory.GetCurrentDirectory(); var excelApp = new Application(); excelApp.Visible = true; excelApp.Workbooks.Open( $"{dir}\\MAN.xls" ); } } 
  • But why make Excel visible ??? do what you want with Visible = false and only then show it - Bald
  • @Bald56rus, I'm not worried about what is happening on the screen, but about the very concept of opening a book on each new sheet. - anweledig
  • I tried to work with excel in due time, the first thing I learned is that it’s necessary to do operations on filling a book in visible = false. - Bald
  • one
    Add using, it is not clear what you are using - astef
  • @Bald56rus, technically apparently so, I thought that you can open the sheet directly into an existing book, and not create a new one. You know, you can just create an excel sheet, or does this suggest that a priori it is in a separate book? - anweledig

1 answer 1

Use Microsoft.Office.Interop.Excel is desirable only if you need to create an instance of Excel and work with it in an interesting mode.

It is more reliable to work with files in Excel format through NPOI or OpenXml libraries , which do not require installation of Microsoft Excel itself. To work on the server, installing client applications is undesirable.

NPOI is the java port of the xls library for working with files (up to Excel 2007).

OpenXML is a library for working with the new xslx format (Excel 2007 and higher)

Both libraries contain functionality for working with other types of Microsoft Office documents.