Click here to Skip to main content
15,887,895 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Use COM Interop to Create an Excel in MVC

Rate me:
Please Sign up or sign in to vote.
2.60/5 (3 votes)
3 May 2017CPOL 13.6K   2   1
This tip will provide one of the ways of generating Excel files using Microsoft COM interop in MVC project.

Introduction

In this tip, you will see how to create Excel files using Microsoft COM Interop in MVC project.

Using the Code

Add reference of COM Interop in project.

Include reference to class file.

C#
//Including reference to class file
  using Microsoft.Office.Interop.Excel;

//Function to export excel file
 public ActionResult ExportToExcel()
 {
     string reportPath="your file path for excel";
     string reportName="YourReport.xlsb";

     System.Data.DataTable table = 
     GetDatatable();//-your code to create datatable and return it
     table.TableName = "Your Report Name"; 
     
     Microsoft.Office.Interop.Excel.Application excelApp = 
     new Microsoft.Office.Interop.Excel.Application();

     //Create an Excel workbook instance 
     Microsoft.Office.Interop.Excel.Workbook excelWorkBook = 
     excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

     Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
     excelWorkSheet.Name = Convert.ToString(table.TableName);
     excelWorkSheet.Columns.AutoFit();
               
     for (int i = 1; i < table.Columns.Count + 1; i++)
     {
        excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;                
     }

     for (int j = 0; j < table.Rows.Count; j++)
     {
        for (int k = 0; k < table.Columns.Count; k++)
        {
           excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
        }
     }
     
    //-- check file directory is present or not/if note create new
    bool exists = System.IO.Directory.Exists(reportPath);
    if (!exists)
    {
      System.IO.Directory.CreateDirectory(reportPath);
    }
     
     excelWorkBook.SaveAs(reportPath+reportName, 
     XlFileFormat.xlExcel12, Type.Missing, Type.Missing, 
     Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, 
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
     excelWorkBook.Close();
     excelApp.Quit();
     
     return File(reportPath+reportName, "application/vnd.ms-excel", reportName);      
    }
//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCreateDirectory Pin
luketongs4-May-17 1:06
luketongs4-May-17 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.