How to save workbook without showing save dialog with Excel interop?

All of the arguments to WorkBook.SaveAs() are optional, but you can just use Type.Missing for most of them if you want to. The typical call would look like: wbook.SaveAs(“c:\\temp\\blah”, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); wbook.Close(); Note that I didn’t include the file extension; Excel will set that for you. … Read more

Importing Excel into a DataTable Quickly

Caling .Value2 is an expensive operation because it’s a COM-interop call. I would instead read the entire range into an array and then loop through the array: object[,] data = Range.Value2; // Create new Column in DataTable for (int cCnt = 1; cCnt <= Range.Columns.Count; cCnt++) { textBox3.Text = cCnt.ToString(); var Column = new DataColumn(); … Read more

How to make correct date format when writing data to Excel

Did you try formatting the entire column as a date column? Something like this: Range rg = (Excel.Range)worksheetobject.Cells[1,1]; rg.EntireColumn.NumberFormat = “MM/DD/YYYY”; The other thing you could try would be putting a single tick before the string expression before loading the text into the Excel cell (not sure if that matters or not, but it works … Read more

What reference do I need to use Microsoft.Office.Interop.Excel in .NET?

Update (thanks user2347528) These assemblies are available as NuGet packages, which is much easier than my original answer. You can install by either right clicking on References in your project and selecting Manage NuGet packages… and searching for one of the packages listed below, or install using the Package Manager Console: PM> Install-Package Microsoft.Office.Interop.Excel Microsoft.Office.Interop.Excel … Read more

Why does Microsoft.Office.Interop.Excel.Application.Quit() leave the background process running?

Got it! application.Workbooks != application.Workbooks This property doesn’t expose a variable, it generates a value. So every time I access the Workbooks property I create a new COM object. I fixed the code and all is well. Thanks, everybody. var excelApplication = new Application(); var workbooks = excelApplication.Workbooks; var workbook = workbooks.Open(pathToExcelWorkbook); // Fixed workbook.Close(); … Read more

Get instance of Excel application with C# by Handle

Use the following code to get the first running instance of Excel: oExcelApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(“Excel.Application”); Example public Excel.Application StartExcel() { Excel.Application instance = null; try { instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(“Excel.Application”); } catch (System.Runtime.InteropServices.COMException ex) { instance = new Excel.ApplicationClass(); } return instance; }