Answered Using server code to call Gembox spreadsheet library

  • Tuesday, August 28, 2012 2:35 PM
     
     

    Hi

    I have worked out how to export data to CSV from LS but I would rather be able to create Excel files directly. I have looked at the Gembox DLL and wondered if that could be used?

    I can't use a .NET library on the client side but I can use it at the server side.

    Can anyone let me know if they think it would work using a dummy table insert to trigger a method at the back end to create the file and save it to the client?  Can the server send the file to the client? Or would it only be able to save the file on the server?

    Any tips could really save me time before I get stuck into this.

    Gus

All Replies

  • Wednesday, August 29, 2012 2:06 AM
    Moderator
     
     

    "Can anyone let me know if they think it would work using a dummy table insert to trigger a method at the back end to create the file"

    Anything that can be done using .NET code can be done in LS, using the Command Table to trigger it, yes. If it didn't need to be set in action from the client, it wouldn't need the table workaround, & could be done directly. The command table allows the saving of a dummy record to be intercepted, & therefore trigger any .NET code that you want to use. If the action was part of the actual process associated with an entity (such as sending an email when you save/update a customer, for example), then the command table wouldn't be needed, you woulkd just intercept the saving of the customer record & trigger the email to be sent. You only need the extra table if there's no existing entity involved.

    "and save it to the client"

    I don't know how to answer this, because I'm not sure what you mean.

    "the server send the file to the client? Or would it only be able to save the file on the server?"

    Again, not sure what you mean by "send it to the client". You could save it as a result in the command table record, the client would then have access to that.

    "Any tips could really save me time before I get stuck into this"

    Have I missed anything you need an answer on?


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

  • Wednesday, August 29, 2012 5:31 AM
     
     Answered Has Code

    Gus:

    I may be able to help a bit on this.  I use Aspose.Cells to create spreadsheets on the server it works great.  I would think that Gembox would work as well.

    I use the command table pattern to execute the code on the server, which creates the spreadsheet and saves it to an "Image" datatype field.  After the record is saved, I call a function on the client to open a dialog to save the file.

    The code gets the data from the image field and allows you to save it to a file.  FYI, my application is a desktop (out of browser) app and I am hosting it on Azure.

    This code loads the saved report into a byte array and calls a function to save the file on the client. (In this example the table is "ReportSets", the image field is "ReportContentPDF" and the file type is PDF):

    byte[] pdfBytes = this.DataWorkspace.ApplicationData.ReportSets_Single(rs.Id).ReportContentPDF; ;
    SaveReportFile(pdfBytes);

    (I got this code from someone else on the forum here.  Sorry I can't remember who is was.)

    Here is the code for the function:

            private void SaveReportFile(byte[] PdfBytes)
            {
                // Download file from selected record.
                if (PdfBytes.Length == 0) return;
    
                Dispatchers.Main.BeginInvoke(() =>
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    // string defaultFilter = "Images *.jpg, *.png, *.gif|*.jpg;*.png;*.gif|Documents *.doc, *.pdf|*.doc;*.pdf|All *.*|*.*";
                    string defaultFilter = "PDF *.pdf|*.pdf|All *.*|*.*";
                    //if (!string.IsNullOrEmpty(doc.FileName))
                    {
                        //   string ext = Path.GetExtension(doc.FileName);
                        sfd.DefaultExt = ".PDF";
                        defaultFilter = string.Format("*.{0}|*.{0}|", sfd.DefaultExt) + defaultFilter;
                    }
                    sfd.Filter = defaultFilter;
    
                    bool? selected = sfd.ShowDialog();
                    if (selected.HasValue && selected.Value)
                    {
                        string fileName = sfd.SafeFileName;
                        long len = 0;
                        using (var fs = sfd.OpenFile())
                        {
                            // Write all bytes in field to file..
                            fs.Write(PdfBytes, 0, PdfBytes.Length);
                            len = fs.Length;
                        }
    
                        // this.ShowMessageBox("File Get: " + sfd.SafeFileName + "\nSize:" + len);
    
                        // Could open file if you knew the path, but SafeFileName does not give path.
                        // string path = @"c:\temp\" + fileName;
                        // var shell = AutomationFactory.CreateObject("Shell.Application");
                        // shell.ShellExecute("mspaint.exe", path, "", "open", 1);
                    }
                    else
                        this.ShowMessageBox("Canceled");
                });
            }

    Hope this helps.

    Mark



    • Edited by marks100 Wednesday, August 29, 2012 5:33 AM
    • Edited by marks100 Wednesday, August 29, 2012 5:36 AM
    • Marked As Answer by GusBeare Wednesday, August 29, 2012 7:16 AM
    •  
  • Wednesday, August 29, 2012 5:46 AM
    Moderator
     
     
    Neat trick Mark!

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

  • Wednesday, August 29, 2012 7:12 AM
     
     

    thanks Mark, very good indeed!

    that's definitely a solution I could use.

  • Wednesday, August 29, 2012 7:16 AM
     
     

    thanks Yann

    I think what I was trying to say was if the file is created by the server code then how does the user download it and save it locally?  How do I get it from the server to the client?

    Mark's solution does this by saving it in the table as binary and then going from there.

    Before I try this method I am looking at a Silverlight DLL for creating Excel files which I hope will run on the client.

    http://excellite.codeplex.com/

    thanks

    Gus

  • Wednesday, August 29, 2012 4:44 PM
     
     
    Thanks Yann.