/// <summary>
        /// Generates the excel sheet asynchronous.
        /// </summary>
        /// <param name="spreadsheetRequest">The spreadsheet request.</param>
        /// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Task.</returns>
        /// <exception cref="Exception"></exception>
        public Task <Stream> GenerateExcelSheetAsync <T>(SpreadsheetRequest <T> spreadsheetRequest, CancellationToken cancellationToken = default)
        {
            Argument.IsNotNull(spreadsheetRequest);

            Stream    result   = new MemoryStream();
            IWorkbook workbook = null;

            var table = spreadsheetRequest.DataSet.ToDataTable(spreadsheetRequest.FileName);

            try
            {
                using var engine = new ExcelEngine();
                workbook         = engine.Excel.Workbooks.Create(1);

                var importDataOptions = new ExcelImportDataOptions
                {
                    FirstRow      = 1,
                    FirstColumn   = 1,
                    IncludeHeader = true,
                    PreserveTypes = false
                };

                workbook.Worksheets[0].ImportDataTable(table, true, 1, 1);
                workbook.Worksheets[0].UsedRange.AutofitColumns();
                workbook.Worksheets[0].UsedRange.WrapText = true;

                workbook.Version = ExcelVersion.Xlsx;
                workbook.SaveAs(result);
            }
            finally
            {
                if (workbook is not null)
                {
                    workbook.Close();
                }

                result.Position = 0;
            }

            return(Task.FromResult(result));
        }
