jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Text;
using QiHe.CodeLib;
using HH.WMS.Utils.ExcelLibrary.BinaryFileFormat;
 
namespace HH.WMS.Utils.ExcelLibrary.SpreadSheet
{
    public class CellCollection
    {
        public Dictionary<int, Row> Rows = new Dictionary<int, Row>();
 
        public int FirstRowIndex = int.MaxValue;
        public int FirstColIndex = int.MaxValue;
        public int LastRowIndex = 0;
        public int LastColIndex = 0;
 
        internal SharedResource SharedResource;
 
        public ColumnWidth ColumnWidth = new ColumnWidth();
 
        public Cell CreateCell(int row, int col, object value, int XFindex)
        {
            XF xf = SharedResource.ExtendedFormats[XFindex];
            CellFormat foramt = SharedResource.CellFormats[xf.FormatIndex];
            Cell cell = new Cell(value, foramt);
            cell.SharedResource = this.SharedResource;
            cell.Style = CreateStyleFromXF(xf);
            this[row, col] = cell;
            return cell;
        }
 
        private CellStyle CreateStyleFromXF(XF xf)
        {
            CellStyle style = new CellStyle();
            style.BackColor = SharedResource.ColorPalette[xf.PatternColorIndex];
            return style;
        }
 
        public Row GetRow(int rowIndex)
        {
            if (!Rows.ContainsKey(rowIndex))
            {
                Rows[rowIndex] = new Row();
            }
            return Rows[rowIndex];
        }
 
        /// <summary>
        /// Get cell by row and col index
        /// </summary>
        /// <param name="row">starts from 0.</param>
        /// <param name="col">starts from 0.</param>
        /// <returns></returns>
        public Cell this[int row, int col]
        {
            get
            {
                if (Rows.ContainsKey(row))
                {
                    return GetRow(row).GetCell(col);
                }
                return Cell.EmptyCell;
            }
            set
            {
                FirstRowIndex = Math.Min(FirstRowIndex, row);
                FirstColIndex = Math.Min(FirstColIndex, col);
                LastRowIndex = Math.Max(LastRowIndex, row);
                LastColIndex = Math.Max(LastColIndex, col);
                value.SharedResource = this.SharedResource;
                GetRow(row).SetCell(col, value);
            }
        }
 
        public IEnumerator<Pair<Pair<int, int>, Cell>> GetEnumerator()
        {
            foreach (KeyValuePair<int, Row> row in Rows)
            {
                foreach (KeyValuePair<int, Cell> cell in row.Value)
                {
                    yield return new Pair<Pair<int, int>, Cell>
                        (new Pair<int, int>(row.Key, cell.Key), cell.Value);
                }
            }
        }
 
        public UInt16 DefaultRowHeight = 300; // twips
 
        // experiment, not works correctly
        public UInt16 GetRowIndexByPos(int y, out UInt16 dy)
        {
            UInt16 rowIndex = 0;
            UInt16 height = 0;
            int pos = (int)(y * 15);
            dy = (UInt16)pos;
            while (true)
            {
                height += GetRowHeight(rowIndex);
                if (height <= pos)
                {
                    dy = (UInt16)(pos - height);
                    rowIndex++;
                }
                else
                {
                    break;
                }
            }
            return rowIndex;
        }
 
        public UInt16 GetRowHeight(int rowIndex)
        {
            if (Rows.ContainsKey(rowIndex))
            {
                return Rows[rowIndex].Height;
            }
            else
            {
                return DefaultRowHeight;
            }
        }
 
        // experiment, not works correctly
        public UInt16 GetColumnIndexByPos(int x, out UInt16 dx)
        {
            UInt16 colIndex = 0;
            UInt16 width = 0;
            int pos = (int)(x * 33.75);
            dx = (UInt16)pos;
            while (true)
            {
                width += ColumnWidth[colIndex];
                if (width <= pos)
                {
                    dx = (UInt16)(pos - width);
                    colIndex++;
                }
                else
                {
                    break;
                }
            }
            return colIndex;
        }
    }
}