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:

SQL Server: Make all UPPER case to Proper Case/Title Case

CREATE FUNCTION [dbo].[fnConvert_TitleCase] (@InputString VARCHAR(4000) ) RETURNS VARCHAR(4000) AS BEGIN DECLARE @Index INT DECLARE @C...