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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using HH.WMS.Utils.ExcelLibrary.CompoundDocumentFormat;
using HH.WMS.Utils.ExcelLibrary.BinaryFileFormat;
using HH.WMS.Utils.ExcelLibrary.BinaryDrawingFormat;
 
namespace HH.WMS.Utils.ExcelLibrary.SpreadSheet
{
    public class Workbook
    {
        public List<Worksheet> Worksheets = new List<Worksheet>();
 
        public MSODRAWINGGROUP DrawingGroup;
 
        public List<Record> Records;
 
        /// <summary>
        /// Load workbook from a file path.
        /// </summary>
        /// <param name="file"></param>
        public static Workbook Load(string file)
        {
            return Load(File.OpenRead(file));
        }
 
        /// <summary>
        /// Load workbook from Stream.
        /// </summary>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        public static Workbook Load(Stream stream)
        {
            CompoundDocument doc = CompoundDocument.Load(stream);
            if (doc == null) throw new Exception("Invalid Excel file");
            byte[] bookdata = doc.GetStreamData("Workbook");
            return WorkbookDecoder.Decode(new MemoryStream(bookdata));
        }
 
        /// <summary>
        /// Save workbook to a file.
        /// </summary>
        /// <param name="file"></param>
        public void Save(string file)
        {
            using (CompoundDocument doc = CompoundDocument.Create(file))
            {
                using (MemoryStream memStream = new MemoryStream())
                {
                    WorkbookEncoder.Encode(this, memStream);
                    doc.WriteStreamData(new string[] { "Workbook" }, memStream.ToArray());
                    doc.Save();
                }
            }
        }
 
        /// <summary>
        /// Save workbook to stream.
        /// </summary>
        /// <param name="stream"></param>
        public void Save(Stream stream)
        {
            CompoundDocument doc = CompoundDocument.Create(stream);
            using (MemoryStream memStream = new MemoryStream())
            {
                WorkbookEncoder.Encode(this, memStream);
                doc.WriteStreamData(new string[] { "Workbook" }, memStream.ToArray());
                doc.Save();
            }
        }
 
        public List<byte[]> ExtractImages()
        {
            List<byte[]> Images = new List<byte[]>();
            if (DrawingGroup != null)
            {
                MsofbtDggContainer dggContainer = DrawingGroup.EscherRecords[0] as MsofbtDggContainer;
                foreach (MsofbtBSE blipStoreEntry in dggContainer.BstoreContainer.EscherRecords)
                {
                    if (blipStoreEntry.BlipRecord == null) continue;
                    Images.Add(blipStoreEntry.ImageData);
                }
            }
            return Images;
        }
 
        public Image ExtractImage(int index)
        {
            if (DrawingGroup != null)
            {
                MsofbtDggContainer dggContainer = DrawingGroup.EscherRecords[0] as MsofbtDggContainer;
                MsofbtBSE blipStoreEntry = dggContainer.BstoreContainer.EscherRecords[index] as MsofbtBSE;
                if (blipStoreEntry.BlipRecord != null)
                {
                    return new Image(blipStoreEntry.ImageData, blipStoreEntry.BlipRecord.Type);
                }
            }
            return null;
        }
 
        internal void RemoveRecord(int index)
        {
            Record rec = Records[index];
            foreach (Record record in Records)
            {
                if (record.Type == RecordType.BOUNDSHEET)
                {
                    //Points to worksheet BOF record
                    ((BOUNDSHEET)record).StreamPosition -= (uint)rec.FullSize;
                    record.Encode();
                }
                if (record.Type == RecordType.EXTSST)
                {
                    foreach (StringOffset offset in ((EXTSST)record).Offsets)
                    {
                        offset.AbsolutePosition -= (uint)rec.FullSize;
                    }
                    record.Encode();
                }
                if (rec.Type == RecordType.XF && record is CellValue)
                {
                    (record as CellValue).XFIndex = 0;
                    record.Encode();
                }
            }
            Records.RemoveAt(index);
        }
 
        public void SaveToStream(Stream stream)
        {
            CompoundDocument doc = CompoundDocument.CreateFromStream(stream);
            MemoryStream mstream = new MemoryStream();
            WorkbookEncoder.Encode(this, mstream);
            doc.WriteStreamData(new string[] { "Workbook" }, mstream.ToArray());
            doc.Save();
        }
    }
}