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
using System;
using System.Collections.Generic;
using System.Text;
using QiHe.CodeLib;
 
namespace HH.WMS.Utils.ExcelLibrary.SpreadSheet
{
    public class ColumnWidth
    {
        internal Dictionary<Pair<UInt16, UInt16>, UInt16> columnWidth = new Dictionary<Pair<ushort, ushort>, ushort>();
 
        public UInt16 Default = 8 * 256;
 
        public UInt16 this[UInt16 colIndex]
        {
            get
            {
                Pair<UInt16, UInt16> range = FindColumnRange(colIndex);
                if (columnWidth.ContainsKey(range))
                {
                    return columnWidth[range];
                }
                else
                {
                    return Default;
                }
            }
            set
            {
                Pair<UInt16, UInt16> range = FindColumnRange(colIndex);
                columnWidth[range] = value;
            }
        }
 
        /// <summary>
        /// Get or set column width, the unit is 1/256 of the width of the zero character, using default font.
        /// </summary>
        /// <param name="firstColIndex">Index to first column in the range</param>
        /// <param name="lastColIndex">Index to last column in the range</param>
        /// <returns></returns>
        public UInt16 this[UInt16 firstColIndex, UInt16 lastColIndex]
        {
            get { return columnWidth[new Pair<ushort, ushort>(firstColIndex, lastColIndex)]; }
            set { columnWidth[new Pair<ushort, ushort>(firstColIndex, lastColIndex)] = value; }
        }
 
        private Pair<UInt16, UInt16> FindColumnRange(UInt16 colIndex)
        {
            foreach (Pair<UInt16, UInt16> range in columnWidth.Keys)
            {
                if (range.Left <= colIndex && colIndex <= range.Right)
                {
                    return range;
                }
            }
            return new Pair<UInt16, UInt16>(colIndex, colIndex);
        }
 
        public IEnumerator<KeyValuePair<Pair<UInt16, UInt16>, UInt16>> GetEnumerator()
        {
            return columnWidth.GetEnumerator();
        }
    }
}