Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a task to export the html data to excel using C#. I have converted the data into JSON object and how to create a excel from the Custom object.I have a custom object as Employee which has properties as Name,Address and Salary.

Can any one help me regarding this ?

[edit]unnecessary code block removed[/edit]
Posted
Updated 9-Mar-14 5:00am
v2
Comments
Kornfeld Eliyahu Peter 9-Mar-14 10:17am    
Excel can interpret tabular data only. Is your HTML tabular? If does you can save it as is and do open html from Excel...
hemantwithu 10-Mar-14 8:18am    
I have solved it. Please check the below answer. :)

1 solution

I have solved the problem as given below
C#
/// <summary>
    /// Export to excel operation is done.
    /// </summary>
    /// <param name="objEmployee"></param>
    public void ExportToExcel(List<Employee> objEmployee)
    {
        // Load Excel application
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

        // Create empty workbook
        excel.Workbooks.Add();

        // Create Worksheet from active sheet
        Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;

        // I created Application and Worksheet objects before try/catch,
        // so that i can close them in finnaly block.
        // It's IMPORTANT to release these COM objects!!
        try
        {
            // ------------------------------------------------
            // Creation of header cells
            // ------------------------------------------------
            workSheet.Cells[1, "A"] = "ID";
            workSheet.Cells[1, "B"] = "Name";
            workSheet.Cells[1, "C"] = "Address";
            workSheet.Cells[1, "D"] = "State";
            workSheet.Cells[1, "E"] = "Country";

            // ------------------------------------------------
            // Populate sheet with some real data from "employees" list
            // ------------------------------------------------
            int row = 2; // start row (in row 1 are header cells)
            foreach (BusinessModels.Employee emp in objEmployee)
            {
                workSheet.Cells[row, "A"] = emp.ID;
                workSheet.Cells[row, "B"] = emp.Name;
                workSheet.Cells[row, "C"] = emp.Address;
                workSheet.Cells[row, "D"] = emp.State;
                workSheet.Cells[row, "E"] = emp.Country;

                row++;
            }

            // Apply some predefined styles for data to look nicely :)
            workSheet.Range["A1"].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);

            // Define filename
            string fileName = string.Format(@"{0}\ExportedData.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            // Save this data as a file
            workSheet.SaveAs(fileName);

            // Display SUCCESS message
            ExcelMessages.ErrorMessage = (string.Format("The file '{0}' is saved successfully!", fileName));
        }
        catch (Exception exception)
        {
            throw new Exception("There was a PROBLEM saving Excel file!\n" + exception.Message);
        }
        finally
        {
            // Quit Excel application
            excel.Quit();

            // Release COM objects (very important!)
            if (excel != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

            if (workSheet != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);

            // Empty variables
            excel = null;
            workSheet = null;

            // Force garbage collector cleaning
           GC.Collect();
        }
    }
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900