Exemple #2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2016;

            //Open an existing spreadsheet which will be used as a template for generating the new spreadsheet.
            //After opening, the workbook object represents the complete in-memory object model of the template spreadsheet.
            IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
            //The first worksheet object in the worksheets collection is accessed.
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brands> list = GetVehicleDetails();

            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();

            importDataOptions.FirstRow = 4;

            if (LayoutOptions.SelectedIndex == 0)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (LayoutOptions.SelectedIndex == 1)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (LayoutOptions.SelectedIndex == 2)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (checkbox.Checked == true)
            {
                if (rdbExpand.Checked == true)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (rdbCollapse.Checked == true)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (textBox.Text != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(textBox.Text);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet["A1:C1"].CellStyle.Font.Bold = true;
            worksheet.UsedRange.AutofitColumns();

            #endregion

            workbook.Version = ExcelVersion.Excel2016;
            workbook.SaveAs("Sample.xlsx", Response, ExcelDownloadType.PromptDialog, ExcelHttpContentType.Excel2016);
        }
        internal void OnButtonClicked(object sender, EventArgs e)
        {
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2016;
            IWorkbook  workbook  = excelEngine.Excel.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brands> list = GetVehicleDetails();

            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();

            importDataOptions.FirstRow = 4;

            if (this.Layout.SelectedIndex == 0)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (this.Layout.SelectedIndex == 1)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (this.Layout.SelectedIndex == 2)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (Switch1.IsToggled)
            {
                if (ExpandButton.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (CollapseButton.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (Level.Text != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(Level.Text);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = Syncfusion.Drawing.Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = Syncfusion.Drawing.Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet["A1:C1"].CellStyle.Font.Bold = true;
            worksheet.UsedRange.AutofitColumns();

            #endregion

            MemoryStream stream = new MemoryStream();
            workbook.SaveAs(stream);
            workbook.Close();
            excelEngine.Dispose();

            if (Device.RuntimePlatform == Device.UWP)
            {
                Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("ImportNestedCollection.xlsx", "application/msexcel", stream);
            }
            else
            {
                Xamarin.Forms.DependencyService.Get <ISave>().Save("ImportNestedCollection.xlsx", "application/msexcel", stream);
            }
        }
        private void createExcelBtn_Click(object sender, EventArgs e)
        {
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2016;
            IWorkbook  workbook  = excelEngine.Excel.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brand> list = GetVehicleDetails();

            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();

            importDataOptions.FirstRow = 4;

            if (layoutOptions.SelectedIndex == 0)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (layoutOptions.SelectedIndex == 1)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (layoutOptions.SelectedIndex == 2)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (groupChkBox.Checked)
            {
                if (expandRadioBtn.Checked)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (collapseRadioBtn.Checked)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (levelTxtBox.Text != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(levelTxtBox.Text);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);
            string fileName = @"ImportData.xlsx";

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = System.Drawing.Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = System.Drawing.Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet["A1:C1"].CellStyle.Font.Bold = true;
            worksheet.UsedRange.AutofitColumns();

            #endregion

            workbook.SaveAs(fileName);
            workbook.Close();
            excelEngine.Dispose();

            //Message box confirmation to view the created document.
            if (MessageBox.Show("Do you want to view the Excel file?", "File has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                try
                {
#if NETCORE
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(fileName)
                    {
                        UseShellExecute = true
                    };
                    process.Start();
#else
                    Process.Start(fileName);
#endif
                    //Exit
                    this.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Exemple #5
0
        public ActionResult ImportNestedCollection(string LayoutOptions, string button, string checkbox, string rdb1, string textBox)
        {
            if (button == null)
            {
                return(View());
            }

            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2016;

            //Open an existing spreadsheet which will be used as a template for generating the new spreadsheet.
            //After opening, the workbook object represents the complete in-memory object model of the template spreadsheet.
            IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
            //The first worksheet object in the worksheets collection is accessed.
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brands> list = GetVehicleDetails();

            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();

            importDataOptions.FirstRow = 4;

            if (LayoutOptions == "Default")
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (LayoutOptions == "Merge")
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (LayoutOptions == "Repeat")
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (checkbox == "Group")
            {
                if (rdb1 == "Expand")
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (rdb1 == "Collapse")
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (textBox != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(textBox);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet["A1:C1"].CellStyle.Font.Bold = true;
            worksheet.UsedRange.AutofitColumns();

            #endregion

            try
            {
                MemoryStream ms = new MemoryStream();
                workbook.SaveAs(ms);
                ms.Position = 0;
                return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ImportData.xlsx"));
            }
            catch (Exception)
            {
            }

            workbook.Close();
            excelEngine.Dispose();

            return(View());
        }
        void OnButtonClicked(object sender, EventArgs e)
        {
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2013;
            IWorkbook  workbook  = application.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brands> list = GetVehicleDetails();

            int index = layoutList.IndexOf(selectedLayout);
            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();

            importDataOptions.FirstRow = 4;

            if (index == 0)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (index == 1)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (index == 2)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (groupSwitch.Selected)
            {
                if (expandButton.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (collapseButton.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (textField.Text != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(textField.Text);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet["A1:C1"].CellStyle.Font.Bold = true;
            worksheet.UsedRange.AutofitColumns();

            #endregion

            MemoryStream stream = new MemoryStream();
            workbook.SaveAs(stream);
            workbook.Close();
            excelEngine.Dispose();

            if (stream != null)
            {
                SaveiOS iOSSave = new SaveiOS();
                iOSSave.Save("ImportData.xlsx", "application/msexcel", stream);
            }
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ExcelEngine  excelEngine = new ExcelEngine();
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2016;
                IWorkbook  workbook  = excelEngine.Excel.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];

                string        fileName = string.Empty;
                IList <Brand> list     = GetVehicleDetails();

                ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();
                importDataOptions.FirstRow = 4;

                if (cmbLayout.SelectedIndex == 0)
                {
                    importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
                }
                else if (cmbLayout.SelectedIndex == 1)
                {
                    importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
                }
                else if (cmbLayout.SelectedIndex == 2)
                {
                    importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
                }

                if (checkBox.IsChecked.Value)
                {
                    if (rdbExpand.IsChecked.Value)
                    {
                        importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                    }
                    else if (rdbCollapse.IsChecked.Value)
                    {
                        importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                        if (tbLevel.Text != string.Empty)
                        {
                            importDataOptions.CollapseLevel = int.Parse(tbLevel.Text);
                        }
                    }
                }

                worksheet.ImportData(list, importDataOptions);
                fileName = @"ImportData.xlsx";

                #region Define Styles
                IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
                IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

                pageHeader.Font.FontName       = "Calibri";
                pageHeader.Font.Size           = 16;
                pageHeader.Font.Bold           = true;
                pageHeader.Color               = System.Drawing.Color.FromArgb(0, 146, 208, 80);
                pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
                pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

                tableHeader.Font.Bold     = true;
                tableHeader.Font.FontName = "Calibri";
                tableHeader.Color         = System.Drawing.Color.FromArgb(0, 146, 208, 80);

                #endregion

                #region Apply Styles
                // Apply style for header
                worksheet["A1:C2"].Merge();
                worksheet["A1"].Text      = "Automobile Brands in the US";
                worksheet["A1"].CellStyle = pageHeader;

                worksheet["A4:C4"].CellStyle = tableHeader;

                worksheet["A1:C1"].CellStyle.Font.Bold = true;
                worksheet.UsedRange.AutofitColumns();

                #endregion

                workbook.SaveAs(fileName);
                workbook.Close();
                excelEngine.Dispose();

                //Message box confirmation to view the created spreadsheet.
                if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
                                    MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                {
                    try
                    {
                        //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
#if NETCORE
                        System.Diagnostics.Process process = new System.Diagnostics.Process();
                        process.StartInfo = new System.Diagnostics.ProcessStartInfo(fileName)
                        {
                            UseShellExecute = true
                        };
                        process.Start();
#else
                        Process.Start(fileName);
#endif

                        //Exit
                        this.Close();
                    }
                    catch (Win32Exception ex)
                    {
                        MessageBox.Show("Excel 2007 is not installed in this system");
                        Console.WriteLine(ex.ToString());
                    }
                }
                //else
                // Exit
                //this.Close();
            }
            catch
            {
                MessageBox.Show("Sorry, Excel can't open two workbooks with the same name at the same time.\nPlease close the workbook and try again.", "File is already open", MessageBoxButton.OK);
            }
        }
        private async void btnGenerateExcel_Click(object sender, RoutedEventArgs e)
        {
            #region Workbook initialization
            //New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open].
            //The instantiation process consists of two steps.

            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2013;

            IWorkbook workbook = application.Workbooks.Create(1);
            //The first worksheet object in the worksheets collection is accessed.
            IWorksheet worksheet = workbook.Worksheets[0];

            IList <Brands> list = GetVehicleDetails();

            ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();
            importDataOptions.FirstRow = 4;

            if (comboBox1.SelectedIndex == 0)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
            }

            if (checkbox1.IsChecked.Value)
            {
                if (rdbExpand.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
                }
                else if (rdbCollapse.IsChecked.Value)
                {
                    importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
                    if (levelTextBox.Text != string.Empty)
                    {
                        importDataOptions.CollapseLevel = int.Parse(levelTextBox.Text);
                    }
                }
            }

            worksheet.ImportData(list, importDataOptions);

            #region Define Styles
            IStyle pageHeader  = workbook.Styles.Add("PageHeaderStyle");
            IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");

            pageHeader.Font.FontName       = "Calibri";
            pageHeader.Font.Size           = 16;
            pageHeader.Font.Bold           = true;
            pageHeader.Color               = Windows.UI.Color.FromArgb(0, 146, 208, 80);
            pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
            pageHeader.VerticalAlignment   = ExcelVAlign.VAlignCenter;

            tableHeader.Font.Bold     = true;
            tableHeader.Font.FontName = "Calibri";
            tableHeader.Color         = Windows.UI.Color.FromArgb(0, 146, 208, 80);

            #endregion

            #region Apply Styles
            // Apply style for header
            worksheet["A1:C2"].Merge();
            worksheet["A1"].Text      = "Automobile Brands in the US";
            worksheet["A1"].CellStyle = pageHeader;

            worksheet["A4:C4"].CellStyle = tableHeader;

            worksheet.Columns[0].ColumnWidth = 10;
            worksheet.Columns[1].ColumnWidth = 20;
            worksheet.Columns[2].ColumnWidth = 25;

            #endregion

            #endregion

            #region Save the Workbook
            StorageFile storageFile;
            if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
                savePicker.SuggestedFileName      = "ImportData";
                savePicker.FileTypeChoices.Add("Excel Files", new List <string>()
                {
                    ".xlsx",
                });
                storageFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                storageFile = await local.CreateFileAsync("ImportData.xlsx", CreationCollisionOption.ReplaceExisting);
            }

            if (storageFile != null)
            {
                //Saving the workbook
                await workbook.SaveAsAsync(storageFile);

                workbook.Close();
                excelEngine.Dispose();

                MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File has been saved successfully.");

                UICommand yesCmd = new UICommand("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new UICommand("No");
                msgDialog.Commands.Add(noCmd);
                IUICommand cmd = await msgDialog.ShowAsync();

                if (cmd == yesCmd)
                {
                    // Launch the saved file
                    bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile);
                }
            }
            else
            {
                workbook.Close();
                excelEngine.Dispose();
            }
            #endregion
        }