123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Biff8Excel.Interfaces;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using Biff8Excel.Formulas;
- using Microsoft.Win32;
- using System.Collections;
- namespace Biff8Excel.Excel
- {
- public class ExcelWorkbook: IDisposable
- {
- string m_fname;
- string m_password ="";
-
- bool bFontsCreated;
- bool bActiveSheetSet;
- List<BoundSheet> mColBoundSheets;
- WorkSheetCollection mColSheets;
- List<ExcelFont> mColFonts;
- List<ExcelFormat> mColFormats;
- List<ExcelCellStyle> mColCellStyls;
-
- ExcelSst recSST;
- Helper recHelper;
- ExternSheet recExternSheet;
- Records.BOF recBOF;
- Records.Windows1 recWindow1;
- Records.SupBook recSupBook;
- ExcelBinaryWriter bw;
-
-
-
-
-
-
- internal bool Calculate()
- {
- ExcelCell oCell;
- ushort oldFailCount;
- ushort failCount =0;
- ExcelCellDictionary ColFormulaCells;
- ExcelCellDictionary ColNumberFormulaCells;
-
- ColFormulaCells = new ExcelCellDictionary();
- ColNumberFormulaCells = new ExcelCellDictionary();
- foreach (ExcelWorksheet sheet in mColSheets)
- {
- if (sheet.TotalRows > 0)
- {
-
- foreach (KeyValuePair<ushort, ExcelCellLinkDictionary> kv in sheet.Rows)
- {
- foreach (KeyValuePair<ushort, ExcelCell> kvc in kv.Value)
- {
- oCell = kvc.Value ;
- if (oCell.CellType == Globals.CELL_TYPE_FORMULA)
- {
- ColFormulaCells.Add(sheet.sheetNumber.ToString("000") + "!" + oCell.CellReference, oCell);
- ColNumberFormulaCells.Add(sheet.sheetNumber.ToString("000") + "!" + oCell.CellReference, oCell);
- }
- else if (oCell.CellType == Globals.CELL_TYPE_NUMBER)
- {
- ColNumberFormulaCells.Add(sheet.sheetNumber.ToString("000") + "!" + oCell.CellReference, oCell);
- }
- }
- }
- }
- }
-
-
-
-
- do
- {
- oldFailCount = failCount;
- failCount = 0;
- foreach (DictionaryEntry oCells in ColFormulaCells)
- {
- if ( !((ExcelCell)oCells.Value).CalculationResolved)
- {
- ((ExcelCell)oCells.Value).Calculate(ColNumberFormulaCells);
- if (((ExcelCell)oCells.Value).CalculationResolved == false)
- failCount++;
- }
- }
-
-
-
- } while (oldFailCount != failCount);
- return (failCount == 0);
- }
- public ExcelCell CreateCell()
- {
- ExcelCell ec = new ExcelCell(this);
- ec.pWorkbook = this;
- return ec;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void CreateDefaultsCellStyles()
- {
- ExcelCellStyle cs;
- for (int i = 1; i < 22; i++)
- {
-
- cs = this.CreateStyle();
-
- cs.VerticalAlignment = EnumVerticalAlignment.Bottom;
- cs.PatternForeColour = EnumColours.SystemBorder;
- cs.PatternBackColour = EnumColours.SystemBackground;
- cs.CellIsLocked = true;
- if (i == 1)
- cs.StyleType = EnumType.Style;
- else if (i >= 2 & i <= 3)
- {
- cs.StyleType = EnumType.Style;
- cs.FontIndex = 1;
- cs.UseParentStyleOptions = 0xF4;
- }
- if (i >= 4 && i <= 5)
- {
- cs.StyleType = EnumType.Style;
- cs.FontIndex = 2;
- cs.UseParentStyleOptions = 0xF4;
- }
- if (i >= 6 && i <= 15)
- {
- cs.StyleType = EnumType.Style;
- cs.UseParentStyleOptions = 0xF4;
- }
- if (i == 16)
- {
- cs.StyleType = EnumType.Cell;
- }
- if (i >= 17 && i <= 21)
- {
- cs.StyleType = EnumType.Style;
- cs.FontIndex = 1;
- cs.UseParentStyleOptions = 0xF8;
- if (i == 17)
- cs.FormatIndex = 0x2B;
- else if (i == 18)
- cs.FormatIndex = 0x29;
- else if (i == 19)
- cs.FormatIndex = 0x2C;
- else if (i == 20)
- cs.FormatIndex = 0x2A;
- else if (i == 21)
- cs.FormatIndex = 0x9;
- }
- mColCellStyls.Add(cs);
- }
- }
- internal ushort CreateNewFont(ExcelFont fnt)
- {
- if (fnt.Name.Length == 0)
- throw new Biff8ExcelException("no font name");
- mColFonts.Add(fnt);
- return (ushort)mColFonts.Count;
- }
- internal ushort CreateNewFormat(ExcelFormat fmt)
- {
- if (fmt.FormatString.Length == 0)
- return 0;
- mColFormats.Add(fmt);
- fmt.index = (ushort)(mColFormats.Count + 163);
- return fmt.index;
- }
- internal ushort CreateNewStyle(ExcelCellStyle cs)
- {
- mColCellStyls.Add(cs);
- return (ushort)(mColCellStyls.Count - 1);
- }
-
-
-
-
- public ExcelCellStyle CreateStyle()
- {
- ExcelCellStyle cs = new ExcelCellStyle();
- cs.pWorkbook = this;
- return cs;
- }
-
-
-
-
-
- public ExcelWorksheet CreateSheet(string sSheetName)
- {
- return CreateSheet(sSheetName, true);
- }
-
-
-
-
-
-
- public ExcelWorksheet CreateSheet(string sSheetName, bool bVisible)
- {
- ExcelWorksheet ws;
- BoundSheet bs;
-
- ws = new ExcelWorksheet(this);
-
- ws.SheetName = sSheetName;
- ws.sheetNumber = (ushort)mColSheets.Count;
- mColSheets.Add(ws);
-
- bs = new BoundSheet();
- bs.SheetName = sSheetName;
- bs.Visible = (bVisible == true ? Visibility.Visible : Visibility.Hidden);
- mColBoundSheets.Add(bs);
- return ws;
- }
- internal Interfaces.IExternSheet ExterSheet
- {
- get { return recExternSheet; }
- }
- public ExcelWorkbook()
- {
- mColFonts = new List<ExcelFont>();
- mColFormats = new List<ExcelFormat>();
- mColCellStyls = new List<ExcelCellStyle>();
- mColSheets = new WorkSheetCollection();
- mColBoundSheets = new List<BoundSheet>();
- recSST = new ExcelSst();
- recSupBook = new Biff8Excel.Records.SupBook();
- recExternSheet = new ExternSheet();
- recHelper = new Helper();
- recWindow1 = new Biff8Excel.Records.Windows1();
- recWindow1.ActivewWorksheet = 1;
- recHelper = new Helper();
- recWindow1 = new Biff8Excel.Records.Windows1();
- recWindow1.ActivewWorksheet = 1;
- recHelper.ExcelWorkSheets = mColSheets;
- recExternSheet.Workbook = this;
- CreateDefaultsCellStyles();
- bw = new ExcelBinaryWriter();
-
- }
- internal List<ExcelFont> Fonts
- {
- get { return mColFonts; }
- }
-
- internal List<ExcelFormat> Formats
- {
- get { return mColFormats; }
- }
-
-
-
-
-
- public ExcelWorksheet GetSheet(object sheet)
- {
- if (sheet is string)
- return mColSheets[sheet.ToString()];
- else if (sheet is int)
- return mColSheets[Convert.ToInt32(sheet)];
- throw new Biff8ExcelException("no type sheet");
-
-
- }
- public Helper Helper
- {
- get { return recHelper; }
- }
- public string Password
- {
- set { m_password = value; }
- }
- internal List<ExcelCellStyle> Styles
- {
- get { return mColCellStyls; }
- }
- internal ExcelSst SharedStringTable
- {
- get { return recSST; }
- }
-
-
-
-
- public object SetActiveSheet
- {
- set
- {
- ExcelWorksheet oSheet;
- int tmp;
- if (bActiveSheetSet)
- return;
- bActiveSheetSet = true;
- if (int.TryParse(value.ToString(), out tmp))
- {
- recWindow1.ActivewWorksheet = (ushort)(tmp - 1);
- oSheet = mColSheets[tmp];
- if (oSheet != null)
- oSheet.Selected = true;
- }
- else
- {
- mColSheets[value.ToString()].Selected = true;
- recWindow1.ActivewWorksheet = mColSheets[value.ToString()].sheetNumber;
- }
- }
- }
- internal ushort SheetIndexFromName(string sSheetName)
- {
- ushort count = 0;
- bool bfound = false ;
- foreach (BoundSheet bs in mColBoundSheets)
- {
- if (bs.SheetName.ToUpper() == sSheetName.ToUpper())
- {
- bfound = true;
- break;
- }
- count++;
- }
-
- if (bfound)
- return count;
- else
- throw new Biff8ExcelException("Invalid Sheet Name");
- }
- public void Save(string fname)
- {
- byte[] buffer;
- int count;
- uint offset;
- uint endOfWorkbook;
- string s;
- m_fname = fname;
- if (this.Calculate() == false)
- {
-
-
-
-
- }
-
-
-
-
- foreach (ExcelWorksheet sheet in mColSheets)
- {
-
- if (m_password.Length > 0)
- {
- sheet.password = m_password;
- }
- sheet.CreateSheetBuffer();
- }
-
- recBOF = new Biff8Excel.Records.BOF();
- recBOF.DataType = Biff8Excel.Records.EnumBofTypes.bofWorkbook;
- buffer = recBOF.GetByte();
- bw.Write(buffer);
-
- if (mColSheets.Count > 0)
- this.SetActiveSheet = 1;
-
- recWindow1.Width = 15360;
- recWindow1.Height = 11520;
- recWindow1.VerticalScrollBar = true;
- recWindow1.HorizontalScrollBar = true;
- recWindow1.DisplayTabsAtBottom = true;
- recWindow1.SelectedWorksheets = 1;
- recWindow1.TabWidth = 600;
- buffer = recWindow1.GetByte();
- bw.Write(buffer);
-
-
-
- this.SetDefaultFont("宋体", 12);
- foreach (ExcelFont fnt in mColFonts)
- {
- buffer = fnt.WriteRecord();
- bw.Write(buffer);
- }
-
- this.WriteDefaultFormats();
-
- foreach (ExcelFormat fmt in mColFormats)
- {
- buffer = fmt.WriteRecord();
- bw.Write(buffer);
- }
-
- foreach (ExcelCellStyle cs in mColCellStyls)
- {
- buffer = cs.WriteRecord();
- bw.Write(buffer);
- }
-
- this.WriteDefaultStyles();
-
-
-
-
-
-
- foreach (BoundSheet bs in mColBoundSheets)
- {
- bs.StreamOffset = (uint)bw.Position;
-
- buffer = bs.WriteRecord;
- bw.Write(buffer);
- }
-
- recSupBook.TotalSheets = (ushort)mColBoundSheets.Count;
- buffer = recSupBook.GetByte();
- bw.Write(buffer);
-
- buffer = recExternSheet.WriteRecord();
- bw.Write(buffer);
-
-
-
- buffer = recSST.WriteRecord();
- bw.Write(buffer);
-
- s = "0A000000";
- bw.Write(Globals.WriteBytes(s));
-
-
-
- offset = (uint)bw.Position;
-
- endOfWorkbook = offset;
- count = 1;
- foreach (BoundSheet bs in mColBoundSheets)
- {
- bw.Position = (int)bs.StreamOffset;
- bs.SheetOffset = offset;
- buffer = bs.WriteRecord;
- bw.Write(buffer);
- offset += mColSheets[count].SizeOnDisk;
- count++;
- }
- bw.Position = (int)endOfWorkbook;
-
- foreach (ExcelWorksheet ws in mColSheets)
- {
- ws.UpdateAbsoluteOffsets((uint)bw.Position);
- bw.Write(ws.DiskBuffer);
- }
-
- COM.Storage file;
- COM.COMStream Data;
- if (File.Exists(m_fname))
- {
- File.SetAttributes(m_fname, FileAttributes.Normal);
- File.Delete(m_fname);
- }
- file = COM.Storage.CreateDocFile(m_fname, COM.StorageMode.ReadWrite | COM.StorageMode.ShareExclusive);
- Data = file.CreateStream("workbook");
- Data.WriteBytes(bw.Stream);
-
-
-
-
- file.Commit(0);
- file.Dispose();
- Data = null;
- }
-
-
-
-
-
- public void SetDefaultFont(string name, ushort size)
- {
- if (bFontsCreated)
- return;
- ExcelFont f;
- bFontsCreated = true;
-
- for (int i = 1; i < 5; i++)
- {
- f = new ExcelFont();
- f.Name = name;
- f.Size = size;
- f.Colour = EnumColours.SystemText;
- f.Bold = false;
- mColFonts.Add(f);
- }
- }
- internal string[] SheetNames
- {
- get
- {
- if (mColSheets.Count == 0)
- return null;
- ushort x = 0;
- string[] s = new string[mColSheets.Count];
- foreach (ExcelWorksheet ws in mColSheets)
- {
- s[x] = ws.SheetName;
- x++;
- }
- return s;
- }
- }
- private void WriteDefaultStyles()
- {
- string s;
- s = "93020400108003FF"
- + "93020400118006FF"
- + "93020400128004FF"
- + "93020400138007FF"
- + "93020400008000FF"
- + "93020400148005FF";
- bw.Write(Globals.WriteBytes(s));
- }
- private void WriteDefaultFormats()
- {
- string s;
- s = "1E041800050013000022A322232C2323303B5C2D22A322232C232330"
- + "1E041D00060018000022A322232C2323303B5B5265645D5C2D22A322232C232330"
- + "1E041E00070019000022A322232C2323302E30303B5C2D22A322232C2323302E3030"
- + "1E04230008001E000022A322232C2323302E30303B5B5265645D5C2D22A322232C2323302E3030"
- + "1E0435002A003000005F2D22A3222A20232C2323305F2D3B5C2D22A3222A20232C2323305F2D3B5F2D22A3222A20222D225F2D3B5F2D405F2D"
- + "1E042C0029002700005F2D2A20232C2323305F2D3B5C2D2A20232C2323305F2D3B5F2D2A20222D225F2D3B5F2D405F2D"
- + "1E043D002C003800005F2D22A3222A20232C2323302E30305F2D3B5C2D22A3222A20232C2323302E30305F2D3B5F2D22A3222A20222D223F3F5F2D3B5F2D405F2D"
- + "1E0434002B002F00005F2D2A20232C2323302E30305F2D3B5C2D2A20232C2323302E30305F2D3B5F2D2A20222D223F3F5F2D3B5F2D405F2D";
- bw.Write(Globals.WriteBytes(s));
- }
-
-
-
-
- #region IDisposable 成员
- public void Dispose()
- {
- mColFonts = null;
- mColFormats = null;
- mColCellStyls = null;
- mColSheets.Dispose();
- mColSheets = null;
- mColBoundSheets = null;
- recSST.Dispose();
- recSST = null;
- recSupBook = null;
- recExternSheet.Dispose();
- recExternSheet = null;
- recHelper.Dispose();
- recHelper = null;
- recWindow1 = null;
- }
- #endregion
- }
- }
|