lunes, 8 de mayo de 2017

Create ODS spreadsheets C# Usin AODL + Query PostgreSQL

Library

using System;
using System.Data;  
using AODL;
using AODL.Document.SpreadsheetDocuments;
using AODL.Document.Content;
using AODL.Document.Content.Tables;
using AODL.Document;
using AODL.Package;
using AODL.Document.Collections;



string query_sql = "SELECT id_producto, description FROM products";


string[,] args_names_field = {
            {"Item", "item","1.5","",""},
            {"SKU","string","2.6","
id_producto",""},
            {"Producto","string","12","
description",""},
};
     new seshat.class_traslate_ods(query_sql+query_sql_grupo+query_sql_grupo1+query_sql_grupo2+ordern_lista,args_names_field,"CATALOGO DE PRODUCTOS");
 


          public class_traslate_ods (string query_sql,string[,] args_colums,string titulo_reporte)
        {
            connectionString = conexion_a_DB._url_servidor+conexion_a_DB._port_DB+conexion_a_DB._usuario_DB+conexion_a_DB._passwrd_user_DB;
            nombrebd = conexion_a_DB._nombrebd;
            int secuencia_req = 0;
            int files_field = 0;
                                  
            //Create new spreadsheet open document (.ods)
            AODL.Document.SpreadsheetDocuments.SpreadsheetDocument spreadsheetDocument = new AODL.Document.SpreadsheetDocuments.SpreadsheetDocument();
            spreadsheetDocument.New();
            AODL.Document.Content.Tables.Table table = new AODL.Document.Content.Tables.Table(spreadsheetDocument, "hoja1", "tablefirst");
            NpgsqlConnection conexion;
            conexion = new NpgsqlConnection (connectionString+nombrebd);
            // Verifica que la base de datos este conectada
            try{
                // creando el titulo del reporte
                string text;
                AODL.Document.Content.Tables.Cell cell;
                cell = table.CreateCell ();
                //cell.OfficeValueType ="float";
                AODL.Document.Content.Text.Paragraph paragraph;

                paragraph = new AODL.Document.Content.Text.Paragraph (spreadsheetDocument);
                text = titulo_reporte;          
                paragraph.TextContent.Add (new AODL.Document.Content.Text.SimpleText (spreadsheetDocument, text));
                cell.Content.Add (paragraph);
                cell.OfficeValueType = "string";
                cell.OfficeValue = text;
                //cell.StyleName = cell.CellStyle.CellProperties.
                table.InsertCellAt (files_field, 0, cell);
                files_field++;

                cell = table.CreateCell ();
                //cell.OfficeValueType ="float";
                paragraph = new AODL.Document.Content.Text.Paragraph (spreadsheetDocument);
                text = "";          
                paragraph.TextContent.Add (new AODL.Document.Content.Text.SimpleText (spreadsheetDocument, text));
                cell.Content.Add (paragraph);
                cell.OfficeValueType = "string";
                cell.OfficeValue = text;
                //cell.StyleName = cell.CellStyle.CellProperties.
                table.InsertCellAt (files_field, 0, cell);
                files_field++;

                // Creando los nombres de ancabezado de los campos  
                for (int colum_field = 0; colum_field <= args_colums.GetUpperBound (0); colum_field++) {
                    cell = table.CreateCell ();
                    //cell.OfficeValueType ="float";
                    paragraph = new AODL.Document.Content.Text.Paragraph(spreadsheetDocument);
                    //AODL.Document.Content.Text.FormatedText formText;
                    //formText.TextStyle.TextProperties.Bold();
                    text = args_colums[ colum_field, 0 ];          
                    paragraph.TextContent.Add(new AODL.Document.Content.Text.SimpleText(spreadsheetDocument,text));
                    cell.Content.Add(paragraph);
                    cell.OfficeValueType = "string";                          
                    cell.OfficeValue = text;
                    //cell.StyleName = cell.CellStyle.CellProperties.
                    table.InsertCellAt (files_field, colum_field, cell);
                    //table.ColumnCollection[colum_field].ColumnStyle.StyleName = "bold";

                    // ancho de la columnas
                    if (args_colums [colum_field, 2] != "") {
                        table.ColumnCollection[colum_field].ColumnStyle.ColumnProperties.Width = args_colums [colum_field, 2]+"cm";
                    }
                }
                files_field++;

                conexion.Open ();
                NpgsqlCommand comando;
                comando = conexion.CreateCommand ();
                comando.CommandText = query_sql;
                //Console.WriteLine(comando.CommandText);
                comando.ExecuteNonQuery();    comando.Dispose();
                NpgsqlDataReader lector = comando.ExecuteReader ();
                while (lector.Read()){
                    secuencia_req ++;
                    for (int colum_field = 0; colum_field <= args_colums.GetUpperBound (0); colum_field++) {
                        cell = table.CreateCell ();
                        //cell.OfficeValueType ="float";
                        paragraph = new AODL.Document.Content.Text.Paragraph(spreadsheetDocument);
                        //AODL.Document.Content.Text.FormatedText formText;
                        //formText.TextStyle.TextProperties.Bold();
                        if(args_colums[ colum_field, 3 ] != ""){
                            text = lector[(string) args_colums[ colum_field, 3 ]].ToString();
                            cell.OfficeValueType = args_colums[ colum_field, 1 ];
                        }else{                          
                            if (args_colums [colum_field, 1] == "item") {
                                text = secuencia_req.ToString().Trim();
                                cell.OfficeValueType = "float";
                            }else{
                                text = "";
                            }
                        }
                        //text = lector[(string) args_names_field[ colum_field ]].ToString().Trim();
                        paragraph.TextContent.Add(new AODL.Document.Content.Text.SimpleText(spreadsheetDocument,text));
                        cell.Content.Add(paragraph);                                                  
                        cell.OfficeValue = text;
                        //cell.StyleName = cell.CellStyle.CellProperties.
                        table.InsertCellAt (files_field, colum_field, cell);
                        //table.ColumnCollection[colum_field].ColumnStyle.StyleName = "bold";
                    }
                    files_field++;
                }
                // activando las formulas
                for (int colum_field = 0; colum_field <= args_colums.GetUpperBound (0); colum_field++) {
                    if (args_colums [colum_field, 4] != "") {
                        cell = table.CreateCell ();
                        paragraph = new AODL.Document.Content.Text.Paragraph(spreadsheetDocument);              
                        cell.Content.Add(paragraph);
                        cell.OfficeValueType = args_colums[ colum_field, 1 ];
                        //   ---cell1.OfficeValue = text;
                        cell.Formula = args_colums[ colum_field, 4 ]+files_field+")";
                        table.InsertCellAt (files_field, colum_field, cell);
                        //Console.WriteLine("{0}, {1}", args_formulas[i,0], args_formulas[i,1]);  
                    }
                }
                conexion.Close();              
                //Insert table into the spreadsheet document
                spreadsheetDocument.TableCollection.Add(table);
                string ods_name = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".ods";
                spreadsheetDocument.SaveTo(ods_name);
                try{
                    // open the document automatic
                    System.Diagnostics.Process.Start(ods_name);
                }catch(Exception ex){
                    MessageDialog msgBoxError = new MessageDialog (MyWinError,DialogFlags.DestroyWithParent,
                                            MessageType.Error,
                                            ButtonsType.Close,"Open error file: {0}",ex.Message);
                    msgBoxError.Run ();        msgBoxError.Destroy();
                }
            }catch(NpgsqlException ex){
                    MessageDialog msgBoxError = new MessageDialog (MyWinError,DialogFlags.DestroyWithParent,
                                    MessageType.Error,
                                    ButtonsType.Close,"PostgresSQL error: {0}",ex.Message);
                    msgBoxError.Run ();        msgBoxError.Destroy();
            }
        }      
    }

No hay comentarios: