NOVINKA - Online rekvalifikační kurz Python programátor. Oblíbená a studenty ověřená rekvalifikace - nyní i online.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

LibreOffice export

C# .NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WindowsFormsApplication3
{
    public class CLibreCalc : IDisposable
    {
        Type _serviceManagerType;
        private object _serviceManager;
        private object _desktop;
        private object _component;

        public enum EHAlignCell
        {
            Standard,
            Left,
            Center,
            Right,
            Block,
            Repeat
        }

        public enum EFontWeight
        {
            UnKnown = 0,
            Thin = 50,
            UltraLight = 60,
            Light = 75,
            SemiLight = 90,
            Normal = 100,
            SemiBold = 110,
            Bold = 150,
            UltraBold = 175,
            Black = 200
        }

        public enum EFontSlant
        {
            None = 0,
            Oblique = 1,
            Italic = 2,
            UnKnown = 3,
            ReverseOblique = 4,
            ReverseItalic = 5
        }

        public CLibreCalc()
        {

        }

        private static string PathConverter(string filePath)
        {
            try
            {
                filePath = filePath.Replace(@"\", "/");
                return "file:///" + filePath;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        private object Invoke(object obj, string method, BindingFlags binding, params object[] par)
        {
            return obj.GetType().InvokeMember(method, binding, null, obj, par);
        }

        public void CreateDocument(string fileName, bool visible)
        {
            string absFileName = String.Empty;
            if (String.IsNullOrEmpty(fileName))
                absFileName = "private:factory/scalc";
            else
                absFileName = PathConverter(fileName);

            _serviceManagerType = Type.GetTypeFromProgID("com.sun.star.ServiceManager", true);
            _serviceManager = System.Activator.CreateInstance(_serviceManagerType);
            
            object[] par = new object[1];
            par[0] = "com.sun.star.frame.Desktop";
            _desktop = _serviceManager.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, _serviceManager, par);

            Object[] PropertyValue = { "com.sun.star.beans.PropertyValue" };
            Object[] name = { "Hidden" };
            Object[] val = { !visible };

            object hidden = (Object)_serviceManagerType.InvokeMember("Bridge_GetStruct", BindingFlags.InvokeMethod, null, _serviceManager, PropertyValue);
            Invoke(hidden, "Name", BindingFlags.SetProperty, name);
            Invoke(hidden, "Value", BindingFlags.SetProperty, val);

            object[] arg = new object[4];
            arg[0] = absFileName;
            arg[1] = "_blank";
            arg[2] = 0;
            arg[3] = new Object[] { hidden };

            _component = _desktop.GetType().InvokeMember("loadComponentFromUrl", BindingFlags.InvokeMethod, null, _desktop, arg);
        }        

        public object GetSheets()
        {
            object sheets = Invoke(_component, "getSheets", BindingFlags.InvokeMethod, null);
            return sheets;
        }

        public object GetSheetByIndex(int index)
        {
            object sheet = Invoke(GetSheets(), "getByIndex", BindingFlags.InvokeMethod, new object[] { index });
            return sheet;
        }

        public object GetRanges()
        {
            object ranges = Invoke(_component, "namedRanges", BindingFlags.GetProperty, null);
            return ranges;
        }

        private object GetNumFormats()
        {            
            object formats = Invoke(_component, "getNumberFormats", BindingFlags.InvokeMethod, null);
            return formats;
        }        

        public object GetRangeSelection(object sheet, string range)
        {
            return Invoke(sheet, "getCellRangeByName", BindingFlags.InvokeMethod, new object[] { range });
        }

        internal object GetCellRangeByPosition(object sheet, int x_beg, int y_beg, int x_end, int y_end)
        {
            return Invoke(sheet, "getCellRangeByPosition", BindingFlags.InvokeMethod, new object[] { x_beg, y_beg, x_end, y_end });
        }

        public object GetRows(object sheet)
        {
            object rows = Invoke(sheet, "rows", BindingFlags.GetProperty, null);
            return rows;
        }

        public object GetColumns(object sheet)
        {
            object columns = Invoke(sheet, "columns", BindingFlags.GetProperty, null);
            return columns;
        }

        public object GetColumnByIndex(object sheet, int index)
        {
            object column = Invoke(GetColumns(sheet), "getByIndex", BindingFlags.InvokeMethod, new object[] { index });
            return column;
        }

        public object GetCell(object sheet, int columnIndex, int rowIndex)
        {
            object cell = Invoke(sheet, "getCellByPosition", BindingFlags.InvokeMethod, new object[] { 0, 0 });
            return cell;
        }

        public object GetCell(object sheet, string cellName)
        {
            object cell = Invoke(sheet, "getCellRangeByName", BindingFlags.InvokeMethod, new object[] { cellName });
            return cell;
        }

        public string GetCellByPosition(object sheet, int columnIndex, int rowIndex)
        {
            string value = string.Empty;
            object cell = null;

            cell = Invoke(sheet, "getCellByPosition", BindingFlags.InvokeMethod, new object[] { columnIndex, rowIndex });

            int cellType = (int)Invoke(cell, "getType", BindingFlags.InvokeMethod, null);

            switch (cellType)
            {
                case 1:
                    value = Invoke(cell, "getValue", BindingFlags.InvokeMethod, null).ToString();
                    break;
                case 3:
                    value = Invoke(cell, "getFormula", BindingFlags.InvokeMethod, null).ToString();
                    break;
                default:
                    value = (string)Invoke(cell, "getString", BindingFlags.InvokeMethod, null);
                    break;
            }

            return value;
        }

        public string GetCellValueByName(object sheet, string cellName)
        {
            object range = Invoke(sheet, "getCellRangeByName", BindingFlags.InvokeMethod, new object[] { cellName });
            if (range == null)
            {
                throw new Exception("Bunka s názvom " + cellName + " neexistuje!");
            }

            return (string)Invoke(range, "getString", BindingFlags.InvokeMethod, null);
        }

        public string GetCellValueByRange(object sheet, string range)
        {
            object selection = Invoke(sheet, "getCellRangeByName", BindingFlags.InvokeMethod, new object[] { range });
            if (selection == null)
            {
                throw new Exception("Rozsah " + range + " nie je definovaný!");
            }
            object cell = Invoke(selection, "getCellByPosition", BindingFlags.InvokeMethod, new object[] { 0, 0 });
            if (cell == null)
            {
                throw new Exception("Nedefinovaná bunka!");
            }

            return (string)Invoke(cell, "getFormula", BindingFlags.InvokeMethod, null);
        }

        public void SetCellValue(object sheet, int columnIndex, int rowIndex, string value)
        {
            object cell = GetCell(sheet, columnIndex, rowIndex);
            Invoke(cell, "setFormula", BindingFlags.InvokeMethod, new object[] { value });
        }

        public void SetCellValue(object sheet, string cellName, object value)
        {
            object cell = GetCell(sheet, cellName);
            Invoke(cell, "setFormula", BindingFlags.InvokeMethod, new object[] { value });
        }

        public void SetCellString(object sheet, string cellName, string value)
        {
            object cell = GetCell(sheet, cellName);
            Invoke(cell, "setString", BindingFlags.InvokeMethod, new object[] { value });
        }

        public object GetDataArray(object sheet, string range)
        {
            object rangeSel = GetRangeSelection(sheet, range);

            return Invoke(rangeSel, "getDataArray", BindingFlags.InvokeMethod, null);
        }

        public void SetDataArray(object sheet, string range, object[] data)
        {
            object rangeSel = GetRangeSelection(sheet, range);

            Invoke(rangeSel, "setDataArray", BindingFlags.InvokeMethod, new object[] { new object[] {data }});
        }

        public void SetDataArray2D(object sheet, string range, object data)
        {
            object rangeSel = GetRangeSelection(sheet, range);

            Invoke(rangeSel, "setDataArray", BindingFlags.InvokeMethod, new object[] { data });
        }

        //https://www.openoffice.org/api/docs/common/ref/com/sun/star/table/CellProperties.html
        public EHAlignCell GetCellAlign(object sheet, string cellName)
        {
            object cell = GetCell(sheet, cellName);
            object align = Invoke(cell, "HoriJustify", BindingFlags.GetProperty, null);
            return (EHAlignCell)align;
        }

        public void SetCellAlign(object sheet, string cellName, EHAlignCell value)
        {
            object cell = GetCell(sheet, cellName);
            Invoke(cell, "HoriJustify", BindingFlags.SetProperty, new object[] { (int)value });            
        }

        //https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Formatting
        public void SetCellFontWeight(object sheet, string cellName, EFontWeight fontWeight)
        {
            object cell = GetCell(sheet, cellName);
            Invoke(cell, "CharWeight", BindingFlags.SetProperty, new object[] { (int)fontWeight });
        }

        public void SetCellFontSlant(object sheet, string cellName, EFontSlant fontSlant)
        {
            object cell = GetCell(sheet, cellName);
            Invoke(cell, "CharPosture", BindingFlags.SetProperty, new object[] { (int)fontSlant });
        }

        private int GetWidthFromPixels(int pixels)
        {
            return pixels * 35;
        }

        public void SetColumnWidth(object sheet, int index, int width)
        {
            Invoke(GetColumnByIndex(sheet, index), "Width", BindingFlags.SetProperty, new object[] { GetWidthFromPixels(width) });
        }

        public void SetCellFormat(object sheet, string cellName, string formatString)
        {
            object[] localSettings = { "com.sun.star.lang.Locale" };
            object locSett = (Object)_serviceManagerType.InvokeMember("Bridge_GetStruct", BindingFlags.InvokeMethod, null, _serviceManager, localSettings);
            //string numberString = "# ##0,00";

            object formats = GetNumFormats();
            object formatID = Invoke(formats, "queryKey", BindingFlags.InvokeMethod, new object[] { formatString, locSett, false });

            if (Convert.ToInt32(formatID) == -1)
                formatID = Invoke(formats, "addNew", BindingFlags.InvokeMethod, new object[] { formatString, locSett });

            object cell = GetCell(sheet, cellName);

            //Invoke(cell, "NumberFormat", BindingFlags.SetProperty, new object[] { formatID });
            Invoke(cell, "NumberFormat", BindingFlags.SetProperty, new object[] { formatID });
        }

        public void SetFrozenPosition(object sheet, int columns, int rows)
        {
            object _controller = Invoke(_component, "getCurrentController", BindingFlags.InvokeMethod, null);
            Invoke(_controller, "freezeAtPosition", BindingFlags.InvokeMethod, new object[] { columns, rows });
        }

        //public void SetCellNumFormat(object sheet, string cellName, string format)
        //{
        //    object cell = GetCell(sheet, cellName);
        //    Invoke(cell, "NumberFormat", BindingFlags.SetProperty, new object[] { format });
        //    //Invoke(GetColumnByIndex(sheet,0), "NumberString", BindingFlags.SetProperty, new object[] { "#.###" });
        //}

        //public object GetCellFormatCode(object sheet, string cellName)
        //{
        //    object cell = GetCell(sheet, cellName);
        //    object formatCode = Invoke(cell, "NumberFormat", BindingFlags.GetProperty, null);
        //    return formatCode;
        //}

        public void CloseCalc()
        {
            Invoke(_component, "dispose", BindingFlags.InvokeMethod, null);
        }

        public void SaveCalc(string pathFileName)
        {
            //Invoke(_component, "store", BindingFlags.InvokeMethod, new object[] { "d:" });
            string fileName = pathFileName;
            fileName = "file:///" + fileName.Replace(@"\", "/");
            Object[] savePropertyValue = new object[]{};
            object _store = Invoke(_component, "StoreToUrl", BindingFlags.InvokeMethod, new object[] { fileName, savePropertyValue });
        }

        public void SetVisible(bool visible)
        {
            //Invoke(_component, "Visible", BindingFlags.SetProperty, new object[] { visible });
            object _controller = Invoke(_component, "getCurrentController", BindingFlags.InvokeMethod, null);
            object _frame = Invoke(_controller, "getFrame", BindingFlags.InvokeMethod, null);
            object _container = Invoke(_frame, "getContainerWindow", BindingFlags.InvokeMethod, null);
            Invoke(_container, "Visible", BindingFlags.SetProperty, new object[] { visible});
            //Invoke(_container, "toFront", BindingFlags.InvokeMethod, null);
        }

        public void Dispose()
        {
            Invoke(_component, "dispose", BindingFlags.InvokeMethod, null);
        }
    }
}

Neformátovaný

Přidáno: 13.11.2015
Expirace: Neuvedeno

Aktivity