예제 #1
0
 //Prints hierarchical list's data from x,y position
 private static void PrintHierarchicalList(this ExcelWorksheet ws, int x, int y, HierarchyElement root)
 {
     ws.Cells[y, x].Value = root.Content;
     if (root.Children.Count > 0)
     {
         int startRow    = y + 1;
         int startColumn = x + 1;
         foreach (var child in root.Children)
         {
             ws.PrintHierarchicalList(startColumn, startRow, child);
             startRow += child.GetCount();
         }
     }
 }
예제 #2
0
        /// <summary>
        /// Insert a hierarchical list in tree style into the worksheet
        /// </summary>
        /// <param name="ws">Worksheet in wich the list is inserted</param>
        /// <param name="col">Starting column of the list</param>
        /// <param name="row">Starting row of the list</param>
        /// <param name="root">Root element of the list</param>
        /// <param name="title">Title of the list</param>
        /// <param name="color">color of the list</param>
        public static void InsertHierarchicalList(this ExcelWorksheet ws, int row, int col, HierarchyElement root, string title = null, ExcelColor color = ExcelColor.Primary)
        {
            //Get hierarchy debth and the number of items in it
            int depth  = root.GetDepth();
            int length = root.GetCount();

            //Get table range
            ExcelRange range = ws.Cells[row, col, row + length - 1, col + depth - 1];


            //Format

            var palette = PaletteStorage.GetPalette(color);

            var startRow = row;


            if (title != null)
            {
                //Get the title's cells and format title
                var titleCells = ws.Cells[range.Start.Row, range.Start.Column, range.Start.Row, range.End.Column];
                titleCells.Fill(palette.MainColor);
                titleCells.Merge();
                titleCells.BorderAround();
                titleCells.Value = title;
                //if there's title then the range should be changed after (pushed down by one row)
                range = ws.Cells[range.Start.Row + 1, range.Start.Column, range.End.Row + 1, range.End.Column];
                startRow++;
            }

            //Format table under title
            range.BorderAround();
            range.Fill(palette.LightColor);

            //Insert data into formatted cells
            ws.PrintHierarchicalList(col, startRow, root);

            //Set columns to a low width
            for (int i = col; i < (col + depth - 1); i++)
            {
                ws.Column(i).Width = 2;
            }
            //set last column to autosize
            //ws.Column(col + depth - 1).AutoFit();//ehhez gdi+ kell
        }