How to properly set Column Width upon creating Excel file? (Column properties)

I normally do this in VB and its easier because Excel records macros in VB. So I normally go to Excel and save the macro I want to do. So that’s what I did now and I got this code: Columns(“E:E”).ColumnWidth = 17.29; Range(“E3”).Interior.Pattern = xlSolid; Range(“E3”).Interior.PatternColorIndex = xlAutomatic; Range(“E3”).Interior.Color = 65535; Range(“E3”).Interior.TintAndShade = 0; … 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

Cannot debug or run Word AddIn because the required version of Microsoft Office is not installed

Apparently, when the addin project is created, debug information such as the MS Office version and the executable path used for debugging is saved in the .CSPROJ file. So debug is not guaranteed to work on another machine. This information is saved in following XML node of the project file: ProjectExtensions \ VisualStudio \ FlavorProperties … 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

How to Freeze Top Row and Apply Filter in Excel Automation with C#

I figured it out! @Jaime’s solution to freezing the top row worked perfectly. And the following is my solution to applying the filter: Thanks, KBP // Fix first row workSheet.Activate(); workSheet.Application.ActiveWindow.SplitRow = 1; workSheet.Application.ActiveWindow.FreezePanes = true; // Now apply autofilter Excel.Range firstRow = (Excel.Range)workSheet.Rows[1]; firstRow.AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);

How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries

I highly recommend CSharpJExcel for reading Excel 97-2003 files (xls) and ExcelPackage for reading Excel 2007/2010 files (Office Open XML format, xlsx). They both work perfectly. They have absolutely no dependency on anything. Sample using CSharpJExcel: Workbook workbook = Workbook.getWorkbook(new System.IO.FileInfo(fileName)); var sheet = workbook.getSheet(0); … var content = sheet.getCell(colIndex, rowIndex).getContents(); … workbook.close(); Sample using … Read more