Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 27 March 2020

How to create Excel file in C#


Hi developers, the article is all about the development of an Excel file. And if you ask me how excited I am working on Excel, frankly I always try to walk away from these reporting kinds of development tasks. But it’s life and you can’t hide. So, folks, I have structured and well prepared this excel development series that will work out for everyone and in any situation. As far as I am concern about creating Excel file using C#, I will suggest a developer should proceed with below two options: -
  • Using Microsoft.Office.Interop.Excel library
  • Using EPPlus library (Mostly Recommended)
I will go through both the Excel development library options one by one.


In this blog, we will go step by step to create Excel with option 1.

Microsoft.Office.Interop.Excel

About
C# by default gives this library under a set of Microsoft Office libraries by which you can access other Office Objects like Excel, Word, InfoPath, Access, MS Project, OneNote, Outlook, PowerPoint, Publisher, SharePoint, Visio, and SmartTag.

Create a new Console Application
1. Open Visual Studio.
2. Create a New Console Application (.NET Framework).
3. Type a name for your project in the Name field.

Click OK.

The new project appears in Solution Explorer.

Add References
In Solution Explorer, right-click on your references folder and then click Add Reference. The Reference Manager dialog box appears.

Select the Assemblies tab on left, select Microsoft.Office.Interop.Excel. Click OK.

Practical Implementations

1.    Open the Program.cs/Class1.cs file.
2.    Add the below namespace before start developing an Excel file.

using Excel = Microsoft.Office.Interop.Excel;

3.    Your complete code will look like this:

using System;
using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;

namespace DemoConsoleApplication
{
    public class CreateExcelFileUsingMicrosoftOffice
    {
        public static void Main(string[] args)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 1] = "Call";
            xlWorkSheet.Cells[1, 2] = "Company";
            xlWorkSheet.Cells[1, 3] = "Contact";
            xlWorkSheet.Cells[1, 4] = "Phone";
            xlWorkSheet.Cells[2, 1] = "1";
            xlWorkSheet.Cells[2, 2] = "2ML Real Estate";
            xlWorkSheet.Cells[2, 3] = "Max Louis";
            xlWorkSheet.Cells[2, 4] = "(4655767)98-876";
            xlWorkSheet.Cells[3, 1] = "2";
            xlWorkSheet.Cells[3, 2] = "100 Club of Houston";
            xlWorkSheet.Cells[3, 3] = "Rick Robert";
            xlWorkSheet.Cells[3, 4] = "(565767)798-876";

            xlWorkSheet.Columns[1].AutoFit();
            xlWorkSheet.Columns[2].AutoFit();
            xlWorkSheet.Columns[3].AutoFit();
            xlWorkSheet.Columns[4].AutoFit();

            xlWorkSheet.Rows[1].Font.Bold = true;

            xlWorkBook.SaveAs("E:\\DemoMSInteropCSharpExcel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

            Console.WriteLine("Excel file created , you can find the file E:\\DemoMSInteropCSharpExcel.xls");
        }
    }
}


Output

Call
Company
Contact
Phone
1
2ML Real Estate
Max Louis
(4655767)98-876
2
100 Club of Houston
Rick Robert
(565767)798-876

  
Related Questions:


Ques- What does Marshal.ReleaseComObject() does?
Ans- The Marshal.ReleaseComObject() decrement the reference count of the Runtime Callable Wrapper (RCW) associated with the specified Component Object Model (COM) object.

Ques- What is Runtime Callable Wrapper (RCW)?
Ans- It is an object generated by CLR in order to allow a COM object to be accessed from managed code. Its functions are to pass data and marshal calls between COM object and .NET client.

Hope you find the article helpful and interesting. 

For any query, comment us below.



Click imagination hunt to read latest blogs.

Posted By – Manish Kumar Gautam

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...