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 Worksheets = new List(); public MSODRAWINGGROUP DrawingGroup; public List Records; /// /// Load workbook from a file path. /// /// public static Workbook Load(string file) { return Load(File.OpenRead(file)); } /// /// Load workbook from Stream. /// /// /// 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)); } /// /// Save workbook to a file. /// /// 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(); } } } /// /// Save workbook to stream. /// /// 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 ExtractImages() { List Images = new List(); 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(); } } }