how we can export the Any field from db table into Excel sheet using asp.net MVC c#
Microsoft Net Framework

To export data from an inquiry table to an Excel sheet using ASP.NET MVC and C#, you can follow these steps:

Step 1: Create a method in your controller that retrieves the inquiry data from your database or any other source. Let's assume your method is called "GetInquiryData".

----------------------------csharp----------------------------


public ActionResult GetInquiryData()
{
    // Retrieve the inquiry data from your source (e.g., database)
    var inquiryData = // Retrieve the data and format it as per your requirement

    return View(inquiryData);
}
 

----------------------------------------------------------------------

Step 2: Create a view to display the inquiry data. This view will have a button to trigger the export to Excel functionality. Let's assume your view is called "InquiryDataView".

----------------------------html----------------------------


@model IEnumerable<YourNamespace.InquiryData>

<table>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <!-- Add more columns as per your requirement -->
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Column1</td>
            <td>@item.Column2</td>
            <!-- Add more columns as per your requirement -->
        </tr>
    }
</table>

<button id="exportBtn">Export to Excel</button>

<script>
    document.getElementById('exportBtn').addEventListener('click', function () {
        window.location.href = '/ControllerName/ExportToExcel';
    });
</script>

------------------------------------------------------------------------------------

Step 3: Create a method in your controller to handle the export functionality. Let's assume your method is called "ExportToExcel".

----------------------------csharp--------------------------------------------------------


public void ExportToExcel()
{
    var inquiryData = // Retrieve the inquiry data from your source (e.g., database)

    // Create a new Excel package
    using (var package = new ExcelPackage())
    {
        // Create a new worksheet
        var worksheet = package.Workbook.Worksheets.Add("InquiryData");

        // Set the headers
        worksheet.Cells[1, 1].Value = "Column 1";
        worksheet.Cells[1, 2].Value = "Column 2";
        // Add more headers as per your requirement

        // Set the data
        int row = 2;
        foreach (var item in inquiryData)
        {
            worksheet.Cells[row, 1].Value = item.Column1;
            worksheet.Cells[row, 2].Value = item.Column2;
            // Add more columns as per your requirement

            row++;
        }

        // Set the response headers
        Response.Clear();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=InquiryData.xlsx");

        // Write the Excel package to the response stream
        Response.BinaryWrite(package.GetAsByteArray());
        Response.End();
    }
}

------------------------------------------------------------------------------------

Note: Make sure to add the necessary references and namespaces, such as `System.Web.Mvc` and `OfficeOpenXml`, for the above code to work. You may need to install the "EPPlus" NuGet package to work with Excel files.

This implementation uses the EPPlus library to generate the Excel file. The "ExportToExcel" method creates an Excel package, adds a worksheet, populates it with the inquiry data, and writes the package to the response stream. When the user clicks the "Export to Excel" button on the view, it triggers the "ExportToExcel" method, and the generated Excel file will be downloaded by the user.

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM
03
07

New Messages

George Floyd
  • Edit Post Edit This Post within a Hour
  • Hide Chat Hide This Post
  • Delete Chat If inappropriate Post By Mistake
  • Report Inappropriate Chat
  • 4.5kb
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks
  • Hi James! Please remember to buy the food for tomorrow! I’m gonna be handling the gifts and Jake’s gonna get the drinks