1

Is it possible to create an excel spreadsheet in visual studio without opening the Excel Program?

Does the data save in the database of the project?

3
  • 2
    Do you actually want Visual Studio to create the file, or do you want to write a C# application to write the file?
    – Wai Ha Lee
    Commented Feb 23, 2018 at 17:46
  • @WaiHaLee I would like to let the Visual Studio to create the file and the work/spreadsheet will appear into other form.
    – Don Gil
    Commented Feb 23, 2018 at 17:49
  • You need to do some research then make an attempt. Post the code where you are having trouble.
    – Mary
    Commented Mar 10, 2018 at 3:38

2 Answers 2

8

You can reference the assembly Microsoft.Office.Interop.Excel and generate the Excel file during your program's initialization like so

using Excel = Microsoft.Office.Interop.Excel;

// ... ...

var excel = new Excel.Application();

var workBooks = excel.Workbooks;
var workBook = workBooks.Add();
var workSheet = (Excel.Worksheet)excel.ActiveSheet;

workSheet.Cells[1, "A"] = "Foo";
workSheet.Cells[1, "B"] = "Bar";

// ...
workBook.SaveAs(Directory.GetCurrentDirectory()+"\\"+filename, Excel.XlFileFormat.xlOpenXMLWorkbook);

That is almost as effective as creating an Excel file in VS and adding it to the project's resources.

0

There's an office open XML SDK that works nicely, to create and modify all MS office file types. That's the best solution, IMHO. But, if you use really old versions of visual studio, they used to have project types that were tightly bound to Excel files. They could add buttons and processing right into the workbook. That functionality might still exist in some downloadable project type.

Not the answer you're looking for? Browse other questions tagged or ask your own question.