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?

djanosik commented 6 years ago

@direstrepo24 The Excel export package is not available in the trial version. I am sorry about that. Please allow me some time to see whether we can make it available for you. I will let you know soon.

djanosik commented 6 years ago

@direstrepo24 You should be able to install the DotVVM.BusinessPack.Export.Excel package including the GridViewExportExcel class. Please let me know how it works for you.

direstrepo24 commented 6 years ago

Hi, get this warning: 'DotVVM.BusinessPack.Export.Excel 1.1.5-rc1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

djanosik commented 6 years ago

Excel export depends on System.Drawing and requires full .NET Framework running on Windows. Your application should target net451 or higher. It may also work on Mono, but it is not tested.

For now, it doesn't work in xplat .NET Core applications. We will definitely fix that in future releases.

direstrepo24 commented 6 years ago

thanks, us working new project with asp.net core and i need implement with this library. Is there any solution for now, otherwise to export to excel?

djanosik commented 6 years ago

I am afraid I can't help you right now. We are going to make the Excel export xplat when the EPPlus or any similar library is compatible with System.Drawing.Common.

direstrepo24 commented 6 years ago

ok, thanks.

djanosik commented 6 years ago

@direstrepo24 Can you use CSV export instead of Excel export?

tomasherceg commented 6 years ago

@djanosik What about CoreCompat.System.Drawing?

djanosik commented 6 years ago

@tomasherceg I would rather not take a dependency on this library. Official cross-platform System.Drawing is already available on NuGet.

direstrepo24 commented 6 years ago

the idea is generate a report in excel. I can place in a button event of dotvvm call a function of for example Epplus or another library to export to excel? Could you give me a simple example? This is important because all applications and businesses are based on reports. thanks

djanosik commented 6 years ago

I am not sure I understand. You can generate the Excel file yourself with any library you want. Or you can use our CSV export for now. CSV files can also be opened in Excel.

direstrepo24 commented 6 years ago

excuseme, what is your .cvs export, any example?

djanosik commented 6 years ago

It works the same way as GridViewExportExcel. You just need to use GridViewExportCsv class from the DotVVM.BusinessPack.Export.Csv package.

var exporter = new GridViewExportCsv<Customer>(new GridViewExportCsvSettings<Customer> { Separator = ";" });
var gridView = Context.View.FindControlByClientId<DotVVM.BusinessPack.Controls.GridView>("data", true);

using (var file = exporter.Export(gridView, Customers))
{
    Context.ReturnFile(file, "Report.csv", "text/csv");
}
direstrepo24 commented 6 years ago

Hi, have this exception: an exception of type 'DotVVM.Framework.Controls.DotvvmControlException' occurred in DotVVM.Framework.dll but was not handled in user code: 'The control with ID 'data' was found, however it is not an instance of the desired type 'DotVVM.BusinessPack.Controls.GridView'.' at DotVVM.Framework.Controls.DotvvmControl.FindControlByClientId[T](String id, Boolean throwIfNotFound), my page is: <dot:GridView ID="data" DataSource="{value: Customers}" InlineEditing="true" > and button event is: <dot:Button Text="Export" Click="{command: Export()}" />

djanosik commented 6 years ago

You need to use <bp:GridView ...> in markup code instead of <dot:GridView ...>.

direstrepo24 commented 6 years ago

Excuseme, i follow this example https://www.dotvvm.com/docs/controls/businesspack/GridView/latest , in the public BusinessPackDataSet Customers { get; set; } but "BusinessPackDataSet" does not appear in the namespace.

djanosik commented 6 years ago

Yes, the docs are written for newer version than you have. Try to use BpGridViewDataSet.

direstrepo24 commented 6 years ago

Excuseme, last question, i have excepction to load data: 'System.NullReferenceException' occurred in DotVVM.BusinessPack.Export.dll but was not handled in user code: 'Object reference not set to an instance of an object.' this ocurred in line --->: ` public void Export() { var exporter = new GridViewExportCsv(new GridViewExportCsvSettings { Separator = ";" }); var gridView = Context.View.FindControlByClientId("data", true);

(exception) ---> using (var file = exporter.Export(gridView, Customers)) { Context.ReturnFile(file, "Report.csv", "text/csv"); } }`

Thanks for you helpme.

djanosik commented 6 years ago

Show me the stack trace. Where is the exception thrown?

direstrepo24 commented 6 years ago

in line: using (var file = exporter.Export(gridView, Customers))

djanosik commented 6 years ago

I don't think it's possible. Show me the stack trace.

direstrepo24 commented 6 years ago

