Monday, 26 June 2017

Export DataSet To Excel in MVC


NameSpace :
using System.Web.Hosting;
using ClosedXML.Excel;


Code  :

[MTAThread]
        private void ExportDataSetToExcel(DataSet ds, String excelName)
        {

            string path = HostingEnvironment.MapPath("~/FolderName/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = path + excelName;
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(ds.Tables[0]);
                wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                wb.Style.Font.Bold = true;
                wb.SaveAs(path, false);
            }
        }

Example:


DataSet objds = new Dataset();
ExportDataSetToExcel(objds, "FileName.xlsx");

1 comment:

Search text in stored procedure in SQL Server

SELECT *   FROM   sys.procedures WHERE  Object_definition(object_id) LIKE '%keyword%'