Example #1
0
        private void download_as_xls()
        {
            string  dataset_id = hdnDatasetId.Value;
            DataSet ds         = getDataSetExportToExcel(dataset_id);

            string dataset_name = objDataClass.DataSetName(dataset_id) + ".xlsx";

            //exportToExcel(ds);

            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(ds.Tables[0], "Dataset description");
                wb.Worksheets.Add(ds.Tables[1], "Data");
                //foreach (System.Data.DataTable dt in ds.Tables)
                //{
                //    wb.Worksheets.Add(dt, "");
                //    wb.Worksheets.Name = “NewTabName”;
                //}

                //Export the Excel file.
                Response.Clear();
                Response.Buffer      = true;
                Response.Charset     = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=" + dataset_name + "");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
        }