In this tutorial, we create an excel file in C# Console Application
Reading Excel file read Reading Excel file in C# Console Application post
Firstly, we must add COM reference that name is “Microsoft Excel Object” from the right side in solution explorer.
Step 1: Rigth click on References then click Add Reference
Step 2: Click COM and Search for Excel library. Then use Microsoft Excel Object Library ( Maybe version will be changed on your computer)
Step 3: Add those using to alias
1 2 3 | using Excel = Microsoft.Office.Interop.Excel; |
Step 4: Enter message and filename for sample
1 2 3 4 5 6 7 8 | string fileName,Sampletext; Console.Write("Enter File Name :"); fileName = Console.ReadLine(); Console.Write("Enter text :"); Sampletext = Console.ReadLine(); |
Step 5: Create Excel Application sample object
1 2 3 4 | //Create excel app object Excel.Application xlSamp = new Microsoft.Office.Interop.Excel.Application(); |
Step 6: Check if Excel is installed
1 2 3 4 5 6 7 8 | if (xlSamp == null) { Console.WriteLine("Excel is not Insatalled"); Console.ReadKey(); return; } |
Step 7: Create a new excel book and sheet
1 2 3 4 5 6 | //Create a new excel book and sheet Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; |
Step 8: Then add a sample text into first cell
1 2 3 4 5 6 | //Then add a sample text into first cell xlWorkBook = xlSamp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = Sampletext; |
Step 9: Save the opened excel book to custom location. Dont forget, you have to add to exist location and you cant add to directly C: root.
1 2 3 4 5 6 | string location = @"E:\" + fileName + ".xls"; xlWorkBook.SaveAs(location, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlSamp.Quit(); |
Step 10: This is Importent for free memory and excel file. Release the Excel Object as this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //release Excel Object try { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSamp); xlSamp = null; } catch (Exception ex) { xlSamp = null; Console.Write("Error " + ex.ToString()); } finally { GC.Collect(); } |
All of the Codes that for Creating Excel File in C# Console Application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | class Program { static void Main(string[] args) { string fileName,Sampletext; Console.Write("Enter File Name :"); fileName = Console.ReadLine(); Console.Write("Enter text :"); Sampletext = Console.ReadLine(); //Create excel app object Excel.Application xlSamp = new Microsoft.Office.Interop.Excel.Application(); if (xlSamp == null) { Console.WriteLine("Excel is not Insatalled"); Console.ReadKey(); return; } //Create a new excel book and sheet Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; //Then add a sample text into first cell xlWorkBook = xlSamp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = Sampletext; //Save the opened excel book to custom location string location = @"D:\" + fileName + ".xls";//Dont forget, you have to add to exist location xlWorkBook.SaveAs(location, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlSamp.Quit(); //release Excel Object try { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSamp); xlSamp = null; } catch (Exception ex) { xlSamp = null; Console.Write("Error " + ex.ToString()); } finally { GC.Collect(); } } } |
🙂
I copied this code, and it is creating an Excel file but not writing to it. Any idea why?