Excel_Biff8.cs 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Drawing;
  7. using FastReport.Export;
  8. using FastReport.Utils;
  9. using FastReport;
  10. using FastReport.Format;
  11. namespace FastReport.Export.BIFF8
  12. {
  13. internal class OLE_Property
  14. {
  15. internal UInt32 propertyID;
  16. internal UInt32 propertyOffset;
  17. internal UInt32 propertyType;
  18. internal object propertyData;
  19. }
  20. internal class OLE_Stream : BIFF8_Stream
  21. {
  22. private DirectoryEntry entry;
  23. public OLE_Stream(CompoundDocumentHeader document_header, DirectoryEntry dir)
  24. : base(document_header, dir)
  25. {
  26. entry = dir;
  27. }
  28. internal void Read()
  29. {
  30. UInt16 byte_order = ReadUshort();
  31. UInt16 Version = ReadUshort();
  32. UInt32 System = ReadUint();
  33. byte[] UID = ReadBytes(16);
  34. UInt32 PropetiesCount = ReadUint();
  35. int up_level_position = (int)this.Position;
  36. for (int i = 0; i < PropetiesCount; i++)
  37. {
  38. this.Position = up_level_position;
  39. byte[] FormatID = ReadBytes(16);
  40. UInt32 Offset = ReadUint();
  41. up_level_position = (int)this.Position;
  42. this.Position = (int)Offset;
  43. int top_border = (int)this.Position;
  44. UInt32 cbSection = ReadUint(); // Section size in bytes
  45. UInt32 cProreties = ReadUint(); // Number of Properties in the Set
  46. OLE_Property[] summary_properties = new OLE_Property[cProreties];
  47. for (int j = 0; j < cProreties; j++)
  48. {
  49. summary_properties[j] = new OLE_Property();
  50. summary_properties[j].propertyID = ReadUint();
  51. summary_properties[j].propertyOffset = ReadUint();
  52. }
  53. for (int j = 0; j < cProreties; j++)
  54. {
  55. this.Position = top_border + (int)summary_properties[j].propertyOffset;
  56. summary_properties[j].propertyType = ReadUint();
  57. switch (summary_properties[j].propertyType)
  58. {
  59. case 0x0002: // uInt32
  60. summary_properties[j].propertyData = ReadUint();
  61. break;
  62. //case 0x1e00: //
  63. // ReadBytes(0x10 - sizeof(UInt32));
  64. // break;
  65. //case 0x4000: //
  66. // ReadBytes(0x0c - sizeof(UInt32));
  67. // break;
  68. //case 0x4001: //
  69. // ReadBytes(0x0c - sizeof(UInt32));
  70. // break;
  71. case 0x001e: // ansi string
  72. UInt32 strlen = ReadUint();
  73. string value = "";
  74. for (int k = 0; k < strlen; k++)
  75. {
  76. value += (char)ReadByte();
  77. }
  78. break;
  79. case 0x0040: // DateTime
  80. UInt32 time_low = ReadUint();
  81. UInt32 time_high = ReadUint();
  82. break;
  83. case 0x0003: // Access rights
  84. summary_properties[j].propertyData = ReadUint();
  85. break;
  86. case 0x000b: // Unknown property
  87. summary_properties[j].propertyData = ReadUint();
  88. break;
  89. case 0x101e: // Unknown property
  90. ReadBytes(0x29 - sizeof(UInt32));
  91. break;
  92. case 0x100c:
  93. ReadBytes(0x23 - sizeof(UInt32));
  94. break;
  95. default:
  96. //throw new Exception("Unimplemented property type");
  97. Console.WriteLine("Unimplemented property type: " + summary_properties[j].propertyType.ToString());
  98. break;
  99. }
  100. int prop_size;
  101. if (j + 1 < cProreties)
  102. {
  103. prop_size = (int)(summary_properties[j + 1].propertyOffset - summary_properties[j].propertyOffset);
  104. }
  105. else
  106. {
  107. prop_size = (int)(cbSection - summary_properties[j].propertyOffset);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. internal class BIFF8_Window1
  114. {
  115. UInt16 hPos;
  116. UInt16 vPos;
  117. UInt16 width;
  118. UInt16 height;
  119. UInt16 flags;
  120. UInt16 active_worksheet;
  121. UInt16 first_visible_tab;
  122. UInt16 selected_sheet_count;
  123. UInt16 worksheet_tab_width;
  124. internal void Read(StreamHelper stream)
  125. {
  126. hPos = stream.ReadUshort();
  127. vPos = stream.ReadUshort();
  128. width = stream.ReadUshort();
  129. height = stream.ReadUshort();
  130. flags = stream.ReadUshort();
  131. active_worksheet = stream.ReadUshort();
  132. first_visible_tab = stream.ReadUshort();
  133. selected_sheet_count = stream.ReadUshort();
  134. worksheet_tab_width = stream.ReadUshort();
  135. }
  136. internal void Write(StreamHelper stream)
  137. {
  138. stream.WriteUshort(0x003d); // WINDOW1
  139. stream.WriteUshort(0x0012); // Payload size
  140. stream.WriteUshort(0x0168); // Horizontal position
  141. stream.WriteUshort(0x0069); // Vertical position
  142. width = 15600;
  143. height = 8190;
  144. stream.WriteUshort(width);
  145. stream.WriteUshort(height);
  146. stream.WriteUshort(0x0038); // Window flags
  147. stream.WriteUshort(0x0000); // Active worksheet
  148. stream.WriteUshort(0x0000); // First visible tab
  149. stream.WriteUshort(0x0001); // Selected sheet count
  150. stream.WriteUshort(0x0258); // Worksheet tab width
  151. }
  152. }
  153. internal class BIFF8_Font
  154. {
  155. internal UInt16 fontHeight;
  156. internal UInt16 options;
  157. internal UInt16 colourIdx;
  158. internal UInt16 fontWeight;
  159. internal UInt16 escapement;
  160. internal byte underlineType;
  161. internal byte fontFamily;
  162. internal byte characterSet;
  163. internal string fontName;
  164. /*
  165. internal void Debug(StreamWriter debug, int idx)
  166. {
  167. debug.Write("\nFont {0}", idx);
  168. debug.Write("\n\tName\t{0}", FontName);
  169. debug.Write("\n\tSize\t{0}", FontHeight);
  170. debug.Write("\n\tWeight\t{0}", FontWeight);
  171. debug.Write("\n\tColor\t{0}", ColourIdx);
  172. debug.Write("\n\tOptions\t{0:X2}", Options);
  173. debug.Write("\n\tEsc\t{0:X2}", Escapement);
  174. debug.Write("\n\tUnderline\t{0:X}", UnderlineType);
  175. debug.Write("\n\tFamily\t{0:X}", FontFamily);
  176. debug.Write("\n\tCharacterSet\t{0:X}\n", CharacterSet);
  177. }
  178. */
  179. internal void Read(StreamHelper stream)
  180. {
  181. fontHeight = stream.ReadUshort();
  182. options = stream.ReadUshort();
  183. colourIdx = stream.ReadUshort();
  184. fontWeight = stream.ReadUshort();
  185. escapement = stream.ReadUshort();
  186. underlineType = (byte)stream.ReadByte();
  187. fontFamily = (byte)stream.ReadByte();
  188. characterSet = (byte)stream.ReadByte();
  189. stream.SkipBytes(1);
  190. fontName = stream.ReadUnicodeString(true);
  191. }
  192. internal void Write(StreamHelper stream)
  193. {
  194. stream.WriteUshort(0x0031);
  195. ushort payload_size = (ushort)(14 + 2 + fontName.Length * 2);
  196. stream.WriteUshort(payload_size);
  197. stream.WriteUshort(fontHeight);
  198. stream.WriteUshort(options);
  199. stream.WriteUshort(colourIdx);
  200. stream.WriteUshort(fontWeight);
  201. stream.WriteUshort(escapement);
  202. stream.WriteByte(underlineType);
  203. stream.WriteByte(fontFamily);
  204. stream.WriteByte(characterSet);
  205. stream.SkipBytes(1);
  206. stream.WriteUnicodeString(fontName, true);
  207. }
  208. public BIFF8_Font(string name, UInt16 height, UInt16 weight)
  209. {
  210. this.fontName = name;
  211. this.fontHeight = height;
  212. this.fontWeight = weight;
  213. }
  214. public BIFF8_Font(ExportIEMStyle style, ushort ColorIdx)
  215. {
  216. Font f = style.Font;
  217. this.fontName = f.Name;
  218. this.fontHeight = (ushort)(12 * Math.Round(f.Height * DrawUtils.ScreenDpiFX));
  219. this.fontWeight = (ushort)((f.Bold ? 70 : 12) * f.SizeInPoints);
  220. this.fontFamily = 0;
  221. this.colourIdx = ColorIdx;
  222. this.underlineType = (byte)(f.Underline ? 1 : 0);
  223. this.options = (ushort)((f.Italic ? 2 : 0) | (f.Bold ? 1 : 0));
  224. }
  225. }
  226. internal class BIFF8_Format
  227. {
  228. UInt16 format_index;
  229. string format_string;
  230. /*
  231. internal void Debug(StreamWriter debug, int idx)
  232. {
  233. debug.Write("\nFormat {0}", idx);
  234. debug.Write("\n\tIndex\t{0}", format_index);
  235. debug.Write("\n\tString\t{0}\n", format_string);
  236. }
  237. */
  238. internal BIFF8_Format(UInt16 format_index, string format_string)
  239. {
  240. this.format_index = format_index;
  241. this.format_string = format_string;
  242. }
  243. internal void Write(StreamHelper stream)
  244. {
  245. stream.WriteUshort(0x041e);
  246. ushort payload_size = (ushort)(2 + (ushort)stream.SizeUnicodeString(format_string, false));
  247. stream.WriteUshort(payload_size); // Size
  248. stream.WriteUshort(format_index);
  249. stream.WriteUnicodeString(format_string, false);
  250. }
  251. }
  252. internal class BIFF8_Hyperlink
  253. {
  254. // TODO: always has default value?
  255. internal ushort rwFirst = 0;
  256. internal ushort rwLast = 0;
  257. internal ushort colFirst = 0;
  258. internal ushort colLast = 0;
  259. internal uint optionFlags;
  260. string huperlink;
  261. bool is_url;
  262. private void Write_URL(StreamHelper stream)
  263. {
  264. byte[] GUID_Moniker = { 0xE0, 0xC9, 0xEA, 0x79, 0xF9, 0xBA, 0xCE, 0x11, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B };
  265. stream.WriteBytes(GUID_Moniker);
  266. uint calc_len = (uint)(huperlink.Length * 4);
  267. stream.WriteUint(calc_len);
  268. for (int i = 0; i < huperlink.Length; i++)
  269. {
  270. stream.WriteUshort((ushort)huperlink[i]);
  271. }
  272. stream.WriteUshort(0); // NULL-terminated
  273. }
  274. private void Write_HyperlinkToLocalFile(StreamHelper stream)
  275. {
  276. byte[] GUID_FileMoniker = { 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 };
  277. stream.WriteBytes(GUID_FileMoniker);
  278. // There is no way to make portable implementation of this function
  279. }
  280. internal void Write(StreamHelper stream)
  281. {
  282. byte[] GUID_StdLink = { 0xd0, 0xC9, 0xEA, 0x79, 0xF9, 0xBA, 0xCE, 0x11, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B };
  283. optionFlags = 0x3;
  284. ushort record_size = (ushort)(32 + (16 + 4 + huperlink.Length * 2 + 2));
  285. stream.WriteUshort(0x01b8); // Hyperlink
  286. stream.WriteUshort(record_size); // Record size
  287. stream.WriteUshort(rwFirst);
  288. stream.WriteUshort(rwLast);
  289. stream.WriteUshort(colFirst);
  290. stream.WriteUshort(colLast);
  291. stream.WriteBytes(GUID_StdLink);
  292. stream.WriteUint(2); // Unknown value defined in BIFF8 spec
  293. stream.WriteUint(optionFlags);
  294. Write_URL(stream);
  295. }
  296. internal BIFF8_Hyperlink(string URL)
  297. {
  298. this.huperlink = URL;
  299. is_url = URL.Contains("://");
  300. }
  301. }
  302. internal class BIFF8_ExtendedFormat
  303. {
  304. internal UInt16 font_index;
  305. internal UInt16 format_index;
  306. internal UInt16 xf_type;
  307. internal byte alignment_and_break;
  308. internal byte rotation;
  309. internal byte identation;
  310. internal byte flags;
  311. internal UInt32 borders;
  312. internal UInt32 attributes;
  313. UInt16 pattern_colors;
  314. private string BorderStyle(uint st, uint color_idx)
  315. {
  316. string name = "";
  317. switch (st)
  318. {
  319. case 0: name = "None"; break;
  320. case 1: name = "Thin"; break;
  321. case 2: name = "Medium"; break;
  322. case 3: name = "Dashed"; break;
  323. case 4: name = "Dotted"; break;
  324. case 5: name = "Thick"; break;
  325. case 6: name = "Double"; break;
  326. case 7: name = "Hair"; break;
  327. case 8: name = "MediumDashed"; break;
  328. case 10: name = "ThinDashDotted"; break;
  329. case 11: name = "MediumDashDotted"; break;
  330. case 12: name = "ThinDashDotDotted"; break;
  331. case 13: name = "MediumDashDotDotted"; break;
  332. case 14: name = "SlantedMediumDashDotted"; break;
  333. default: name = "Unknown "; break;
  334. }
  335. name = name + " " + color_idx.ToString();
  336. return name;
  337. }
  338. private string HorizontalAlign(int ha)
  339. {
  340. string text = "";
  341. switch (ha)
  342. {
  343. case 0: text = "General"; break;
  344. case 1: text = "Left"; break;
  345. case 2: text = "Centered"; break;
  346. case 3: text = "Right"; break;
  347. case 4: text = "Filled"; break;
  348. case 5: text = "Justified"; break;
  349. case 6: text = "CAS"; break; // Centred across selection
  350. case 7: text = "Distributed"; break;
  351. default: text = "Unknown"; break;
  352. }
  353. return text;
  354. }
  355. private string VerticalAlign(int va)
  356. {
  357. string text = "";
  358. switch (va)
  359. {
  360. case 0: text = "Top"; break;
  361. case 1: text = "Centered"; break;
  362. case 2: text = "Bottom"; break;
  363. case 3: text = "Justified"; break;
  364. case 4: text = "Distributed"; break;
  365. default: text = "Unknown"; break;
  366. }
  367. return text;
  368. }
  369. private string Pattern(uint patt)
  370. {
  371. string text = "";
  372. switch (patt)
  373. {
  374. case 0: text = "None"; break;
  375. case 1: text = "Solid"; break;
  376. case 2: text = "Chess"; break;
  377. case 3: text = "HorThick"; break;
  378. case 4: text = "VerThick"; break;
  379. case 5: text = "ChessThick"; break;
  380. case 6: text = "Hor"; break;
  381. case 7: text = "Ver"; break;
  382. case 8: text = "DiagBack"; break;
  383. case 9: text = "Diag"; break;
  384. case 10: text = "Cross"; break;
  385. case 11: text = "CrossDiag"; break;
  386. default: text = "Unknown"; break;
  387. }
  388. return text;
  389. }
  390. /*
  391. internal void Debug(StreamWriter debug, int idx)
  392. {
  393. debug.Write("\nXF {0}", idx);
  394. debug.Write("\n\tFormInd\t{0}", format_index);
  395. debug.Write("\n\tFontIdx\t{0}", font_index);
  396. debug.Write("\n\tType\t{0:X2}", xf_type);
  397. debug.Write("\n\tFlags\t{0:X}", flags);
  398. debug.Write("\n\tAlign\t{0:X} H={1} V={2} {3} {4}", alignment_and_break,
  399. HorizontalAlign(alignment_and_break & 0x03),
  400. VerticalAlign((alignment_and_break >> 4) & 0x03),
  401. (alignment_and_break & 0x8) == 0 ? "" : "WordWarp",
  402. (alignment_and_break & 0x80) == 0 ? "" : "Justify"
  403. );
  404. debug.Write("\n\tBorders\t{0:X4} L={1} R={2} T={3} B={4}", borders,
  405. BorderStyle(0xf & borders, 0xff & (borders >> 16)),
  406. BorderStyle(0xf & (borders >> 4), 0xff & (borders >> 23)),
  407. BorderStyle(0xf & (borders >> 8), 0xff & (attributes)),
  408. BorderStyle(0xf & (borders >> 12), 0xff & (attributes >> 7))
  409. );
  410. debug.Write("\n\tAttrib\t{0:X4} Pattern {1}", attributes,
  411. Pattern(attributes >> 26));
  412. debug.Write("\n\tPattern\t{0:X2}", pattern_colors);
  413. }
  414. */
  415. internal void Read(StreamHelper stream)
  416. {
  417. font_index = stream.ReadUshort();
  418. format_index = stream.ReadUshort();
  419. xf_type = stream.ReadUshort();
  420. alignment_and_break = (byte)stream.ReadByte();
  421. rotation = (byte)stream.ReadByte();
  422. identation = (byte)stream.ReadByte();
  423. flags = (byte)stream.ReadByte();
  424. borders = stream.ReadUint();
  425. attributes = stream.ReadUint();
  426. pattern_colors = stream.ReadUshort();
  427. }
  428. internal void Write(StreamHelper stream)
  429. {
  430. stream.WriteUshort(0x00e0); // XF extended format
  431. stream.WriteUshort(0x0014); // Record size
  432. stream.WriteUshort(font_index);
  433. stream.WriteUshort(format_index);
  434. stream.WriteUshort(xf_type);
  435. stream.WriteByte(alignment_and_break);
  436. stream.WriteByte(rotation);
  437. stream.WriteByte(identation);
  438. stream.WriteByte(flags);
  439. stream.WriteUint(borders);
  440. stream.WriteUint(attributes);
  441. stream.WriteUshort(pattern_colors);
  442. }
  443. internal BIFF8_ExtendedFormat()
  444. {
  445. this.xf_type = 0xfff5;
  446. this.flags = 0xf7;
  447. this.alignment_and_break = 0x9;
  448. }
  449. internal BIFF8_ExtendedFormat(
  450. ushort font_idx,
  451. ushort format_idx,
  452. ushort type,
  453. byte flags,
  454. byte align)
  455. {
  456. this.font_index = font_idx;
  457. this.format_index = format_idx;
  458. this.xf_type = type;
  459. this.flags = flags;
  460. this.alignment_and_break = align;
  461. }
  462. private byte GetAngle(ExportIEMStyle EStyle)
  463. {
  464. int angle = EStyle.Angle;
  465. if (angle != 0 && angle <= 90)
  466. {
  467. angle = 90 + angle;
  468. }
  469. else if (angle >= 270)
  470. {
  471. angle = 360 - angle;
  472. }
  473. else
  474. {
  475. angle = 0;
  476. }
  477. return (byte)angle;
  478. }
  479. private byte GetAlignment(ExportIEMStyle EStyle)
  480. {
  481. byte result = (byte)(EStyle.WordWrap == true ? 0x8 : 0x0);
  482. switch (EStyle.HAlign)
  483. {
  484. case HorzAlign.Center:
  485. result |= 2;
  486. break;
  487. case HorzAlign.Justify:
  488. result |= 5;
  489. break;
  490. case HorzAlign.Left:
  491. result |= 1;
  492. break;
  493. case HorzAlign.Right:
  494. result |= 3;
  495. break;
  496. }
  497. switch (EStyle.VAlign)
  498. {
  499. case VertAlign.Top:
  500. result |= (0 << 4);
  501. break;
  502. case VertAlign.Center:
  503. result |= (1 << 4);
  504. break;
  505. case VertAlign.Bottom:
  506. result |= (2 << 4);
  507. break;
  508. }
  509. return result;
  510. }
  511. private byte GetBorderLineStyle(BorderLine line)
  512. {
  513. int style = 0;
  514. switch (line.Style)
  515. {
  516. case LineStyle.Solid: style = line.Width <= 1 ? 1 : line.Width <= 3 ? 2 : 5; break;
  517. case LineStyle.Dash: style = line.Width <= 3 ? 3 : 8; break;
  518. case LineStyle.Dot: style = 4; break;
  519. case LineStyle.DashDot: style = line.Width <= 3 ? 10 : 11; break;
  520. case LineStyle.DashDotDot: style = line.Width <= 3 ? 12 : 13; break;
  521. case LineStyle.Double: style = 6; break;
  522. }
  523. return (byte)style;
  524. }
  525. private void GetBorders(Workbook workbook, ExportIEMStyle EStyle, out uint result, out uint attrib)
  526. {
  527. result = 0;
  528. attrib = 0;
  529. if ((EStyle.Border.Lines & BorderLines.Left) == BorderLines.Left)
  530. {
  531. result |= (uint)(GetBorderLineStyle(EStyle.Border.LeftLine)) | ((0x7f & (((uint)workbook.AddColor(EStyle.Border.LeftLine.Color)))) << 16);
  532. }
  533. if ((EStyle.Border.Lines & BorderLines.Right) == BorderLines.Right)
  534. {
  535. result |= (uint)(GetBorderLineStyle(EStyle.Border.RightLine) << 4) | ((uint)workbook.AddColor(EStyle.Border.RightLine.Color)) << 23; ;
  536. }
  537. if ((EStyle.Border.Lines & BorderLines.Top) == BorderLines.Top)
  538. {
  539. result |= ((uint)(GetBorderLineStyle(EStyle.Border.TopLine)) << 8);
  540. attrib |= 0x7f & ((uint)workbook.AddColor(EStyle.Border.TopLine.Color));
  541. }
  542. if ((EStyle.Border.Lines & BorderLines.Bottom) == BorderLines.Bottom)
  543. {
  544. result |= (uint)(GetBorderLineStyle(EStyle.Border.BottomLine) << 12);
  545. attrib |= ((uint)workbook.AddColor(EStyle.Border.BottomLine.Color)) << 7;
  546. }
  547. }
  548. private Color MixColors(Color s, Color e)
  549. {
  550. return Color.FromArgb((int)(s.A + e.A) / 2, (int)(s.R + e.R) / 2, (int)(s.G + e.G) / 2, (int)(s.B + e.B) / 2);
  551. }
  552. internal BIFF8_ExtendedFormat(
  553. bool style,
  554. ushort font_index,
  555. ExportIEMStyle EStyle,
  556. Workbook workbook,
  557. int indFormat
  558. )
  559. {
  560. this.font_index = font_index;
  561. switch (EStyle.Format.Name)
  562. {
  563. case "Currency":
  564. format_index = 7;
  565. break;
  566. case "Date":
  567. case "Number":
  568. format_index = (ushort)indFormat;
  569. break;
  570. case "General":
  571. default: format_index = 0; break;
  572. }
  573. this.alignment_and_break = this.GetAlignment(EStyle);
  574. this.rotation = this.GetAngle(EStyle);
  575. this.identation = 0;
  576. ushort fill_color = 0;
  577. if (style)
  578. {
  579. this.xf_type = 0xfff7;
  580. this.flags = 0x00; // 0xf7;
  581. }
  582. else
  583. {
  584. this.xf_type = 0x00;
  585. this.flags = 0xfc;
  586. }
  587. this.GetBorders(workbook, EStyle, out this.borders, out this.attributes);
  588. if (EStyle.Fill is SolidFill)
  589. {
  590. if (EStyle.FillColor.A != 0)
  591. {
  592. this.attributes |= (1 << 26);
  593. fill_color = workbook.AddColor(EStyle.FillColor);
  594. this.pattern_colors = (ushort)(fill_color | (ushort)(fill_color << 7));
  595. }
  596. }
  597. else if (EStyle.Fill is LinearGradientFill)
  598. {
  599. this.attributes |= (1 << 26);
  600. LinearGradientFill lgf = EStyle.Fill as LinearGradientFill;
  601. fill_color = workbook.AddColor(MixColors(lgf.StartColor, lgf.EndColor)); ;
  602. }
  603. else if (EStyle.Fill is PathGradientFill)
  604. {
  605. this.attributes |= (1 << 26);
  606. PathGradientFill pgf = EStyle.Fill as PathGradientFill;
  607. fill_color = workbook.AddColor(MixColors(pgf.CenterColor, pgf.EdgeColor)); ;
  608. }
  609. else if (EStyle.Fill is HatchFill)
  610. {
  611. this.attributes |= (1 << 26);
  612. HatchFill hf = EStyle.Fill as HatchFill;
  613. fill_color = workbook.AddColor(MixColors(hf.ForeColor, hf.BackColor)); ;
  614. }
  615. else if (EStyle.Fill is GlassFill)
  616. {
  617. this.attributes |= (1 << 26);
  618. GlassFill hf = EStyle.Fill as GlassFill;
  619. fill_color = workbook.AddColor(hf.Color); ;
  620. }
  621. else
  622. {
  623. this.attributes |= (1 << 26);
  624. fill_color = workbook.AddColor(Color.LightGray);
  625. }
  626. this.pattern_colors = (ushort)(fill_color | (ushort)(fill_color << 7));
  627. }
  628. }
  629. internal class BIFF8_Style
  630. {
  631. UInt16 style_attrib;
  632. string style_str;
  633. byte style_id;
  634. byte level;
  635. /*
  636. internal void Debug(StreamWriter debug, int idx)
  637. {
  638. debug.Write("\n\nStyles {0}", idx);
  639. debug.Write("\n\tID\t{0}", style_id);
  640. debug.Write("\n\tAttrib\t{0:X2}", style_attrib);
  641. debug.Write("\n\tLevel\t{0}", level);
  642. if (style_str != null)
  643. {
  644. debug.Write("\n\tName\t {0}", style_str);
  645. }
  646. debug.Write("\n");
  647. }
  648. */
  649. internal void Read(StreamHelper stream)
  650. {
  651. style_attrib = stream.ReadUshort();
  652. if (0 == (style_attrib & 0x8000))
  653. {
  654. style_str = stream.ReadUnicodeString(false);
  655. }
  656. else
  657. {
  658. style_id = (byte)stream.ReadByte();
  659. level = (byte)stream.ReadByte();
  660. }
  661. }
  662. internal void Write(StreamHelper stream)
  663. {
  664. stream.WriteUshort(0x0293);
  665. if (0 == (style_attrib & 0x8000))
  666. {
  667. ushort payload_size = (ushort)(2 + (ushort)stream.SizeUnicodeString(style_str, false));
  668. stream.WriteUshort(payload_size); // Size
  669. stream.WriteUshort(style_attrib);
  670. stream.WriteUnicodeString(style_str, false);
  671. }
  672. else
  673. {
  674. stream.WriteUshort(0x0004); // Size
  675. stream.WriteUshort(style_attrib);
  676. stream.WriteByte(style_id);
  677. stream.WriteByte(level);
  678. }
  679. }
  680. internal BIFF8_Style()
  681. {
  682. style_attrib = 0x8000;
  683. style_id = 0;
  684. level = 0xff;
  685. }
  686. internal BIFF8_Style(ushort idx, ExportIEMStyle style)
  687. {
  688. style_attrib = (ushort)(0x8000 + idx);
  689. style_id = 0; // Normal
  690. level = 0xff;
  691. }
  692. }
  693. internal class BIFF8_SharedStringTable
  694. {
  695. UInt32 total_number_strings;
  696. UInt32 number_following_strings;
  697. ArrayList shared_strings = new ArrayList();
  698. /*
  699. internal void Debug(StreamWriter debug)
  700. {
  701. debug.Write("\n\nShared trings");
  702. for (int i = 0; i < shared_strings.Count; i++)
  703. {
  704. debug.Write("\n\t{0}\t{1}", i, shared_strings[i]);
  705. }
  706. }
  707. */
  708. enum StringStates { Start, Work, Brake };
  709. internal int Read(StreamHelper stream, int RecordSize)
  710. {
  711. int total_size = (int)stream.Position;
  712. total_number_strings = stream.ReadUint();
  713. number_following_strings = stream.ReadUint();
  714. uint rest = number_following_strings;
  715. long stream_position = stream.Position;
  716. string str = "";
  717. int num_rich_runs = 0;
  718. StringStates state = StringStates.Start;
  719. UInt16 Char_Count = 0;
  720. byte grBit = 0;
  721. byte options;
  722. RecordSize -= 8;
  723. while (rest != 0)
  724. {
  725. switch (state)
  726. {
  727. case StringStates.Start:
  728. long tnp_position = stream.Position;
  729. Char_Count = (ushort)stream.ReadUshort();
  730. options = (byte)stream.ReadByte();
  731. if ((options & 0x8) != 0)
  732. num_rich_runs = stream.ReadUshort();
  733. grBit = (byte)(options & 0x1);
  734. if (Char_Count != 0)
  735. state = StringStates.Work;
  736. else
  737. {
  738. shared_strings.Add("");
  739. rest--;
  740. }
  741. RecordSize -= (int)(stream.Position - tnp_position);
  742. break;
  743. case StringStates.Brake:
  744. UInt16 RecordID = stream.ReadUshort();
  745. RecordSize = stream.ReadUshort();
  746. if (RecordID != 0x003c)
  747. {
  748. stream.Position -= 8;
  749. break;
  750. }
  751. grBit = (byte)stream.ReadByte();
  752. RecordSize--;
  753. state = StringStates.Work;
  754. break;
  755. case StringStates.Work:
  756. char ch;
  757. if (grBit == 0)
  758. {
  759. ch = (char)stream.ReadByte();
  760. RecordSize--;
  761. }
  762. else
  763. {
  764. ch = (char)stream.ReadUshort();
  765. RecordSize -= 2;
  766. }
  767. str += ch;
  768. Char_Count--;
  769. if (Char_Count == 0)
  770. {
  771. state = StringStates.Start;
  772. shared_strings.Add(str);
  773. str = "";
  774. rest--;
  775. }
  776. break;
  777. }
  778. if (RecordSize == 0)
  779. state = StringStates.Brake;
  780. }
  781. total_size = (int)stream.Position - total_size;
  782. return total_size;
  783. }
  784. internal void Write(StreamHelper stream)
  785. {
  786. ushort payload_size = 8;
  787. number_following_strings = (uint)shared_strings.Count;
  788. bool begin = true;
  789. long size_position;
  790. for (int index = 0; index < number_following_strings;)
  791. {
  792. if (begin)
  793. {
  794. stream.WriteUshort(0x00fc);
  795. size_position = stream.Position;
  796. stream.WriteUshort(0);
  797. stream.WriteUint(total_number_strings);
  798. stream.WriteUint(number_following_strings);
  799. begin = false;
  800. }
  801. else
  802. {
  803. stream.WriteUshort(0x003c);
  804. size_position = stream.Position;
  805. stream.WriteUshort(0);
  806. }
  807. while (payload_size < 8224)
  808. {
  809. if (index >= shared_strings.Count) break;
  810. ushort string_size = (ushort)stream.SizeUnicodeString((string)shared_strings[index], false);
  811. if (payload_size + string_size < 8224)
  812. {
  813. stream.WriteUnicodeString((string)shared_strings[index], false);
  814. payload_size += string_size;
  815. index++;
  816. }
  817. else
  818. {
  819. break;
  820. }
  821. }
  822. long curent_position = stream.Position;
  823. stream.Position = size_position;
  824. stream.WriteUshort(payload_size);
  825. payload_size = 0;
  826. stream.Position = curent_position;
  827. }
  828. }
  829. internal void Add(string str)
  830. {
  831. shared_strings.Add(str);
  832. }
  833. internal uint Count { get { return (uint)shared_strings.Count; } }
  834. }
  835. internal abstract class BIFF8_CellData
  836. {
  837. internal long stream_position;
  838. internal ushort cell_data_size;
  839. internal abstract void Read(StreamHelper stream);
  840. internal abstract void Write(StreamHelper stream);
  841. }
  842. internal class BIFF8_Blank : BIFF8_CellData
  843. {
  844. UInt16 blank_row_idx;
  845. UInt16 blank_col_idx;
  846. UInt16 blank_xf_idx;
  847. /*
  848. internal void Debug(StreamWriter debug, int idx)
  849. {
  850. debug.Write("\n\n\tBlank \t{0}", idx);
  851. debug.Write("\n\t\tRow\t{0}", blank_row_idx);
  852. debug.Write("\n\t\tCol\t{0}", blank_col_idx);
  853. debug.Write("\n\t\tBlankXF\t{0}", blank_xf_idx);
  854. debug.Write("\n");
  855. }
  856. */
  857. internal override void Read(StreamHelper stream)
  858. {
  859. stream_position = stream.Position;
  860. blank_row_idx = stream.ReadUshort();
  861. blank_col_idx = stream.ReadUshort();
  862. blank_xf_idx = stream.ReadUshort();
  863. cell_data_size = (ushort)(stream.Position - stream_position);
  864. }
  865. internal override void Write(StreamHelper stream)
  866. {
  867. stream_position = stream.Position;
  868. stream.WriteUshort(0x0201); // BLANK
  869. stream.WriteUshort(0x0006); // record size
  870. stream.WriteUshort(blank_row_idx);
  871. stream.WriteUshort(blank_col_idx);
  872. stream.WriteUshort(blank_xf_idx);
  873. cell_data_size = (ushort)(stream.Position - stream_position);
  874. }
  875. internal BIFF8_Blank() { }
  876. internal BIFF8_Blank(ushort row_idx, ushort col_idx, ushort xf_idx)
  877. {
  878. this.blank_row_idx = row_idx;
  879. this.blank_col_idx = col_idx;
  880. this.blank_xf_idx = xf_idx;
  881. }
  882. }
  883. internal class BIFF8_MulBlank : BIFF8_CellData
  884. {
  885. UInt16 blank_row;
  886. UInt16 first_blank_column;
  887. UInt16 last_blank_column;
  888. UInt16[] fx_indexes;
  889. UInt16 recordSize;
  890. /*
  891. internal void Debug(StreamWriter debug, int idx)
  892. {
  893. debug.Write("\n\n\tMulBlank \t{0}", idx);
  894. debug.Write("\n\t\tRow\t{0}", blank_row);
  895. debug.Write("\n\t\tFirstCol\t{0}", first_blank_column);
  896. debug.Write("\n\t\tKastCol\t{0}", last_blank_column);
  897. for (int i = 0; i < fx_indexes.Length; i++)
  898. debug.Write("\n\t\tfx[{0}]\t{1}", i, fx_indexes[i]);
  899. debug.Write("\n");
  900. }
  901. */
  902. internal override void Read(StreamHelper stream)
  903. {
  904. stream_position = stream.Position;
  905. blank_row = stream.ReadUshort();
  906. first_blank_column = stream.ReadUshort();
  907. int count = ((recordSize - sizeof(UInt16) * 3) >> 1);
  908. fx_indexes = new ushort[count];
  909. for (int i2fx = 0; i2fx < count; i2fx++)
  910. {
  911. fx_indexes[i2fx] = stream.ReadUshort();
  912. }
  913. last_blank_column = stream.ReadUshort();
  914. if (last_blank_column - first_blank_column + 1 != count)
  915. throw new Exception("BIFF8_MulBlank: file structue error");
  916. cell_data_size = (ushort)(stream.Position - stream_position);
  917. }
  918. internal override void Write(StreamHelper stream)
  919. {
  920. stream_position = stream.Position;
  921. ushort rec_size = (ushort)((fx_indexes.Length << 1) + sizeof(UInt16) * 3);
  922. stream.WriteUshort(0x00be); // MULBLANK
  923. stream.WriteUshort(rec_size); // record size
  924. stream.WriteUshort(blank_row);
  925. stream.WriteUshort(first_blank_column);
  926. for (int i2fx = 0; i2fx < fx_indexes.Length; i2fx++)
  927. {
  928. stream.WriteUshort(fx_indexes[i2fx]);
  929. }
  930. stream.WriteUshort(last_blank_column);
  931. cell_data_size = (ushort)(stream.Position - stream_position);
  932. }
  933. public BIFF8_MulBlank(UInt16 RecordSize)
  934. {
  935. this.recordSize = RecordSize;
  936. }
  937. public BIFF8_MulBlank(UInt16 blank_row, UInt16 first_blank_column, UInt16 last_blank_column, UInt16 fx_index)
  938. {
  939. int count = last_blank_column - first_blank_column + 1;
  940. this.blank_row = blank_row;
  941. this.first_blank_column = first_blank_column;
  942. this.last_blank_column = last_blank_column;
  943. this.fx_indexes = new ushort[count];
  944. for (int i = 0; i < count; i++)
  945. this.fx_indexes[i] = fx_index;
  946. }
  947. }
  948. internal class BIFF8_FloatNumber : BIFF8_CellData
  949. {
  950. ushort rw;
  951. ushort col;
  952. ushort ifxe;
  953. double rk;
  954. internal override void Read(StreamHelper stream)
  955. {
  956. stream_position = stream.Position;
  957. rw = stream.ReadUshort();
  958. col = stream.ReadUshort();
  959. ifxe = stream.ReadUshort();
  960. rk = stream.ReadDouble();
  961. }
  962. internal override void Write(StreamHelper stream)
  963. {
  964. stream.WriteUshort(0x0203);
  965. stream.WriteUshort(14); // record size
  966. stream.WriteUshort(rw);
  967. stream.WriteUshort(col);
  968. stream.WriteUshort(ifxe);
  969. stream.WriteDouble(rk);
  970. }
  971. internal BIFF8_FloatNumber() { }
  972. internal BIFF8_FloatNumber(ushort row_idx, ushort col_idx, ushort xf_idx, decimal obj)
  973. {
  974. this.rw = row_idx;
  975. this.col = col_idx;
  976. this.ifxe = xf_idx;
  977. this.rk = ((double)obj);
  978. }
  979. }
  980. internal class BIFF8_Number : BIFF8_CellData
  981. {
  982. UInt16 rw;
  983. UInt16 col;
  984. UInt16 ifxe;
  985. UInt32 rk;
  986. /*
  987. internal void Debug(StreamWriter debug, int idx)
  988. {
  989. debug.Write("\n\n\tNumber \t{0}", idx);
  990. debug.Write("\n\t\tRow\t{0}", rw);
  991. debug.Write("\n\t\tol\t{0}", col);
  992. debug.Write("\n\t\tXF\t{0}", ifxe);
  993. debug.Write("\n\t\tEncVal\t{0}", rk);
  994. debug.Write("\n");
  995. }
  996. */
  997. internal override void Read(StreamHelper stream)
  998. {
  999. rw = stream.ReadUshort();
  1000. col = stream.ReadUshort();
  1001. ifxe = stream.ReadUshort();
  1002. rk = stream.ReadUint();
  1003. }
  1004. internal override void Write(StreamHelper stream)
  1005. {
  1006. stream.WriteUshort(0x027e);
  1007. stream.WriteUshort(10); // record size
  1008. stream.WriteUshort(rw);
  1009. stream.WriteUshort(col);
  1010. stream.WriteUshort(ifxe);
  1011. stream.WriteUint(rk);
  1012. }
  1013. internal BIFF8_Number() { }
  1014. internal BIFF8_Number(ushort row_idx, ushort col_idx, ushort xf_idx, float obj)
  1015. {
  1016. rw = row_idx;
  1017. col = col_idx;
  1018. ifxe = xf_idx;
  1019. rk = (UInt32)obj;
  1020. rk <<= 2;
  1021. }
  1022. internal BIFF8_Number(ushort row_idx, ushort col_idx, ushort xf_idx, DateTime dt)
  1023. {
  1024. rw = row_idx;
  1025. col = col_idx;
  1026. ifxe = xf_idx;
  1027. DateTime centuryBegin = new DateTime(1899, 12, 30);
  1028. long elapsedTicks = dt.Ticks - centuryBegin.Ticks;
  1029. TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
  1030. rk = (uint)((elapsedSpan.Days << 2) | 2);
  1031. }
  1032. internal BIFF8_Number(ushort row_idx, ushort col_idx, ushort xf_idx, Decimal value)
  1033. {
  1034. rw = row_idx;
  1035. col = col_idx;
  1036. ifxe = xf_idx;
  1037. long date = (long)(value * 100);
  1038. rk = (UInt32)date;
  1039. rk <<= 2;
  1040. rk |= 3;
  1041. }
  1042. }
  1043. internal class FIFF8_CellRange : BIFF8_CellData
  1044. {
  1045. UInt16 first_row_idx;
  1046. UInt16 last_row_idx;
  1047. UInt16 first_col_idx;
  1048. UInt16 last_col_idx;
  1049. internal override void Read(StreamHelper stream)
  1050. {
  1051. first_row_idx = stream.ReadUshort();
  1052. last_row_idx = stream.ReadUshort();
  1053. first_col_idx = stream.ReadUshort();
  1054. last_col_idx = stream.ReadUshort();
  1055. }
  1056. internal override void Write(StreamHelper stream)
  1057. {
  1058. stream.WriteUshort(first_row_idx);
  1059. stream.WriteUshort(last_row_idx);
  1060. stream.WriteUshort(first_col_idx);
  1061. stream.WriteUshort(last_col_idx);
  1062. }
  1063. internal FIFF8_CellRange() { }
  1064. internal FIFF8_CellRange(UInt16 first_row, UInt16 last_row, UInt16 first_col, UInt16 last_col)
  1065. {
  1066. this.first_row_idx = first_row;
  1067. this.last_row_idx = last_row;
  1068. this.first_col_idx = first_col;
  1069. this.last_col_idx = last_col;
  1070. }
  1071. }
  1072. internal class BIFF8_LabelSST : BIFF8_CellData
  1073. {
  1074. internal UInt16 label_row_idx;
  1075. internal UInt16 label_col_idx;
  1076. internal UInt16 label_xf_idx;
  1077. internal UInt32 label_sst_idx;
  1078. /*
  1079. internal void Debug(StreamWriter debug, int idx)
  1080. {
  1081. debug.Write("\n\n\tLabel \t{0}", idx);
  1082. debug.Write("\n\t\tRow\t{0}", label_row_idx);
  1083. debug.Write("\n\t\tCol\t{0}", label_col_idx);
  1084. debug.Write("\n\t\tXF\t{0}", label_xf_idx);
  1085. debug.Write("\n\t\tSST_IDX\t{0}", label_sst_idx);
  1086. debug.Write("\n");
  1087. }
  1088. */
  1089. internal override void Read(StreamHelper stream)
  1090. {
  1091. stream_position = stream.Position;
  1092. label_row_idx = stream.ReadUshort();
  1093. label_col_idx = stream.ReadUshort();
  1094. label_xf_idx = stream.ReadUshort();
  1095. label_sst_idx = stream.ReadUint();
  1096. }
  1097. internal override void Write(StreamHelper stream)
  1098. {
  1099. stream_position = stream.Position;
  1100. stream.WriteUshort(0x00fd); // LABELSST
  1101. stream.WriteUshort(0x000a); // record size
  1102. stream.WriteUshort(label_row_idx);
  1103. stream.WriteUshort(label_col_idx);
  1104. stream.WriteUshort(label_xf_idx);
  1105. stream.WriteUint(label_sst_idx);
  1106. cell_data_size = (ushort)(stream.Position - stream_position);
  1107. }
  1108. internal BIFF8_LabelSST() { }
  1109. internal BIFF8_LabelSST(ushort row_idx, ushort col_idx, ushort xf_idx, uint sst_idx)
  1110. {
  1111. this.label_row_idx = row_idx;
  1112. this.label_col_idx = col_idx;
  1113. this.label_xf_idx = xf_idx;
  1114. this.label_sst_idx = sst_idx;
  1115. }
  1116. }
  1117. internal class BIFF8_Colinfo
  1118. {
  1119. internal UInt16 first_column;
  1120. internal UInt16 last_column;
  1121. internal UInt16 column_width;
  1122. internal UInt16 index_to_xf_recod;
  1123. internal UInt16 colinfo_options;
  1124. /*
  1125. internal void Debug(StreamWriter debug, int idx)
  1126. {
  1127. debug.Write("\n\nColinfo");
  1128. debug.Write("\n\tFirst\t{0}", first_column);
  1129. debug.Write("\n\tLast\t{0}", last_column);
  1130. debug.Write("\n\tWidth\t{0}", column_width);
  1131. debug.Write("\n\tXF\t{0}", index_to_xf_recod);
  1132. debug.Write("\n\tOptions\t{0:X2}", colinfo_options);
  1133. debug.Write("\n");
  1134. }
  1135. */
  1136. internal void Read(StreamHelper stream)
  1137. {
  1138. first_column = stream.ReadUshort();
  1139. last_column = stream.ReadUshort();
  1140. column_width = stream.ReadUshort();
  1141. index_to_xf_recod = stream.ReadUshort();
  1142. colinfo_options = stream.ReadUshort();
  1143. stream.SkipBytes(2);
  1144. }
  1145. internal void Write(StreamHelper stream)
  1146. {
  1147. stream.WriteUshort(0x007d); // COLINFO
  1148. stream.WriteUshort(0x000c); // payload size
  1149. stream.WriteUshort(first_column);
  1150. stream.WriteUshort(last_column);
  1151. stream.WriteUshort(column_width);
  1152. stream.WriteUshort(index_to_xf_recod);
  1153. stream.WriteUshort(colinfo_options);
  1154. stream.SkipBytes(2);
  1155. }
  1156. internal BIFF8_Colinfo()
  1157. {
  1158. }
  1159. internal BIFF8_Colinfo(ushort Column, ushort Width, ushort index_xf)
  1160. {
  1161. first_column = Column;
  1162. last_column = Column;
  1163. column_width = Width;
  1164. index_to_xf_recod = index_xf;
  1165. colinfo_options = 0;
  1166. }
  1167. }
  1168. internal class RowBlock : ArrayList
  1169. {
  1170. internal ArrayList cells = new ArrayList();
  1171. UInt16[] first_cell_offsets;
  1172. Int32 relative_offset_to_first_row;
  1173. internal void Read(StreamHelper stream)
  1174. {
  1175. int current_position = (int)stream.Position;
  1176. relative_offset_to_first_row = stream.ReadInt();
  1177. // int count_of_rows = (int)current_sheet.last_row - (int)current_sheet.first_row; // fix me
  1178. first_cell_offsets = stream.ReadUshorts(this.Count);
  1179. current_position -= relative_offset_to_first_row;
  1180. current_position += 0x14;
  1181. int row_index = 0;
  1182. for (row_index = 0; row_index < first_cell_offsets.Length;)
  1183. {
  1184. current_position += first_cell_offsets[row_index];
  1185. BIFF8_Row curr_row = this[row_index++] as BIFF8_Row;
  1186. int next_position;
  1187. if (row_index < first_cell_offsets.Length)
  1188. next_position = current_position + first_cell_offsets[row_index];
  1189. else
  1190. next_position = int.MaxValue;
  1191. foreach (BIFF8_CellData cell in cells)
  1192. {
  1193. if (cell.stream_position >= current_position && cell.stream_position < next_position)
  1194. {
  1195. curr_row.Add(cell);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. internal void Write(StreamHelper stream)
  1201. {
  1202. foreach (BIFF8_Row row in this)
  1203. {
  1204. row.Write(stream);
  1205. }
  1206. foreach (BIFF8_Row row in this)
  1207. {
  1208. foreach (BIFF8_CellData cell in row)
  1209. {
  1210. cell.Write(stream);
  1211. }
  1212. }
  1213. stream.WriteUshort(0x00d7); // DBCELL
  1214. ushort payload_size = (ushort)(4 + 2 * this.Count);
  1215. stream.WriteUshort(payload_size);
  1216. BIFF8_Row first_row = this[0] as BIFF8_Row;
  1217. uint first_row_offset = (uint)(stream.Position - first_row.last_position);
  1218. stream.WriteUint(first_row_offset);
  1219. ushort cell_offset = (ushort)(this.Count * 0x10);
  1220. foreach (BIFF8_Row row in this)
  1221. {
  1222. if (row.Count == 0) continue;
  1223. BIFF8_CellData first_cell = row[0] as BIFF8_CellData;
  1224. stream.WriteUshort(cell_offset);
  1225. cell_offset = first_cell.cell_data_size;
  1226. }
  1227. }
  1228. }
  1229. internal class BIFF8_Row : ArrayList
  1230. {
  1231. internal UInt16 row_index;
  1232. internal UInt16 first_col_index;
  1233. internal UInt16 last_col_index;
  1234. internal UInt16 row_height_bitfield;
  1235. internal UInt32 row_options;
  1236. internal int last_position;
  1237. /*
  1238. internal void Debug(StreamWriter debug, int idx)
  1239. {
  1240. debug.Write("\n\nRow");
  1241. debug.Write("\n\tIndex\t{0}", row_index);
  1242. debug.Write("\n\tFirst\t{0}", first_col_index);
  1243. debug.Write("\n\tLast\t{0}", last_col_index);
  1244. debug.Write("\n\tOptions\t{0:X4}", row_options);
  1245. debug.Write("\n\tHeight\t{0:X2}", row_height_bitfield);
  1246. debug.Write("\n");
  1247. }
  1248. */
  1249. internal void Read(StreamHelper stream)
  1250. {
  1251. row_index = stream.ReadUshort();
  1252. first_col_index = stream.ReadUshort();
  1253. last_col_index = stream.ReadUshort();
  1254. row_height_bitfield = stream.ReadUshort();
  1255. stream.SkipBytes(4);
  1256. row_options = stream.ReadUint();
  1257. last_position = (int)stream.Position;
  1258. }
  1259. internal void Write(StreamHelper stream)
  1260. {
  1261. stream.WriteUshort(0x0208); // ROW
  1262. stream.WriteUshort(0x0010); // record size
  1263. stream.WriteUshort(row_index);
  1264. stream.WriteUshort(first_col_index);
  1265. stream.WriteUshort(last_col_index);
  1266. stream.WriteUshort(row_height_bitfield);
  1267. stream.SkipBytes(4);
  1268. stream.WriteUint(row_options);
  1269. last_position = (int)stream.Position;
  1270. }
  1271. internal BIFF8_Row() { }
  1272. internal BIFF8_Row(
  1273. ushort row_index,
  1274. ushort first_col_index,
  1275. ushort last_col_index,
  1276. ushort row_height_bitfield,
  1277. uint row_options)
  1278. {
  1279. this.row_index = row_index;
  1280. this.first_col_index = first_col_index;
  1281. this.last_col_index = last_col_index;
  1282. this.row_height_bitfield = row_height_bitfield;
  1283. this.row_options = row_options;
  1284. }
  1285. }
  1286. internal class BIFF8_Sheet : ArrayList
  1287. {
  1288. // Sheet definiton in global section
  1289. UInt32 absolute_position;
  1290. byte sheet_state;
  1291. byte sheet_type;
  1292. string sheet_name;
  1293. // Sheet index
  1294. internal UInt32 first_row;
  1295. internal UInt32 last_row;
  1296. UInt32 def_col_width_ptr;
  1297. UInt32[] db_cell_pos;
  1298. // Dimension
  1299. UInt32 first_used_row;
  1300. UInt32 last_used_row;
  1301. UInt16 first_used_column;
  1302. UInt16 last_used_column;
  1303. // Window 2
  1304. UInt16 window_options;
  1305. UInt16 first_visible_row;
  1306. UInt16 first_visible_col;
  1307. UInt16 grid_line_color_idx;
  1308. UInt16 page_break_magnification;
  1309. UInt16 normal_magnification;
  1310. // PAGESETUP
  1311. UInt16 paper_size;
  1312. UInt16 scaling_factor;
  1313. UInt16 start_page_number;
  1314. UInt16 fit_workshit_width;
  1315. UInt16 fit_workshit_height;
  1316. UInt16 page_options;
  1317. UInt16 print_horizontal_dpi;
  1318. UInt16 print_vertical_dpi;
  1319. double header_margin;
  1320. double footer_margin;
  1321. UInt16 print_copies_count;
  1322. /* +++++++++++++++++++++++++++++++++++++++++ */
  1323. internal UInt16 hcenter;
  1324. internal UInt16 vcenter;
  1325. internal double lmargin;
  1326. internal double rmargin;
  1327. internal double umargin;
  1328. internal double bmargin;
  1329. /* +++++++++++++++++++++++++++++++++++++++++ */
  1330. internal long sheet_position_index;
  1331. /* +++++++++++++++++++++++++++++++++++++++++ */
  1332. internal ArrayList colinfo_list = new ArrayList();
  1333. internal ArrayList cell_ranges = new ArrayList();
  1334. internal ArrayList url_list = new ArrayList();
  1335. /*
  1336. internal void Debug(StreamWriter debug, int idx)
  1337. {
  1338. debug.Write("\nSheet {0}", sheet_name);
  1339. }
  1340. */
  1341. /* ++++++++++++ Picture goes here ++++++++++++ */
  1342. internal ArrayList pictures_list = new ArrayList();
  1343. internal void Read_Globals(StreamHelper stream)
  1344. {
  1345. absolute_position = stream.ReadUint();
  1346. sheet_state = (byte)stream.ReadByte();
  1347. sheet_type = (byte)stream.ReadByte();
  1348. sheet_name = stream.ReadUnicodeString(true);
  1349. }
  1350. internal void Write_Globals(StreamHelper stream)
  1351. {
  1352. stream.WriteUshort(0x0085); // SHEET
  1353. ushort payload_size = (ushort)(8 + sheet_name.Length * 2);
  1354. stream.WriteUshort(payload_size); // Payload size
  1355. this.sheet_position_index = stream.Position;
  1356. stream.WriteUint(absolute_position);
  1357. stream.WriteByte(sheet_state);
  1358. stream.WriteByte(sheet_type);
  1359. stream.WriteUnicodeString(sheet_name, true);
  1360. }
  1361. internal void Read_Index(StreamHelper stream)
  1362. {
  1363. stream.SkipBytes(4); // Not used in BIFF8
  1364. first_row = stream.ReadUint();
  1365. last_row = stream.ReadUint();
  1366. def_col_width_ptr = stream.ReadUint();
  1367. if (last_row == first_row)
  1368. {
  1369. return;
  1370. }
  1371. UInt32 nm = (last_row - first_row - 1) / 32 + 1;
  1372. db_cell_pos = new UInt32[nm];
  1373. for (int i = 0; i < nm; i++)
  1374. {
  1375. db_cell_pos[i] = stream.ReadUint();
  1376. }
  1377. }
  1378. internal void Write_Index(StreamHelper stream)
  1379. {
  1380. stream.WriteUshort(0x020b); // INDEX
  1381. if (last_row == first_row)
  1382. {
  1383. stream.WriteUshort(0x0010); // Payload size
  1384. stream.SkipBytes(4); // Not used in BIFF8
  1385. stream.WriteUint(first_row);
  1386. stream.WriteUint(last_row);
  1387. stream.WriteUint(def_col_width_ptr);
  1388. return;
  1389. }
  1390. UInt32 nm = (last_row - first_row - 1) / 32 + 1;
  1391. stream.WriteUshort((ushort)(0x0010 + nm * sizeof(UInt32))); // Payload size
  1392. stream.SkipBytes(4); // Not used in BIFF8
  1393. stream.WriteUint(first_row);
  1394. stream.WriteUint(last_row);
  1395. stream.WriteUint(def_col_width_ptr);
  1396. if (last_row == first_row)
  1397. {
  1398. return;
  1399. }
  1400. db_cell_pos = new UInt32[nm];
  1401. for (int i = 0; i < nm; i++)
  1402. {
  1403. stream.WriteUint(db_cell_pos[i]);
  1404. }
  1405. }
  1406. internal void Write_Margins(StreamHelper stream)
  1407. {
  1408. stream.WriteUshort(0x0026); // LMARGIN
  1409. stream.WriteUshort(8);
  1410. stream.WriteDouble(this.lmargin);
  1411. stream.WriteUshort(0x0027); // RMARGIN
  1412. stream.WriteUshort(8);
  1413. stream.WriteDouble(this.rmargin);
  1414. stream.WriteUshort(0x0028); // TMARGIN
  1415. stream.WriteUshort(8);
  1416. stream.WriteDouble(this.umargin);
  1417. stream.WriteUshort(0x0029); // BMARGIN
  1418. stream.WriteUshort(8);
  1419. stream.WriteDouble(this.bmargin);
  1420. }
  1421. internal void Write_PageBreaks(StreamHelper stream, ExportMatrix matrix)
  1422. {
  1423. List<int> breaks = new List<int>();
  1424. for (int i = 0; i <= matrix.Height - 1; i++)
  1425. if (matrix.YPosById(i) >= matrix.PageBreak(breaks.Count))
  1426. {
  1427. breaks.Add(i);
  1428. }
  1429. if (breaks.Count != 0)
  1430. {
  1431. stream.WriteUshort(0x001b); // // HORIZONTALPAGEBREAKS:
  1432. stream.WriteUshort((ushort)(2 + 6 * breaks.Count));
  1433. stream.WriteUshort((ushort)breaks.Count);
  1434. for (int j = 0; j < breaks.Count; j++)
  1435. {
  1436. stream.WriteUshort((ushort)breaks[j]);
  1437. stream.WriteUshort(0);
  1438. stream.WriteUshort(16384);
  1439. }
  1440. }
  1441. }
  1442. internal void Read_PageSetup(StreamHelper stream)
  1443. {
  1444. paper_size = stream.ReadUshort();
  1445. scaling_factor = stream.ReadUshort();
  1446. start_page_number = stream.ReadUshort();
  1447. fit_workshit_width = stream.ReadUshort();
  1448. fit_workshit_height = stream.ReadUshort();
  1449. page_options = stream.ReadUshort();
  1450. print_horizontal_dpi = stream.ReadUshort();
  1451. print_vertical_dpi = stream.ReadUshort();
  1452. header_margin = stream.ReadDouble();
  1453. footer_margin = stream.ReadDouble();
  1454. print_copies_count = stream.ReadUshort();
  1455. }
  1456. internal void Write_PageSetup(StreamHelper stream)
  1457. {
  1458. stream.WriteUshort(0x00a1);
  1459. stream.WriteUshort(0x0022);
  1460. stream.WriteUshort(paper_size);
  1461. stream.WriteUshort(scaling_factor);
  1462. stream.WriteUshort(start_page_number);
  1463. stream.WriteUshort(fit_workshit_width);
  1464. stream.WriteUshort(fit_workshit_height);
  1465. stream.WriteUshort(page_options);
  1466. stream.WriteUshort(print_horizontal_dpi);
  1467. stream.WriteUshort(print_vertical_dpi);
  1468. stream.WriteDouble(header_margin);
  1469. stream.WriteDouble(footer_margin);
  1470. stream.WriteUshort(print_copies_count);
  1471. }
  1472. internal void Read_Dimension(StreamHelper stream)
  1473. {
  1474. first_used_row = stream.ReadUint();
  1475. last_used_row = stream.ReadUint();
  1476. first_used_column = stream.ReadUshort();
  1477. last_used_column = stream.ReadUshort();
  1478. stream.SkipBytes(2);
  1479. }
  1480. internal void Write_Dimension(StreamHelper stream)
  1481. {
  1482. stream.WriteUshort(0x0200);
  1483. stream.WriteUshort(0x000e);
  1484. stream.WriteUint(first_used_row);
  1485. //int val = 0;
  1486. //// debug
  1487. //last_used_row = (uint) val;
  1488. //last_used_column = (ushort) val;
  1489. stream.WriteUint(last_used_row);
  1490. stream.WriteUshort(first_used_column);
  1491. stream.WriteUshort(last_used_column);
  1492. stream.SkipBytes(2);
  1493. }
  1494. internal void Read_Window2(StreamHelper stream)
  1495. {
  1496. window_options = stream.ReadUshort();
  1497. first_visible_row = stream.ReadUshort();
  1498. first_visible_col = stream.ReadUshort();
  1499. grid_line_color_idx = stream.ReadUshort();
  1500. stream.SkipBytes(2);
  1501. page_break_magnification = stream.ReadUshort();
  1502. normal_magnification = stream.ReadUshort();
  1503. stream.SkipBytes(4);
  1504. }
  1505. internal void Write_Window2(StreamHelper stream)
  1506. {
  1507. stream.WriteUshort(0x023e);
  1508. stream.WriteUshort(0x0012);
  1509. stream.WriteUshort(window_options);
  1510. stream.WriteUshort(first_visible_row);
  1511. stream.WriteUshort(first_visible_col);
  1512. stream.WriteUshort(grid_line_color_idx);
  1513. stream.SkipBytes(2);
  1514. stream.WriteUshort(page_break_magnification);
  1515. stream.WriteUshort(normal_magnification);
  1516. stream.SkipBytes(4);
  1517. }
  1518. internal void Read_Ranges(StreamHelper stream)
  1519. {
  1520. UInt16 range_count_follow = stream.ReadUshort();
  1521. for (int range_idx = 0; range_idx < range_count_follow; range_idx++)
  1522. {
  1523. FIFF8_CellRange single_range = new FIFF8_CellRange();
  1524. single_range.Read(stream);
  1525. cell_ranges.Add(single_range);
  1526. }
  1527. }
  1528. internal void Write_Ranges(StreamHelper stream)
  1529. {
  1530. int counter = cell_ranges.Count;
  1531. int record_count = counter / 1027;
  1532. if ((counter % 1027) != 0) record_count++;
  1533. int item_count;
  1534. int range_idx = 0;
  1535. for (int i = 0; i < record_count; i++)
  1536. {
  1537. item_count = (counter <= 1027) ? counter : 1027;
  1538. ushort payload_size = (ushort)(sizeof(UInt16) * (1 + item_count * 4));
  1539. stream.WriteUshort(0x00e5);
  1540. stream.WriteUshort(payload_size);
  1541. stream.WriteUshort((ushort)item_count);
  1542. for (int j = 0; j < item_count; j++)
  1543. {
  1544. FIFF8_CellRange single_range = cell_ranges[range_idx++] as FIFF8_CellRange;
  1545. single_range.Write(stream);
  1546. counter--;
  1547. }
  1548. }
  1549. }
  1550. internal void Write_URLs(StreamHelper stream)
  1551. {
  1552. foreach (BIFF8_Hyperlink hlink in url_list)
  1553. hlink.Write(stream);
  1554. }
  1555. internal void Write(Workbook stream, bool page_breaks, ExportMatrix matrix)
  1556. {
  1557. long store_index = stream.Position;
  1558. stream.Position = sheet_position_index;
  1559. stream.WriteUint((uint)store_index);
  1560. stream.Position = store_index;
  1561. stream.Write_BOF(0x0010); // Sheet
  1562. // stream.Write_Index(this);
  1563. if (page_breaks)
  1564. this.Write_PageBreaks(stream, matrix);
  1565. this.Write_Margins(stream);
  1566. this.Write_PageSetup(stream);
  1567. foreach (BIFF8_Colinfo column in this.colinfo_list)
  1568. {
  1569. column.Write(stream);
  1570. }
  1571. this.Write_Dimension(stream);
  1572. if (this.Count != 0)
  1573. {
  1574. foreach (RowBlock row_block in this)
  1575. {
  1576. row_block.Write(stream);
  1577. }
  1578. }
  1579. this.Write_Drawings(stream);
  1580. this.Write_Window2(stream);
  1581. this.Write_Ranges(stream);
  1582. this.Write_URLs(stream);
  1583. stream.Write_EOF(); // Sheet
  1584. }
  1585. private void Write_Drawings(StreamHelper stream)
  1586. {
  1587. bool is_drawing_group = true;
  1588. uint total_size = 0;
  1589. long current_stream_position;
  1590. BIFF8_ExcelDrawing first_drawing = null;
  1591. foreach (BIFF8_ExcelDrawing drawing in this.pictures_list)
  1592. {
  1593. total_size += drawing.Write(stream, is_drawing_group);
  1594. if (is_drawing_group)
  1595. {
  1596. first_drawing = drawing;
  1597. is_drawing_group = false;
  1598. }
  1599. drawing.Write_GraphicsObject(stream);
  1600. }
  1601. if (first_drawing != null)
  1602. {
  1603. current_stream_position = stream.Position;
  1604. stream.Position = first_drawing.total_spgr_size_position;
  1605. stream.WriteUint(total_size);
  1606. stream.Position = first_drawing.total_dg_size_position;
  1607. stream.WriteUint(total_size + 24);
  1608. stream.Position = current_stream_position;
  1609. }
  1610. }
  1611. internal BIFF8_Sheet()
  1612. {
  1613. }
  1614. internal BIFF8_Sheet(string Name, bool hideGridLines)
  1615. {
  1616. this.sheet_name = Name;
  1617. this.absolute_position = 0x3a7;
  1618. scaling_factor = 0x0064;
  1619. start_page_number = 0x0000;
  1620. fit_workshit_width = 0x0001;
  1621. fit_workshit_height = 0x0001;
  1622. page_options = 0x0482;
  1623. print_horizontal_dpi = 300;
  1624. print_vertical_dpi = 300;
  1625. header_margin = 0.0;
  1626. footer_margin = 0.0;
  1627. print_copies_count = 1;
  1628. window_options = 0x06b6;
  1629. first_visible_row = 0;
  1630. first_visible_col = 0;
  1631. grid_line_color_idx = 0x0040;
  1632. page_break_magnification = 0;
  1633. normal_magnification = 0;
  1634. if (hideGridLines)
  1635. window_options ^= 2;
  1636. }
  1637. internal RowBlock AddRow(BIFF8_Row row)
  1638. {
  1639. RowBlock row_block = null;
  1640. do
  1641. {
  1642. if (this.Count != 0)
  1643. {
  1644. row_block = this[this.Count - 1] as RowBlock;
  1645. if (row_block.Count < 32) break;
  1646. }
  1647. row_block = new RowBlock();
  1648. this.Add(row_block);
  1649. } while (false);
  1650. row_block.Add(row);
  1651. return row_block;
  1652. }
  1653. }
  1654. internal class Workbook : BIFF8_Stream
  1655. {
  1656. private static uint paletteSize = 0x40;
  1657. private ArrayList font_list = new ArrayList();
  1658. private ArrayList format_list = new ArrayList();
  1659. private ArrayList xf_list = new ArrayList();
  1660. private ArrayList styles = new ArrayList();
  1661. private ArrayList sheet_list = new ArrayList();
  1662. private BIFF8_SharedStringTable shared_strings = new BIFF8_SharedStringTable();
  1663. private BIFF8_Window1 window1 = new BIFF8_Window1();
  1664. private RowBlock row_block;
  1665. private Hashtable pic_list = new Hashtable();
  1666. internal bool hideGridLines;
  1667. BIFF8_ExcelDrawing drawing;
  1668. // palette
  1669. int palUsed = 0;
  1670. int[] palette = new int[paletteSize];
  1671. BIFF8_MSODrawingGroup drawing_group;
  1672. // StreamWriter debug;
  1673. private int ColorDiff(int color1, int color2)
  1674. {
  1675. color1 ^= color2;
  1676. int res = 0;
  1677. for (int i = 0; i < 4; i++)
  1678. {
  1679. res += color1 & 0xff;
  1680. color1 = color1 >> 8;
  1681. }
  1682. return res;
  1683. }
  1684. private int NearestColor(int[] colors, int firstIndex, int color)
  1685. {
  1686. int res = firstIndex;
  1687. int resDiff = ColorDiff(colors[res], color);
  1688. for (int i = firstIndex + 1; i < colors.Length; i++)
  1689. {
  1690. int diff = ColorDiff(colors[i], color);
  1691. if (diff < resDiff)
  1692. {
  1693. res = i;
  1694. resDiff = diff;
  1695. }
  1696. }
  1697. return res;
  1698. }
  1699. internal ushort AddColor(Color color, bool notFind)
  1700. {
  1701. return AddColor((color.A << 24) | (color.B << 16) | (color.G << 8) | color.R, notFind);
  1702. }
  1703. internal ushort AddColor(Color color)
  1704. {
  1705. return AddColor((color.A << 24) | (color.B << 16) | (color.G << 8) | color.R, false);
  1706. }
  1707. internal ushort AddColor(int color)
  1708. {
  1709. return AddColor(color, false);
  1710. }
  1711. internal ushort AddColor(int color, bool notFind)
  1712. {
  1713. color &= 0xffffff;
  1714. int i;
  1715. if (palUsed < 8)
  1716. i = -1;
  1717. else
  1718. i = notFind ? palUsed : NearestColor(palette, 7, color);
  1719. // If an exact match found, return the index.
  1720. if (!notFind)
  1721. {
  1722. if (i >= 0 && palette[i] == color)
  1723. return (ushort)i;
  1724. // If no exact match found but palette is full,
  1725. // return an index to the nearest color.
  1726. if (i >= 0 && palUsed == paletteSize)
  1727. return (ushort)i;
  1728. }
  1729. // If no exact match found and the palette is not full,
  1730. // add the new color.
  1731. //
  1732. // alman's fix
  1733. if (palUsed == paletteSize)
  1734. return (ushort)NearestColor(palette, 7, color);
  1735. palette[palUsed] = color;
  1736. return (ushort)palUsed++;
  1737. }
  1738. private void Write_Palette()
  1739. {
  1740. WriteUshort(0x0092); // palette id
  1741. WriteUshort(56 * 4 + 2); // payload size
  1742. WriteUshort(56); // palette size
  1743. WriteInts(palette, 8); // palette data
  1744. }
  1745. internal void Write_BOF(UInt16 type)
  1746. {
  1747. WriteUshort(0x0809); // BOF
  1748. WriteUshort(0x0010); // Recod size
  1749. WriteUshort(0x0600); // Version
  1750. WriteUshort(type); // Type
  1751. WriteUshort(0x1faa); // Build ID
  1752. WriteUshort(0x07cd); // Build year
  1753. WriteUint(0x000100c1); // History flags
  1754. WriteUint(0x00000406); // Lovest version
  1755. }
  1756. internal void PrintUshortOption(UInt16 RecordID, UInt16 value)
  1757. {
  1758. WriteUshort(RecordID); // RecordID
  1759. WriteUshort(0x0002); // Payload size
  1760. WriteUshort(value); // Value
  1761. }
  1762. private void Write_1904()
  1763. {
  1764. WriteUshort(0x0022); // DATE mode
  1765. WriteUshort(0x0002); // Payload size
  1766. WriteUshort(0x0000); // False
  1767. }
  1768. internal void Write_EOF()
  1769. {
  1770. WriteUshort(0x000a); // END OF FILE
  1771. WriteUshort(0x0000); // No payload for this command
  1772. }
  1773. public void Read(DirectoryEntry entry)
  1774. {
  1775. if (entry.type != DirectoryEntry.DirEntryType.UseStream) throw new Exception("Entry is not User stream");
  1776. //debug = new StreamWriter(@"C:\Users\alman\Desktop\Support\ReadBiffGen.txt");
  1777. int BytesCount = (int)entry.size;
  1778. int debug_size;
  1779. int count_of_row_records = 0;
  1780. int Sheet_Index = -1;
  1781. BIFF8_Sheet current_sheet = null;
  1782. ArrayList cells = new ArrayList();
  1783. do
  1784. {
  1785. UInt16 RecordID = ReadUshort();
  1786. int RecordSize = ReadUshort();
  1787. BytesCount -= 4;
  1788. debug_size = (int)this.Position;
  1789. switch (RecordID)
  1790. {
  1791. case 0x0809: // BOF
  1792. UInt16 version = ReadUshort();
  1793. UInt16 type = ReadUshort();
  1794. UInt16 build_id = ReadUshort();
  1795. UInt16 build_year = ReadUshort();
  1796. UInt32 history_flafs = ReadUint();
  1797. UInt32 lovest_ver = ReadUint();
  1798. if (type == 0x0010)
  1799. {
  1800. Sheet_Index++;
  1801. current_sheet = this.sheet_list[Sheet_Index] as BIFF8_Sheet;
  1802. }
  1803. break;
  1804. case 0x00e1: // INTERFACE
  1805. UInt16 i_codepage = ReadUshort();
  1806. break;
  1807. case 0x00c1: // MMS
  1808. UInt16 mms = ReadUshort();
  1809. break;
  1810. case 0x00e2: // INTERFACEEND
  1811. break;
  1812. case 0x005c: // WRITEACCESS
  1813. SkipBytes(RecordSize);
  1814. break;
  1815. case 0x0042:
  1816. UInt16 codepage = ReadUshort();
  1817. break;
  1818. case 0x0161: // Double stream flag
  1819. UInt16 double_stream = ReadUshort();
  1820. break;
  1821. case 0x01c0: // ????
  1822. SkipBytes(RecordSize);
  1823. break;
  1824. case 0x13d: // RRD
  1825. SkipBytes(RecordSize);
  1826. break;
  1827. case 0x009c:
  1828. SkipBytes(RecordSize);
  1829. break;
  1830. case 0x0019: // WINDOWPOTECT
  1831. UInt16 protect_val = ReadUshort();
  1832. break;
  1833. case 0x0012: // PROTCT
  1834. UInt16 protect_modify = ReadUshort();
  1835. break;
  1836. case 0x0013: // PASSWORD
  1837. UInt16 pass_hash = ReadUshort();
  1838. break;
  1839. case 0x1af: // PROTREV
  1840. UInt16 prot_rev = ReadUshort();
  1841. break;
  1842. case 0x01bc: // PROTREVPASS
  1843. UInt16 prot_rev_pass = ReadUshort();
  1844. break;
  1845. case 0x003d: // WINDOW1
  1846. window1.Read(this);
  1847. break;
  1848. case 0x0040: // BACKUP
  1849. UInt16 create_backup = ReadUshort();
  1850. break;
  1851. case 0x008d: // HIDEOBJ
  1852. UInt16 hide_object = ReadUshort();
  1853. break;
  1854. case 0x0022: // DATAMODE
  1855. UInt16 data_mode = ReadUshort();
  1856. break;
  1857. case 0x000e: // PRECISION
  1858. UInt16 precision = ReadUshort();
  1859. break;
  1860. case 0x01b7: // REFRESHALL
  1861. UInt16 refreshall = ReadUshort();
  1862. break;
  1863. case 0x00da: // BOOKBOOL is "save external values?"
  1864. UInt16 book_bool = ReadUshort();
  1865. break;
  1866. case 0x0031: // FONT
  1867. BIFF8_Font font = new BIFF8_Font("", 0, 0);
  1868. font.Read(this);
  1869. //#if DEBUG
  1870. // font.Debug(debug, font_list.Count);
  1871. //#endif
  1872. font_list.Add(font);
  1873. break;
  1874. case 0x041e: // Format
  1875. UInt16 format_idx = ReadUshort();
  1876. string format_str = ReadUnicodeString(false);
  1877. BIFF8_Format format = new BIFF8_Format(format_idx, format_str);
  1878. //#if DEBUG
  1879. // format.Debug(debug, format_list.Count);
  1880. //#endif
  1881. format_list.Add(format);
  1882. break;
  1883. case 0x00e0: // XF extended format
  1884. BIFF8_ExtendedFormat xf_format = new BIFF8_ExtendedFormat();
  1885. xf_format.Read(this);
  1886. //#if DEBUG
  1887. // xf_format.Debug(debug, this.xf_list.Count);
  1888. //#endif
  1889. this.xf_list.Add(xf_format);
  1890. break;
  1891. case 0x87c: // Something unknown
  1892. case 0x87d: // Something unknown
  1893. SkipBytes(RecordSize);
  1894. break;
  1895. case 0x0293: // STYLE
  1896. BIFF8_Style style = new BIFF8_Style();
  1897. style.Read(this);
  1898. //#if DEBUG
  1899. // style.Debug(debug, this.styles.Count);
  1900. //#endif
  1901. this.styles.Add(style);
  1902. break;
  1903. case 0x0892: // Style payload
  1904. SkipBytes(RecordSize);
  1905. break;
  1906. case 0x088e: // ???????
  1907. SkipBytes(RecordSize);
  1908. break;
  1909. case 0x0160: // USESELFS
  1910. UInt16 natural_lang_formulas = ReadUshort();
  1911. break;
  1912. case 0x0085: // SHEET
  1913. BIFF8_Sheet sheet = new BIFF8_Sheet();
  1914. sheet.Read_Globals(this);
  1915. //#if DEBUG
  1916. // sheet.Debug(debug, this.sheet_list.Count);
  1917. //#endif
  1918. this.sheet_list.Add(sheet);
  1919. break;
  1920. case 0x089a:
  1921. case 0x08a3:
  1922. SkipBytes(RecordSize);
  1923. break;
  1924. case 0x008c:
  1925. UInt16 interface_language = ReadUshort();
  1926. UInt16 system_region = ReadUshort();
  1927. break;
  1928. case 0x01c1:
  1929. SkipBytes(RecordSize);
  1930. break;
  1931. case 0x00eb: // MSODRAWINGGROUP
  1932. drawing_group = new BIFF8_MSODrawingGroup();
  1933. int tmp = drawing_group.Read(this, RecordSize);
  1934. RecordSize = tmp;
  1935. break;
  1936. case 0x00fc: // Shared string table
  1937. int tmp2 = shared_strings.Read(this, RecordSize);
  1938. RecordSize = tmp2;
  1939. break;
  1940. case 0x00ff: // Extended SST
  1941. ushort strings_in_bucket = ReadUshort();
  1942. int payload_size = RecordSize - sizeof(ushort);
  1943. while (payload_size > 0)
  1944. {
  1945. uint stream_pos = ReadUint();
  1946. ushort sst_offset = ReadUshort();
  1947. SkipBytes(2);
  1948. payload_size -= 8;
  1949. }
  1950. break;
  1951. case 0x0863: // Extra Book Info
  1952. SkipBytes(RecordSize);
  1953. break;
  1954. case 0x0896: // Theme
  1955. case 0x089B: // COMPRESSPICTURES
  1956. case 0x088C: // Compatibility Checker 12
  1957. SkipBytes(RecordSize);
  1958. break;
  1959. case 0x000a: // EOF
  1960. break;
  1961. // Sheet properties here
  1962. case 0x020b: // INDEX
  1963. current_sheet.Read_Index(this);
  1964. break;
  1965. case 0x000d: //CALCMODE
  1966. UInt16 calc_mode = ReadUshort();
  1967. break;
  1968. case 0x000c: // CALCCOUNT
  1969. UInt16 calc_count = ReadUshort();
  1970. break;
  1971. case 0x000f: // REFMODE
  1972. UInt16 ref_mode = ReadUshort();
  1973. break;
  1974. case 0x0011: // ITERATION
  1975. UInt16 iteration = ReadUshort();
  1976. break;
  1977. case 0x0010: // DELTA
  1978. SkipBytes(RecordSize);
  1979. break;
  1980. case 0x005f: // SAVERECALC
  1981. UInt16 save_recalc = ReadUshort();
  1982. break;
  1983. case 0x002a: // PRINTHEADERS
  1984. UInt16 print_headers = ReadUshort();
  1985. break;
  1986. case 0x002b: // PRINTGRIDLINES
  1987. UInt16 print_grid = ReadUshort();
  1988. break;
  1989. case 0x0082: // GRIDSET (?? Stupid option)
  1990. UInt16 grid_set = ReadUshort();
  1991. break;
  1992. case 0x0080: // GUTS
  1993. UInt16 outlines_width = ReadUshort();
  1994. UInt16 outlines_height = ReadUshort();
  1995. UInt16 visible_row_levels = ReadUshort();
  1996. UInt16 visible_column_Levels = ReadUshort();
  1997. break;
  1998. case 0x0225: // DEFAULTROWHEIGHT
  1999. UInt16 def_row_options = ReadUshort();
  2000. UInt16 default_row_height = ReadUshort();
  2001. break;
  2002. case 0x0081: // SHEETPR (Sheet properties)
  2003. UInt16 sheet_flags = ReadUshort();
  2004. break;
  2005. case 0x001b: // HORIZONTALPAGEBREAKS:
  2006. ushort cbrk = ReadUshort();
  2007. for (int j = 0; j < cbrk; j++)
  2008. {
  2009. ushort row_of_break = ReadUshort();
  2010. ushort beg_col = ReadUshort();
  2011. ushort end_col = ReadUshort();
  2012. }
  2013. break;
  2014. case 0x0014: // SHEET HEADER
  2015. string sheet_header = "";
  2016. if (RecordSize != 0)
  2017. {
  2018. sheet_header = ReadUnicodeString(false);
  2019. }
  2020. break;
  2021. case 0x0015: // SHET FOOTER
  2022. string sheet_footer = "";
  2023. if (RecordSize != 0)
  2024. {
  2025. sheet_footer = ReadUnicodeString(false);
  2026. }
  2027. break;
  2028. case 0x0083: // HCENTER
  2029. current_sheet.hcenter = ReadUshort();
  2030. break;
  2031. case 0x0084: // VCENTER
  2032. current_sheet.vcenter = ReadUshort();
  2033. break;
  2034. case 0x0026: // LMARGIN
  2035. current_sheet.lmargin = ReadDouble();
  2036. break;
  2037. case 0x0027: // RMARGIN
  2038. current_sheet.rmargin = ReadDouble();
  2039. break;
  2040. case 0x0028: // UP_MARGIN
  2041. current_sheet.umargin = ReadDouble();
  2042. break;
  2043. case 0x0029: // BOTTOM_MARGIN
  2044. current_sheet.bmargin = ReadDouble();
  2045. break;
  2046. case 0x00a1: // PAGESETUP
  2047. current_sheet.Read_PageSetup(this);
  2048. break;
  2049. case 0x089c: // ???
  2050. SkipBytes(RecordSize);
  2051. break;
  2052. case 0x004d: // internal settings for current printer
  2053. SkipBytes(RecordSize);
  2054. break;
  2055. case 0x0055: // DEFCOLWIDTH
  2056. UInt16 default_column_width = ReadUshort();
  2057. break;
  2058. case 0x007d: // COLINFO
  2059. BIFF8_Colinfo colinfo = new BIFF8_Colinfo();
  2060. colinfo.Read(this);
  2061. //#if DEBUG
  2062. // colinfo.Debug(debug, current_sheet.colinfo_list.Count);
  2063. //#endif
  2064. current_sheet.colinfo_list.Add(colinfo);
  2065. break;
  2066. case 0x0200: // DIMENSION
  2067. current_sheet.Read_Dimension(this);
  2068. count_of_row_records = 0;
  2069. break;
  2070. case 0x0208: // ROW
  2071. BIFF8_Row row = new BIFF8_Row();
  2072. row.Read(this);
  2073. //#if DEBUG
  2074. // row.Debug(debug, count_of_row_records);
  2075. //#endif
  2076. row_block = current_sheet.AddRow(row);
  2077. count_of_row_records++;
  2078. break;
  2079. case 0x00fd: // LABELSST
  2080. BIFF8_LabelSST label = new BIFF8_LabelSST();
  2081. label.Read(this);
  2082. //#if DEBUG
  2083. // label.Debug(debug, row_block.cells.Count);
  2084. //#endif
  2085. row_block.cells.Add(label);
  2086. break;
  2087. case 0x0203: // NUMBER: Cell Value, Floating-Point Number
  2088. BIFF8_FloatNumber float_cell = new BIFF8_FloatNumber();
  2089. float_cell.Read(this);
  2090. row_block.cells.Add(float_cell);
  2091. break;
  2092. case 0x0201: // BLANK
  2093. BIFF8_Blank blank = new BIFF8_Blank();
  2094. blank.Read(this);
  2095. //#if DEBUG
  2096. // blank.Debug(debug, row_block.cells.Count);
  2097. //#endif
  2098. row_block.cells.Add(blank);
  2099. break;
  2100. case 0x027e: // Number
  2101. BIFF8_Number number = new BIFF8_Number();
  2102. number.Read(this);
  2103. //#if DEBUG
  2104. // number.Debug(debug, row_block.cells.Count);
  2105. //#endif
  2106. row_block.cells.Add(number);
  2107. break;
  2108. case 0x00be: // MULBLANK
  2109. BIFF8_MulBlank mul_blank = new BIFF8_MulBlank((ushort)RecordSize);
  2110. mul_blank.Read(this);
  2111. //#if DEBUG
  2112. // mul_blank.Debug(debug, row_block.cells.Count);
  2113. //#endif
  2114. row_block.cells.Add(mul_blank);
  2115. break;
  2116. case 0x00d7: // DBCELL
  2117. row_block = current_sheet[current_sheet.Count - 1] as RowBlock;
  2118. row_block.Read(this);
  2119. break;
  2120. case 0x00ec: // DRAWING
  2121. drawing = new BIFF8_ExcelDrawing();
  2122. drawing.Read(this, (UInt32)RecordSize);
  2123. current_sheet.pictures_list.Add(drawing);
  2124. break;
  2125. case 0x005d: // OBJ: Describes a Graphic Object
  2126. drawing.Read_GraphicsObject(this, (UInt32)RecordSize);
  2127. break;
  2128. case 0x023e: // WINDOW2
  2129. current_sheet.Read_Window2(this);
  2130. break;
  2131. case 0x088B:
  2132. case 0x001D:
  2133. SkipBytes(RecordSize);
  2134. break;
  2135. case 0x00e5: // CELL RANGE
  2136. current_sheet.Read_Ranges(this);
  2137. break;
  2138. case 0x0867:
  2139. SkipBytes(RecordSize);
  2140. break;
  2141. default:
  2142. Console.WriteLine(
  2143. string.Format("Found unparsed BIFF record: {0:X04} {1} bytes",
  2144. new object[] { RecordID, RecordSize })
  2145. );
  2146. SkipBytes(RecordSize);
  2147. break;
  2148. }
  2149. debug_size = (int)this.Position - debug_size;
  2150. if (debug_size != RecordSize)
  2151. {
  2152. string.Format("Catch it");
  2153. throw new Exception("Item size error");
  2154. }
  2155. BytesCount -= RecordSize;
  2156. } while (BytesCount > 0);
  2157. //#if DEBUG
  2158. // shared_strings.Debug(debug);
  2159. //#endif
  2160. // debug.Close();
  2161. }
  2162. internal void Prepare(ExportMatrix export_matrix, bool page_breaks, string name_postfix)
  2163. {
  2164. const float MargDiv = 25.4F;
  2165. AddColor(0x000000); // black
  2166. AddColor(0xFFFFFF); // white
  2167. AddColor(0x0000FF); // red
  2168. AddColor(0x00FF00); // green
  2169. AddColor(0xFF0000); // blue
  2170. AddColor(0x00FFFF); // yellow
  2171. AddColor(0xFF00FF); // magenta
  2172. AddColor(0xFFFF00); // cyan
  2173. BIFF8_Blank blank_cell;
  2174. BIFF8_ExtendedFormat biff_style_xf;
  2175. BIFF8_Font biff_font;
  2176. BIFF8_Style biff_style;
  2177. biff_style_xf = new BIFF8_ExtendedFormat();
  2178. for (int i = 0; i < 15; i++)
  2179. xf_list.Add(biff_style_xf);
  2180. ushort empty_cell_style = (ushort)xf_list.Count;
  2181. // empty cell style
  2182. biff_style_xf = new BIFF8_ExtendedFormat();
  2183. biff_style_xf.xf_type = 0x1;
  2184. xf_list.Add(biff_style_xf);
  2185. biff_style = new BIFF8_Style();
  2186. styles.Add(biff_style);
  2187. for (int i = 0; i < 21; i++)
  2188. {
  2189. biff_font = new BIFF8_Font("Arial", 220, 12);
  2190. font_list.Add(biff_font);
  2191. }
  2192. int start_font_idx = font_list.Count + 1;
  2193. ushort default_styles_count = (ushort)xf_list.Count;
  2194. for (ushort i = 0; i < export_matrix.StylesCount; i++)
  2195. {
  2196. ExportIEMStyle EStyle = export_matrix.StyleById(i);
  2197. biff_font = new BIFF8_Font(EStyle, AddColor(EStyle.TextColor, true));
  2198. font_list.Add(biff_font);
  2199. ushort idx = (ushort)(i + default_styles_count);
  2200. // 164 - 382 indices for users formats
  2201. BIFF8_ExtendedFormat biff_xf = new BIFF8_ExtendedFormat(false, (ushort)(i + start_font_idx), EStyle, this, 164 + idx);
  2202. xf_list.Add(biff_xf);
  2203. if (biff_xf.format_index > 163)
  2204. format_list.Add(new BIFF8_Format(biff_xf.format_index, ExportUtils.GetExcelFormatSpecifier(EStyle.Format)));
  2205. biff_style = new BIFF8_Style((ushort)idx, EStyle);
  2206. styles.Add(biff_style);
  2207. }
  2208. string name = export_matrix.Report.PreparedPages.GetPage(0).Name + name_postfix;
  2209. BIFF8_Sheet sheet = new BIFF8_Sheet(name, hideGridLines);
  2210. this.sheet_list.Add(sheet);
  2211. for (ushort x = 0; x < export_matrix.Width - 1; x++)
  2212. {
  2213. ushort width = (ushort)(34 * (export_matrix.XPosById(x + 1) - export_matrix.XPosById(x)));
  2214. sheet.colinfo_list.Add(new BIFF8_Colinfo(x, width, empty_cell_style));
  2215. }
  2216. for (ushort y = 0; y < export_matrix.Height - 1; y++)
  2217. {
  2218. ushort row_height = (ushort)(14 * (export_matrix.YPosById(y + 1) - export_matrix.YPosById(y)));
  2219. BIFF8_Row row = new BIFF8_Row(y, 0, (ushort)export_matrix.Width, row_height, 0x00000140);
  2220. sheet.AddRow(row);
  2221. ushort StyleIndex = 0;
  2222. for (ushort x = 0; x < export_matrix.Width - 1; x++)
  2223. {
  2224. string val = "$" + Convert.ToChar('A' + x) + "$" + (y + 1).ToString();
  2225. StyleIndex = 0;
  2226. int i = export_matrix.Cell(x, y);
  2227. if (i != -1)
  2228. {
  2229. int fx, fy, dx, dy;
  2230. export_matrix.ObjectPos(i, out fx, out fy, out dx, out dy);
  2231. ExportIEMObject Obj = export_matrix.ObjectById(i);
  2232. StyleIndex = (ushort)(Obj.StyleIndex + default_styles_count);
  2233. if (Obj.Counter < dy)
  2234. {
  2235. if (Obj.Counter == 0)
  2236. {
  2237. BIFF8_CellData cell;
  2238. if (Obj.IsDateTime)
  2239. {
  2240. DateTime dt = (DateTime)Obj.Value;
  2241. cell = new BIFF8_Number(y, x, StyleIndex, dt);
  2242. }
  2243. else if (Obj.IsNumeric && Obj.Value != null)
  2244. {
  2245. Decimal num = (Decimal)Obj.Value;
  2246. cell = new BIFF8_FloatNumber(y, x, StyleIndex, num);
  2247. }
  2248. else
  2249. {
  2250. // val = ExportUtils.XmlString(Obj.Text, Obj.HtmlTags);
  2251. val = "";
  2252. foreach (char ch in Obj.Text)
  2253. if (ch != 0xd) val += ch;
  2254. cell = new BIFF8_LabelSST(y, x, StyleIndex, shared_strings.Count);
  2255. shared_strings.Add(val);
  2256. }
  2257. row.Add(cell);
  2258. if (Obj.URL != null && Obj.URL.Length != 0)
  2259. {
  2260. BIFF8_Hyperlink link = new BIFF8_Hyperlink(Obj.URL);
  2261. sheet.url_list.Add(link);
  2262. }
  2263. }
  2264. else
  2265. {
  2266. blank_cell = new BIFF8_Blank((ushort)y, (ushort)x, StyleIndex);
  2267. row.Add(blank_cell);
  2268. }
  2269. if (!Obj.IsText) // Picture
  2270. {
  2271. if (Obj.Width > 0 && Obj.Counter == 0)
  2272. {
  2273. BIFF8_MSODrawingRecord rec;
  2274. if (drawing_group == null) drawing_group = new BIFF8_MSODrawingGroup();
  2275. int pic_index;
  2276. if (!this.pic_list.ContainsKey(Obj.Hash))
  2277. {
  2278. rec = new BIFF8_MSODrawingRecord();
  2279. pic_index = drawing_group.Count;
  2280. rec.LoadPicture(Obj, pic_index);
  2281. drawing_group.Add(rec);
  2282. this.pic_list.Add(Obj.Hash, pic_index);
  2283. }
  2284. else
  2285. {
  2286. pic_index = (int)this.pic_list[Obj.Hash];
  2287. rec = drawing_group[pic_index] as BIFF8_MSODrawingRecord;
  2288. rec.AddReference();
  2289. }
  2290. drawing = new BIFF8_ExcelDrawing();
  2291. drawing.AddPicureToSheet(Obj, pic_index, fx, fy, dx, dy);
  2292. sheet.pictures_list.Add(drawing);
  2293. }
  2294. }
  2295. // Add multiple blanks for merged cells
  2296. BIFF8_MulBlank cells;
  2297. if (dx > 1)
  2298. {
  2299. if (Obj.Counter == 0 && Obj.IsText)
  2300. cells = new BIFF8_MulBlank((ushort)y, (ushort)(x + 1), (ushort)(x + dx - 1), StyleIndex);
  2301. else
  2302. cells = new BIFF8_MulBlank((ushort)y, (ushort)(x), (ushort)(x + dx - 1), StyleIndex);
  2303. row.Add(cells);
  2304. x += (ushort)(dx - 1);
  2305. }
  2306. Obj.Counter++;
  2307. if ((Obj.Counter == dy) && (dx > 1 || dy > 1))
  2308. {
  2309. FIFF8_CellRange single_range = new FIFF8_CellRange((ushort)fy, (ushort)(fy + dy - 1), (ushort)fx, (ushort)(fx + dx - 1));
  2310. sheet.cell_ranges.Add(single_range);
  2311. }
  2312. }
  2313. }
  2314. else
  2315. {
  2316. BIFF8_CellData cell = new BIFF8_Blank(y, x, StyleIndex);
  2317. row.Add(cell);
  2318. }
  2319. }
  2320. }
  2321. base.SetLength(0);
  2322. this.Write_BOF(0x0005); // Workbook globals
  2323. this.window1.Write(this);
  2324. this.Write_1904();
  2325. // Skip many records at this point
  2326. foreach (BIFF8_Font font in this.font_list) font.Write(this);
  2327. foreach (BIFF8_Format format in this.format_list) format.Write(this);
  2328. foreach (BIFF8_ExtendedFormat xf in this.xf_list) xf.Write(this);
  2329. // Skip many records at this point
  2330. foreach (BIFF8_Style style in this.styles) style.Write(this);
  2331. this.Write_Palette(); // PALETTE
  2332. // Skip many records at this point
  2333. foreach (BIFF8_Sheet sheetItem in this.sheet_list)
  2334. {
  2335. sheetItem.Write_Globals(this);
  2336. }
  2337. // Skip some records at this point
  2338. if (drawing_group != null) drawing_group.Write(this);
  2339. shared_strings.Write(this);
  2340. // Skip some records at this point
  2341. Write_EOF();
  2342. // Write sheet to stream
  2343. foreach (BIFF8_Sheet sheetItem in this.sheet_list)
  2344. {
  2345. sheet.lmargin = export_matrix.PageLMargin(0) / MargDiv;
  2346. sheet.rmargin = export_matrix.PageRMargin(0) / MargDiv;
  2347. sheet.umargin = export_matrix.PageTMargin(0) / MargDiv;
  2348. sheet.bmargin = export_matrix.PageBMargin(0) / MargDiv;
  2349. sheetItem.Write(this, page_breaks, export_matrix);
  2350. }
  2351. int eof = -1;
  2352. unchecked
  2353. {
  2354. this.streamDirEntry.bOF = (uint)eof;
  2355. }
  2356. this.streamDirEntry.size = (uint)this.Length;
  2357. this.Position = 0;
  2358. if (drawing_group != null)
  2359. {
  2360. drawing_group.Clear();
  2361. drawing_group = null;
  2362. }
  2363. }
  2364. public Workbook(CompoundDocumentHeader document_header, DirectoryEntry dir)
  2365. : base(document_header, dir)
  2366. {
  2367. }
  2368. internal void Write(FileStream f)
  2369. {
  2370. DirectoryEntry dir = base.streamDirEntry;
  2371. }
  2372. }
  2373. /// <summary>
  2374. /// Excel 2003 export class
  2375. /// </summary>
  2376. public partial class Excel2003Document : ExportBase
  2377. {
  2378. ExportMatrix matrix;
  2379. int page_num;
  2380. bool split_pages = false;
  2381. CompoundDocumentHeader hdr;
  2382. DirectoryStream root_dir;
  2383. Workbook workbook;
  2384. OLE_Stream summary_info;
  2385. OLE_Stream doc_summary_info;
  2386. private bool wysiwyg;
  2387. private bool pageBreaks;
  2388. private bool hideGridLines;
  2389. private bool printOptimized;
  2390. /// <summary>
  2391. /// Gets or sets a value that determines whether the wysiwyg mode should be used
  2392. /// for better results.
  2393. /// </summary>
  2394. public bool Wysiwyg
  2395. {
  2396. get { return wysiwyg; }
  2397. set { wysiwyg = value; }
  2398. }
  2399. /// <summary>
  2400. /// Gets or sets a value that determines whether to insert page breaks in the output file or not.
  2401. /// </summary>
  2402. public bool PageBreaks
  2403. {
  2404. get { return pageBreaks; }
  2405. set { pageBreaks = value; }
  2406. }
  2407. /// <summary>
  2408. /// Gets or sets a value that determines whether to hide gridlines.
  2409. /// </summary>
  2410. public bool HideGridLines
  2411. {
  2412. get { return hideGridLines; }
  2413. set { hideGridLines = value; }
  2414. }
  2415. /// <summary>
  2416. /// Enables or disables optimization of images for printing.
  2417. /// </summary>
  2418. public bool PrintOptimized
  2419. {
  2420. get { return printOptimized; }
  2421. set { printOptimized = value; }
  2422. }
  2423. /// <summary>
  2424. /// Read and parse Excel document
  2425. /// </summary>
  2426. public void Read(string FileName)
  2427. {
  2428. FileStream f = File.Open(FileName, System.IO.FileMode.Open);
  2429. hdr = new CompoundDocumentHeader(f);
  2430. hdr.Read();
  2431. root_dir = new DirectoryStream(hdr);
  2432. root_dir.Read(hdr.dir, hdr);
  2433. /*
  2434. #if _DEBUG
  2435. Console.WriteLine(string.Format("{0,-35} {1,-12} {2,8} {3,5} {4,4} {5,4} {6,4}", new object[] {
  2436. "Name",
  2437. "Type",
  2438. "Size",
  2439. "BOF",
  2440. "Root", "Left", "Rght"}));
  2441. #endif
  2442. */
  2443. foreach (DirectoryEntry entry in root_dir)
  2444. {
  2445. if (entry.type == DirectoryEntry.DirEntryType.Empty) continue;
  2446. /*
  2447. #if _DEBUG
  2448. Console.WriteLine(string.Format("{0,-35} {1,-12} {2,8} {3,5} {4,4} {5,4} {6,4}",
  2449. new object[] {
  2450. entry.entry_name,
  2451. entry.type,
  2452. entry.Size,
  2453. entry.BOF,
  2454. entry.RootDirID,
  2455. entry.LeftChildDirID,
  2456. entry.RightChildDirID
  2457. }));
  2458. #endif
  2459. */
  2460. switch (entry.entry_name)
  2461. {
  2462. case "Root Entry":
  2463. hdr.ReadShortStreamContainer(entry);
  2464. break;
  2465. case "Workbook":
  2466. workbook = new Workbook(hdr, entry);
  2467. workbook.Read(entry);
  2468. break;
  2469. case "SummaryInformation":
  2470. summary_info = new OLE_Stream(hdr, entry);
  2471. //summary_info.Read();
  2472. break;
  2473. case "DocumentSummaryInformation":
  2474. doc_summary_info = new OLE_Stream(hdr, entry);
  2475. //doc_summary_info.Read();
  2476. break;
  2477. case "CompObj":
  2478. continue;
  2479. default:
  2480. throw new Exception("Unknown stream");
  2481. }
  2482. }
  2483. f.Close();
  2484. }
  2485. private void Prepare(string name_postfix)
  2486. {
  2487. hdr.Reset();
  2488. root_dir.Reset();
  2489. workbook.Prepare(matrix, pageBreaks, name_postfix);
  2490. workbook.Write();
  2491. summary_info.Write();
  2492. doc_summary_info.Write();
  2493. DirectoryEntry root = root_dir.Add("Root Entry");
  2494. root_dir.Add("Workbook", workbook, -1, -1);
  2495. root_dir.Add("SummaryInformation", summary_info, 1, -1);
  2496. root_dir.Add("DocumentSummaryInformation", doc_summary_info, 2, -1);
  2497. root_dir.Write(hdr.dir, hdr);
  2498. hdr.Write();
  2499. }
  2500. internal void Write(string FileName)
  2501. {
  2502. this.Prepare("");
  2503. FileStream f = File.Open(FileName, System.IO.FileMode.Create, FileAccess.ReadWrite);
  2504. hdr.FinalWriteToStream(f);
  2505. f.Close();
  2506. }
  2507. /// <inheritdoc/>
  2508. public Excel2003Document()
  2509. {
  2510. pageBreaks = true;
  2511. wysiwyg = true;
  2512. hideGridLines = false;
  2513. printOptimized = false;
  2514. }
  2515. internal void Create()
  2516. {
  2517. // These lines are useful for debug XLS file
  2518. // this.Read(@"C:\Users\alman\Desktop\Support\Simple List_back.xls");
  2519. // this.Read(@"C:\Users\alman\Desktop\Support\Simple List.xls");
  2520. //this.Read(@"C:\Users\alman\Desktop\Support\Groups.xls");
  2521. // this.Read(@"C:\Users\alman\Desktop\Support\Single.xls");
  2522. // this.Read(@"C:\Users\alman\Desktop\Support\crlf_fix.xls");
  2523. // this.Read(@"C:\Users\alman\Desktop\Support\Huge.xls");
  2524. hdr = new CompoundDocumentHeader();
  2525. root_dir = new DirectoryStream(hdr);
  2526. DirectoryEntry workbook_entry = new DirectoryEntry("Workbook", DirectoryEntry.DirEntryType.UseStream);
  2527. workbook = new Workbook(hdr, workbook_entry);
  2528. workbook.hideGridLines = hideGridLines;
  2529. DirectoryEntry summary_entry = new DirectoryEntry("SummaryInformation", DirectoryEntry.DirEntryType.UseStream);
  2530. summary_info = new OLE_Stream(hdr, summary_entry);
  2531. DirectoryEntry doc_summary_entry = new DirectoryEntry("DocumentSummaryInformation", DirectoryEntry.DirEntryType.UseStream);
  2532. doc_summary_info = new OLE_Stream(hdr, doc_summary_entry);
  2533. }
  2534. #region Protected Methods
  2535. private void initalize_matix()
  2536. {
  2537. matrix = new ExportMatrix();
  2538. if (wysiwyg)
  2539. matrix.Inaccuracy = 0.5f;
  2540. else
  2541. matrix.Inaccuracy = 10;
  2542. matrix.PlainRich = true;
  2543. matrix.AreaFill = false; // true; //
  2544. matrix.CropAreaFill = true;
  2545. matrix.Report = Report;
  2546. matrix.Images = true;
  2547. matrix.WrapText = false;
  2548. matrix.FullTrust = false;
  2549. matrix.MaxCellHeight = 409 * 1;
  2550. matrix.PrintOptimized = PrintOptimized;
  2551. }
  2552. /// <inheritdoc/>
  2553. protected override void Start()
  2554. {
  2555. base.Start();
  2556. page_num = 1;
  2557. this.Create();
  2558. if (!split_pages)
  2559. {
  2560. initalize_matix();
  2561. }
  2562. }
  2563. /// <inheritdoc/>
  2564. protected override void ExportPageBegin(ReportPage page)
  2565. {
  2566. base.ExportPageBegin(page);
  2567. if (split_pages)
  2568. {
  2569. initalize_matix();
  2570. }
  2571. matrix.AddPageBegin(page);
  2572. }
  2573. /// <inheritdoc/>
  2574. protected override void ExportBand(BandBase band)
  2575. {
  2576. base.ExportBand(band);
  2577. //if (sheets.ContainsKey(band.Page.Name))
  2578. //{
  2579. // sh = sheets[band.Page.Name] as OoXMLSheet;
  2580. // sh.Matrix.AddBand(band);
  2581. //}
  2582. //else
  2583. {
  2584. matrix.AddBand(band, this);
  2585. }
  2586. }
  2587. /// <inheritdoc/>
  2588. protected override void ExportPageEnd(ReportPage page)
  2589. {
  2590. //if (sheets.ContainsKey(page.Name))
  2591. //{
  2592. // sh = sheets[page.Name] as OoXMLSheet;
  2593. // sh.Matrix.AddPageEnd(page);
  2594. //}
  2595. //else
  2596. //{
  2597. matrix.AddPageEnd(page);
  2598. // page_name = page.Name;
  2599. // Export(true);
  2600. // sh = new OoXMLSheet(matrix, page.Name, sheets.Count + 1);
  2601. // sheets[page.Name] = sh;
  2602. // FWorkBook.AddSheet(sh);
  2603. //}
  2604. if (split_pages)
  2605. {
  2606. matrix.Prepare();
  2607. this.Prepare("_" + page_num.ToString());
  2608. page_num++;
  2609. }
  2610. }
  2611. /// <inheritdoc/>
  2612. protected override void Finish()
  2613. {
  2614. if (!split_pages)
  2615. {
  2616. matrix.Prepare();
  2617. this.Prepare("");
  2618. }
  2619. hdr.FinalWriteToStream(Stream);
  2620. }
  2621. /// <inheritdoc/>
  2622. protected override string GetFileFilter()
  2623. {
  2624. return new MyRes("FileFilters").Get("XlsFile");
  2625. }
  2626. /// <inheritdoc/>
  2627. public override void Serialize(FRWriter writer)
  2628. {
  2629. base.Serialize(writer);
  2630. writer.WriteBool("Wysiwyg", Wysiwyg);
  2631. writer.WriteBool("PageBreaks", PageBreaks);
  2632. writer.WriteBool("HideGridLines", HideGridLines);
  2633. writer.WriteBool("PrintOptimized", PrintOptimized);
  2634. }
  2635. #endregion
  2636. }
  2637. }