riganti / dotvvm

Open source MVVM framework for Web Apps
https://www.dotvvm.com
Apache License 2.0
743 stars 97 forks source link

GridViewExportExcel how export excel? #502

Closed direstrepo24 closed 6 years ago

direstrepo24 commented 6 years ago

How i can export to excel?, as in the example of "Bussines Editor" in https://themes.dotvvm.com/editor, i tried:

var exporter = new  GridViewExportExcel<Customer>(new GridViewExportExcelSettings<Customer>());
            var gridView = Context.View.FindControlByClientId<DotVVM.BusinessPack.Controls.GridView>("data", true);

            using (var file = exporter.Export(gridView, Customers))
            {
                Context.ReturnFile(file, "excel.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            }

but I can not find the space name "GridViewExportExcel", i have Bussinespack trial version. help me please?

CharanSaradaSSD commented 3 years ago

@CharanSaradaSSD This code should do the trick. Just put it inside of your command and it will generate the Excel file and return it to the user.

public class GridViewExportViewModel : DotvvmViewModelBase
{
    private readonly BindingCompilationService bindingService;

    public BusinessPackDataSet<CustomerData> CustomersDataSet { get; set; } = new BusinessPackDataSet<CustomerData>() { PagingOptions = { PageSize = 5 } };

    public GridViewExportViewModel(BindingCompilationService bindingService)
    {
         this.bindingService = bindingService;
    }

    public void Export()
    {
         var gridView = Context.View.FindControlByClientId<BusinessPack.Controls.GridView>("CustomersGridView", true);
         var gridViewExportExcel = new GridViewExportExcel<CustomerData>(bindingService);
         using (var exportStream = gridViewExportExcel.Export(gridView, customersDataSet))
         {
                  Context.ReturnFile(exportStream, "export.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
         }
     }
}

I am running in to the error while return file is happening. Please help on this.

DotVVM.Framework.Hosting.DotvvmInterruptRequestExecutionException: 'Request interrupted: ReturnFile (export.xlsx)

Stack Trace:

at DotvvmRequestContextExtensions.ReturnFile(IDotvvmRequestContext context, Stream stream, String fileName, String mimeType, IEnumerable1 additionalHeaders, String attachmentDispositionType) at GridTest.ViewModels.GridViewTestViewModel.Export() in C:\00Source\GridTest\ViewModels\GridViewTestViewModel.cs:line 75 at DotVVM.Framework.Binding.BindingHelper.Evaluate(ICommandBinding binding, DotvvmBindableObject control, Func2[] args) at DotVVM.Framework.Runtime.Commands.CommandResolver.<>c__DisplayClass1_0.b0() at DotVVM.Framework.Hosting.DotvvmPresenter.d29.MoveNext()

mrnustik commented 3 years ago

@CharanSaradaSSD This is not an error. As described in the conversation above this is an internal exception of the DotVVM framework which is used for interrupting the current request and returning the file from it. Just skip it and your exported excel file should download after.

CharanSaradaSSD commented 3 years ago

@CharanSaradaSSD This is not an error. As described in the conversation above this is an internal exception of the DotVVM framework which is used for interrupting the current request and returning the file from it. Just skip it and your exported excel file should download after.

Thanks @mrnustik it works Do you have a similar way by any chance for CSV, word and Pdf.

For csv I used public void ExportCSV() { var gridView = Context.View.FindControlByClientId("CustomersGridView", true); var gridViewExport = new GridViewExportCsv(bindingService, new GridViewExportCsvSettings { Separator = ";" }); using (var exportStream = gridViewExport.Export(gridView, Customers)) { Context.ReturnFile(exportStream, "export.csv", "text/csv"); } } I am ending up with exception at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.<PrepareLineData>b__6_0(String d) at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.String.Join(String separator, IEnumerable1 values) at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.CreateLine(IEnumerable1 lineData) at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.CreateHeader(GridView gridView, List1 getGridViewColumnSettings) at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.Export(GridView gridView, IBusinessPackDataSet`1 dataSet) at GridTest.ViewModels.GridViewTestViewModel.ExportCSV() in C:\00Source\GridTest\ViewModels\GridViewTestViewModel.cs:line 79

Thanks for your Prompt reply...

mrnustik commented 3 years ago

@CharanSaradaSSD Can you please also provide me with the message and type of the thrown Exception? I will look into it.

CharanSaradaSSD commented 3 years ago

@CharanSaradaSSD Can you please also provide me with the message and type of the thrown Exception? I will look into it.

System.NullReferenceExceptionObject reference not set to an instance of an object.

At

var exportStream = gridViewExport.Export(gridView, Customers)

CharanSaradaSSD commented 3 years ago

@CharanSaradaSSD This is not an error. As described in the conversation above this is an internal exception of the DotVVM framework which is used for interrupting the current request and returning the file from it. Just skip it and your exported excel file should download after.

We found our own ways to get the grid data but this exception at return file from context is not allowing us to do another operation with an error "Postback interrupted". Please let us know if have an option to come out of it. image

AjayaKalakheti commented 2 years ago

@CharanSaradaSSD This is not an error. As described in the conversation above this is an internal exception of the DotVVM framework which is used for interrupting the current request and returning the file from it. Just skip it and your exported excel file should download after.

We found our own ways to get the grid data but this exception at return file from context is not allowing us to do another operation with an error "Postback interrupted". Please let us know if have an option to come out of it. image

Hi @CharanSaradaSSD, Did you get the answer for your issue?