Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#

To set a range to text: xlYourRange.NumberFormat = “@”; You can also prefix a value you put in a cell with an apostrophe for it to format it as text: xlYourRange.Value = “‘0123456”; To set a range to number xlYourRange.NumberFormat = “0”; Obviously if you want to set the format for the entire column then … 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

Use Office Interop on ASP.net MVC6 website

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. If you are building a solution that runs in a server-side … Read more

WinWord.exe won’t quit after calling Word.Documents.Add – Word .NET Interop

(All of my advice is adapted from this answer about Excel interop.) There are a few important things here: 1) Never use 2 dots on the same line. Also consider an indexer as a dot Good Word.Documents d = wordApp.Documents; Word.Document aDoc = d.Open(/*…*/); BAD Word.Document aDoc = wordApp.Documents.Open(/*…*/); 2) Release all of your pointers. … 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

The quest for the Excel custom function tooltip

I’ve posted a proof-of-concept project to GitHub as the Excel-DNA IntelliSense project, implementing this. Using the UI Automation classes to monitor the appropriate Excel user interface events, a form is displayed when appropriate. The code is wrapped as an Excel-DNA add-in, and works on my Excel 2013 / Windows 8 machine. I’ve tested on one … 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

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; }