excepción: CLR/System.NullReferenceException An exception of type 'System.NullReferenceException' occurred in DotVVM.BusinessPack.Export.dll but was not handled in user code: 'Object reference not set to an instance of an object.' at DotVVM.BusinessPack.Export.GridViewExportBase1.GetOrderedGridViewUserSettings(GridView gridView) at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.Export(GridView gridView, GridViewDataSet`1 dataSet) at electroweb.ViewModels.ViewModelCustomer.Export() in /Users/torrefuertesas/dev/electro/electroweb/ViewModels/ViewModelCustomer.cs:line 75 at DotVVM.Framework.Binding.Expressions.CommandBindingExpression.Evaluate(DotvvmBindableObject control, DotvvmProperty property, Object[] args) at DotVVM.Framework.Runtime.Commands.CommandResolver.<>cDisplayClass10.b0() at DotVVM.Framework.Hosting.DotvvmPresenter.d28.MoveNext() PROBLEMAS SALIDA CONSOLA DE DEPURACIÓN TERMINAL Request starting HTTP/1.1 GET http://localhost:5000/dotvvmResource/globalize---/globalize---
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 96.372ms 200 text/javascript info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:5000/
dotvvm-spa___/Elementos application/json 2696 Step into: Stepping over non-user code 'electroweb.ViewModels.Customer..ctor' Exception thrown: 'System.NullReferenceException' in DotVVM.BusinessPack.Export.dll

djanosik commented 6 years ago

Can you please format the stack trace to make it readable? Or just post a screenshot?

direstrepo24 commented 6 years ago

I use Spa page, and button "Export": " at DotVVM.BusinessPack.Export.GridViewExportBase1.GetOrderedGridViewUserSettings(GridView gridView)\n at DotVVM.BusinessPack.Export.Csv.GridViewExportCsv1.Export(GridView gridView, GridViewDataSet1 dataSet)\n at electroweb.ViewModels.ViewModelCustomer.Export() in /Users/torrefuertesas/dev/electro/electroweb/ViewModels/ViewModelCustomer.cs:line 75\n at lambda_method(Closure )\n at DotVVM.Framework.Binding.Expressions.CommandBindingExpression.Evaluate(DotvvmBindableObject control, DotvvmProperty property, Object[] args)\n at DotVVM.Framework.Runtime.Commands.CommandResolver.<>c__DisplayClass1_0.b0()\n at DotVVM.Framework.Hosting.DotvvmPresenter.d28.MoveNext()"`

image

djanosik commented 6 years ago

Try to set the UserSettings property on the GridView to a GridViewUserSettings instance you create in your ViewModel.

direstrepo24 commented 6 years ago

Excuse me, do not you understand can you give me some code?

djanosik commented 6 years ago

It's explained in docs: https://www.dotvvm.com/docs/controls/businesspack/GridView/latest

direstrepo24 commented 6 years ago

image

djanosik commented 6 years ago

The property is called UserSettings. Take a look at docs please.

direstrepo24 commented 6 years ago

Hi, i have this error, set Usersetting, but : image

I used spa page,

djanosik commented 6 years ago

Ok, see this issue: https://github.com/riganti/dotvvm/issues/426

direstrepo24 commented 6 years ago

i see this: image

djanosik commented 6 years ago

This is probably unrelated to DotVVM or this issue. How does the stack trace look?

direstrepo24 commented 6 years ago

image and : image

djanosik commented 6 years ago

Hmm, I need stack trace of the "Timeouts are not supported on this stream." exception.

direstrepo24 commented 6 years ago

image

djanosik commented 6 years ago

I can't read the stack trace. But if this is where the exception is thrown, it's not a problem at all. You are accessing unsupported properties in debugger. Or is it also thrown at runtime? Please, try to make it easier for me to help you.

direstrepo24 commented 6 years ago

hi, thanks for you help, this in time execution, when click in Export, go to the viewModel this : image

djanosik commented 6 years ago

I've already told you the DotvvmInterrupRequestExecutionException should be thrown. It's not an issue. Are you able to download the returned file or not?

direstrepo24 commented 6 years ago

no, it goes to the exception:

image

djanosik commented 6 years ago

I know and it's the correct behavior. Please, turn off the debugger and try to download the file.

direstrepo24 commented 6 years ago

Ready, it ok. Thanks

djanosik commented 6 years ago

Use dotnet run from command line. Or just skip the exception and continue.

CharanSaradaSSD commented 3 years ago

This seems to be outdated Can you provide documentation or sample code that is updated?

mrnustik commented 3 years ago

@CharanSaradaSSD Hi, which version of Bussiness Pack are you using? I will provide you with the sample code for the version. We are currently working on better documentation for the export but it is not ready yet.

CharanSaradaSSD commented 3 years ago

@CharanSaradaSSD Hi, which version of Bussiness Pack are you using? I will provide you with the sample code for the version. We are currently working on better documentation for the export but it is not ready yet.

2.5.3

CharanSaradaSSD commented 3 years ago

2.5.3 This is the version I am working on

mrnustik 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");
         }
     }
}
mrnustik commented 3 years ago

Just a note: In version 3.0.0 we have managed to make the Excel export much better. It provides support for all of Excel data types and extensibility points.

Right now the 3.0.0 version is in preview, but the Excel export should be stable. We are currently working on the documentation for it so it should be available soon.