We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Export DataTable to excel

Thread ID:

Created:

Updated:

Platform:

Replies:

121022 Nov 3,2015 05:41 PM UTC Nov 5,2015 05:18 AM UTC ASP.NET Web Forms 3
loading
Tags: Grid
Manolo
Asked On November 3, 2015 05:41 PM UTC

Hi,

I fill a grid with a datatable and the columns are autogenerat. I want export the grid to excel.

My method is:

protected void gridDatos_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs
{         
    ExcelExport exp = new ExcelExport();
    var datos = ((DataTable)gridDatos.DataSource).AsEnumerable();
    exp.Export(gridDatos.Model, (IEnumerable)datos, "Export.xlsx", ExcelVersion.Excel2010, true, true, "default-theme");
}


But i get an error,  "Object reference not stablished how object instance" (or similar, my translation isn't good)

Datos contains a IEnumerable <DataRow>

Thanks

Sellappandi Ramu [Syncfusion]
Replied On November 4, 2015 05:57 AM UTC

Hi Manolo, 


Thanks for contacting Syncfusion support.


We considered this “Data table exporting” as an feature and a support incident has been created under your account to track the status of this requirement. Please log on to our support website to check for further updates. 


https://www.syncfusion.com/account/login?ReturnUrl=/support/directtrac/incidents 


As of now, we converted the data table to list before the exporting functionality as a workaround.


Please refer to the following example code and sample,

public class HomeController : Controller

    {

        DataTable _dTable = new DataTable();

        List<Product> data = new List<Product>();

        public ActionResult Index()

        {

            GetGridDT();

            ViewBag.dataTable = _dTable;

            return View();

        }


        public void ExportToExcel(string GridModel)

        {

            ExcelExport exp = new ExcelExport();

            GetGridData(_dTable);

            var DataSource = data;

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, DataSource, "Export.xlsx"ExcelVersion.Excel2010, falsefalse"flat-saffron");

        }

        public void ExportToWord(string GridModel)

        {

            WordExport exp = new WordExport();

            GetGridData(_dTable);

            var DataSource = data;

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, DataSource, "Export.docx"falsefalse"flat-saffron");

        }

        public void ExportToPdf(string GridModel)

        {

            PdfExport exp = new PdfExport();

            GetGridData(_dTable);

            var DataSource = data;

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, DataSource, "Export.pdf"falsefalse"flat-saffron");

        }

        private GridProperties ConvertGridObject(string gridProperty)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));

            GridProperties gridProp = new GridProperties();

            foreach (KeyValuePair<stringobject> ds in div)

            {

                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public |BindingFlags.IgnoreCase);

                if (property != null)

                {

                    Type type = property.PropertyType;

                    string serialize = serializer.Serialize(ds.Value);

                    object value = serializer.Deserialize(serialize, type);

                    property.SetValue(gridProp, value, null);

                }

            }

            return gridProp;

        }


        public void GetGridDT()

        {

            var connection = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString;

            using (var dataAdapter = new SqlDataAdapter("SELECT * from Products", connection))

            {

                var dataTable = new DataTable();


                dataAdapter.Fill(_dTable);


            }

        }

        public void GetGridData(DataTable dt)

        {

            GetGridDT();

            foreach (DataRow row in dt.Rows)

            {

                Product item = GetItem<Employee>(row);

                data.Add(item);

            }

        }

        private static Product GetItem<Employee>(DataRow dr)

        {

            Type temp = typeof(Product);

            Product obj = new Product();

            foreach (DataColumn column in dr.Table.Columns)

            {

                foreach (PropertyInfo pro in temp.GetProperties())

                {

                    if (pro.Name == column.ColumnName)

                        pro.SetValue(obj, dr[column.ColumnName], null);

                    else

                        continue;

                }

            }

            return obj;
        }


Sample: http://www.syncfusion.com/downloads/support/forum/121022/ze/Sample_121022-1065372406


Regards, 

Sellappandi R


Manolo
Replied On November 4, 2015 08:52 AM UTC

Ok, Thank you

Sellappandi Ramu [Syncfusion]
Replied On November 5, 2015 05:18 AM UTC

Hi Manolo, 

Thanks for the update.

Please get back to us if you need any further assistance.  

Regards, 
Sellappandi R


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;