locked
Export Data from Lightswitch Web App to Excel/Word RRS feed

  • Question

  • Hi everyone.

    I have a question about export from LS to Excel/Word, I did some things but unfortunately doesnt works on my Deploy Site in Azure, i read this blog:

    How to use previously Out-of-Browser-only features in LightSwitch for In-Browser Apps

    Recently I found a way to export in csv format, but i need to format the document in cells like a normal Excel document, this is what i did:

    partial void Export_Execute()
            {
                this.FindControl("Exportar").ControlAvailable += exportToExcel_ControlAvailable;
            }
    
            private string GetCSV()
            {
                StringBuilder csv = new StringBuilder();
                csv.AppendFormat("No. Orden \t Fecha \t Empresa \t Tarea" + "" + System.Environment.NewLine);
    
                foreach (var c in this.Table)
                {
                    csv.AppendFormat(c.Orden + "," +
                        c.Fecha + "," +
                        c.Empresa + "," +
                        c.Tarea + "," +
                        System.Environment.NewLine
                        , c);
                }
                return csv.ToString(0, csv.Length - 1);
            }
    
            private void exportToExcel_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                this.FindControl("Exportar").ControlAvailable -= exportToExcel_ControlAvailable;
                var Button = (Button)e.Control;
                Button.Click += ExportarButton_Click;
            }
    
            private void ExportarButton_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                string csv = GetCSV();
                var dialog = new SaveFileDialog();
                dialog.Filter = "CSV (*.csv)|*.csv";
                if (dialog.ShowDialog() == true)
                {
                    using (StreamWriter stream = new StreamWriter(dialog.OpenFile()))
                    {
                        stream.Write(csv, Encoding.UTF8);
                        stream.Close();
                        this.ShowMessageBox("El archivo ha sido guardado satisfactoriamente", "Mensaje", MessageBoxOption.Ok);
                    }
                }
            }

    But dont have any format:

    I need some help from you, would appreciate any comment, have a good day.


    Monday, January 13, 2014 7:45 PM

Answers

All replies