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();
            }
        }      
    }

jueves, 2 de marzo de 2017

Creacion de un TreeView GTK# (Create treeview GTK# C-Sharp)

Buen Dia Amigos

Aca del dejo como crear un TreeView en GKT#, bajo monodevelop 5.10 Linux aunque tambien funciona para Xamarin Studio sin problemas

Librerias a Usar

using System;
using Gtk;
using Gdk;
using Glade;
using Npgsql;
using System.Collections;
using System.Data;
using System.Collections.Generic;







Llamada del scrip

Explicacion del arreglo

{ "Cant.", "text","","","","","true"},

0 titulo
1 tipo de la columna
2 ancho de la columna
3 R   Color
4 G   Color
5 B   Color
6 true visualiza columna   false oculta columna


void crea_treeview()
        {
           
object[] parametros = { treeview_concepto_remis,treeViewEngineconcepremis};
            string[,] coltreeview = {
                { "Cant.", "text","","","","","true"},
                { "Ref.", "text","","","","","true" },
                { "Descripcion", "text","400","","","","true" },
                { "Pre. Unitario", "text","","","","","true" },
                { "Total", "text","","","","","true" },
                { "Lote", "text","100","","","","true" },
                { "Caducidad", "text","80","","","","true" },
                { "Cod. Barra", "text","","","","","true" },
                { "Ref.", "text","","","","","true" },
                { "ID Producto", "text","","","","","true" },
                { "Nro.remision", "text","","","","","false" }
            };
            crea_colums_treeview (parametros, coltreeview,"treeview_concepto_remis");

}


Como se crea automaticamente las columnas del TreeView

void crea_colums_treeview(object[] args,string [,] args_colums,string nombre_treeview_)
        {
            //var columns_renderertext = new List();
            //var columns_renderertoggle = new List();
            Gtk.CellRendererText text;
            Gtk.CellRendererToggle toggle;
            Gtk.TreeView treeviewobject = null;
            Gtk.ListStore treeViewEngine = null;
            ArrayList columns = new ArrayList ();

            treeviewobject = (object) args[0] as Gtk.TreeView;
            treeViewEngine = (object) args[1] as Gtk.ListStore;

            var columns_treeview = new List();

            foreach (TreeViewColumn tvc in treeviewobject.Columns)
                treeviewobject.RemoveColumn(tvc);

            Type[] t = new Type[args_colums.GetUpperBound (0)+1];
            for (int colum_field = 0; colum_field <= args_colums.GetUpperBound (0); colum_field++) {
                if ((string)args_colums [colum_field, 1] == "text") {
                    t [colum_field] = typeof(string);
                }
                if ((string)args_colums [colum_field, 1] == "toogle") {
                    t [colum_field] = typeof(bool);
                }
            }
            treeViewEngine = new Gtk.ListStore(t);
            //treeViewEngine = liststore_;
            treeviewobject.Model = treeViewEngine;
            treeviewobject.RulesHint = true;
            //treeviewobject.Selection.Mode = SelectionMode.Multiple;
            if (args_colums.GetUpperBound (0) >= 0) {
                for (int colum_field = 0; colum_field <= args_colums.GetUpperBound (0); colum_field++) {
                    if ((string)args_colums [colum_field, 0] != "") {
                        if ((string)args_colums [colum_field, 1] == "text") {
                            // column for holiday names
                            text = new CellRendererText ();
                            text.Xalign = 0.0f;
                            columns.Add (text);
                            columns_treeview.Add (new TreeViewColumn ((string)args_colums [colum_field, 0], text, "text", colum_field));
                            //columns_renderertext.Add (new CellRendererText ()); 
                            //columns_renderertext [colum_field+1].Xalign = 0.0f;
                            //columns.Add (columns_renderertext [colum_field+1]);
                            //columns_treeview.Add (new TreeViewColumn ((string)args_colums [colum_field, 0], columns_renderertext [colum_field], "text", colum_field));
                            // ancho de la columna
                            if ((string) args_colums [colum_field, 2] != "") {
                                text.Width = int.Parse((string) args_colums [colum_field, 2]);
                            }
                            // color de la columna
                            if ((string) args_colums [colum_field, 3] != "") {
                                text.CellBackgroundGdk = new Gdk.Color (byte.Parse((string) args_colums [colum_field, 3]), byte.Parse((string) args_colums [colum_field, 4]), byte.Parse((string) args_colums [colum_field, 5]));
                            }
                            if (nombre_treeview_ == "treeview_concepto_remis"){
                                if(colum_field == 0) {
                                    text.Editable = true;
                                    text.Edited += NumberCellEdited_cantremis;
                                }
                                if(colum_field == 2) {
                                    text.Editable = true;
                                    text.Edited += NumberCellEdited_concepto;
                                }
                                if(colum_field == 3) {
                                    text.Editable = true;
                                    text.Edited += NumberCellEdited_precioremis;
                                }
                                if(colum_field == 5) {
                                    text.Editable = true;
                                    text.Edited += NumberCellEdited_lote;
                                }
                                if(colum_field == 6) {
                                    text.Editable = true;
                                    text.Edited += NumberCellEdited_caducidad;
                                }
                            }
                        }
                        if ((string)args_colums [colum_field, 1] == "toogle") {
                            // column for holiday names
                            toggle = new CellRendererToggle ();
                            toggle.Xalign = 0.0f;
                            columns.Add (toggle);
                            columns_treeview.Add (new TreeViewColumn ((string)args_colums [colum_field, 0], toggle, "active", colum_field));
                            //columns_renderertoggle.Add(new CellRendererToggle());
                            //columns_renderertoggle[colum_field].Xalign = 0.0f;
                            //columns.Add (columns_renderertoggle[colum_field]);
                            //columns_treeview.Add (new TreeViewColumn ((string)args_colums [colum_field, 0], columns_renderertoggle[colum_field], "active", colum_field));
                            //toggle.Toggled += new ToggledHandler (selecciona_fila);
                            // ancho de la columna
                            if ((string) args_colums [colum_field, 2] != "") {
                                toggle.Width = int.Parse((string) args_colums [colum_field, 2]);
                            }
                            // color de la columna
                            if ((string) args_colums [colum_field, 3] != "") {
                                toggle.CellBackgroundGdk = new Gdk.Color (byte.Parse((string) args_colums [colum_field, 3]), byte.Parse((string) args_colums [colum_field, 4]), byte.Parse((string) args_colums [colum_field, 5]));
                            }
                        }
                        columns_treeview [colum_field].Resizable = true;
                        treeviewobject.InsertColumn (columns_treeview [colum_field], colum_field);
                        columns_treeview [colum_field].Visible = Convert.ToBoolean((string)args_colums [colum_field, 6]);
                    }
                }
                if (nombre_treeview_ == "treeview_concepto_remis"){
                    //treeview_concepto_remis.RowActivated += on_button_ligar_erp_clicked;
                    treeview_concepto_remis = treeviewobject;
                    treeViewEngineconcepremis = treeViewEngine;
                }
                if (nombre_treeview_ == "treeview_lista_remisiones") {
                    treeview_lista_remisiones.RowActivated += on_button_abrirremis_clicked;
                    treeview_lista_remisiones = treeviewobject;
                    treeViewEngineListaRemis = treeViewEngine;
                }
            }
        }