MES_Common.cs 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Printing;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using Dongke.IBOSS.Framework.Controls;
  10. /*using iTextSharp.text;
  11. using iTextSharp.text.pdf;*/
  12. using Microsoft.VisualBasic;
  13. using Microsoft.VisualBasic.CompilerServices;
  14. using Microsoft.VisualBasic.FileIO;
  15. using NPOI.XSSF.UserModel;
  16. using NPOI.SS.UserModel;
  17. using Curtain.Log;
  18. namespace Dongke.IBOSS.PRD.Framework.Controls
  19. {
  20. public class MES_Common
  21. {
  22. /* private class testevent : IPdfPageEvent
  23. {
  24. private PdfContentByte cb;
  25. private PdfTemplate template;
  26. public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
  27. {
  28. }
  29. public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
  30. {
  31. }
  32. public void OnCloseDocument(PdfWriter writer, Document document)
  33. {
  34. ((PdfContentByte)template).BeginText();
  35. ((PdfContentByte)template).SetFontAndSize(BaseFont.CreateFont(), 10f);
  36. ((PdfContentByte)template).SetTextMatrix(0f, 0f);
  37. ((PdfContentByte)template).ShowText(Conversions.ToString(checked(writer.PageNumber - 1)));
  38. ((PdfContentByte)template).EndText();
  39. }
  40. public void OnEndPage(PdfWriter writer, Document document)
  41. {
  42. string text = " - " + writer.PageNumber;
  43. BaseFont val = BaseFont.CreateFont();
  44. float widthPoint = val.GetWidthPoint("1", 10f);
  45. Rectangle pageSize = document.PageSize;
  46. cb.SetRGBColorFill(100, 100, 100);
  47. cb.BeginText();
  48. cb.SetFontAndSize(val, 10f);
  49. cb.SetTextMatrix(document.LeftMargin + (document.PageSize.Width - document.RightMargin - document.LeftMargin) / 2f, pageSize.GetBottom(document.BottomMargin) - document.BottomMargin / 3f);
  50. cb.ShowText(text);
  51. cb.EndText();
  52. cb.AddTemplate(template, document.LeftMargin - widthPoint + (document.PageSize.Width - document.RightMargin - document.LeftMargin) / 2f, pageSize.GetBottom(document.BottomMargin) - document.BottomMargin / 3f);
  53. }
  54. public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text)
  55. {
  56. }
  57. public void OnOpenDocument(PdfWriter writer, Document document)
  58. {
  59. cb = writer.DirectContent;
  60. template = cb.CreateTemplate(50f, 50f);
  61. }
  62. public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
  63. {
  64. }
  65. public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
  66. {
  67. }
  68. public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
  69. {
  70. }
  71. public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
  72. {
  73. }
  74. public void OnStartPage(PdfWriter writer, Document document)
  75. {
  76. }
  77. }
  78. */
  79. private static MES_Common _instance;
  80. /*private Document mydoc;
  81. private PdfWriter mywriter;*/
  82. private float _paperwidth;
  83. private float _paperheight;
  84. private bool _paperlandscape;
  85. private Margins _margin;
  86. private float myratio;
  87. private bool _repeatheader;
  88. private bool disposedValue;
  89. public static MES_Common Instance
  90. {
  91. get
  92. {
  93. if (_instance == null)
  94. {
  95. _instance = new MES_Common();
  96. }
  97. return _instance;
  98. }
  99. }
  100. public bool RepeatHeader
  101. {
  102. get
  103. {
  104. return _repeatheader;
  105. }
  106. set
  107. {
  108. _repeatheader = value;
  109. }
  110. }
  111. public Margins PaperMargin
  112. {
  113. get
  114. {
  115. return _margin;
  116. }
  117. set
  118. {
  119. _margin = value;
  120. }
  121. }
  122. public float PaperWidth
  123. {
  124. get
  125. {
  126. return _paperwidth;
  127. }
  128. set
  129. {
  130. _paperwidth = value;
  131. }
  132. }
  133. public float PaperHeight
  134. {
  135. get
  136. {
  137. return _paperheight;
  138. }
  139. set
  140. {
  141. if (value > 0f)
  142. {
  143. _paperheight = value;
  144. }
  145. }
  146. }
  147. public bool PaperLandScape
  148. {
  149. get
  150. {
  151. return _paperlandscape;
  152. }
  153. set
  154. {
  155. _paperlandscape = value;
  156. }
  157. }
  158. private MES_Common()
  159. {
  160. }
  161. public bool ExportExcel(DataGridView mydgv, TreeView columnheadertree, Font titlefont, string title, string FileName, Font tableheaderfont, string Tableheader, Font tablefooterfont, string TableFooter, string sheetname, bool isopenfile, bool isshowprogress, bool isTopNodeFill)
  162. {
  163. checked
  164. {
  165. bool result;
  166. if (mydgv == null)
  167. {
  168. result = false;
  169. }
  170. else
  171. {
  172. if (titlefont == null)
  173. {
  174. titlefont = new Font("黑体", 18f, FontStyle.Bold);
  175. }
  176. if (tableheaderfont == null)
  177. {
  178. tableheaderfont = new Font("宋体", 12f);
  179. }
  180. if (tablefooterfont == null)
  181. {
  182. tablefooterfont = new Font("宋体", 12f);
  183. }
  184. progressexcel progressexcel = null;
  185. Module1.G_CancelExportDGVToExcel = false;
  186. if (isshowprogress)
  187. {
  188. progressexcel = new progressexcel();
  189. progressexcel.TopMost = true;
  190. progressexcel.Visible = true;
  191. }
  192. Ruler ruler = new Ruler();
  193. try
  194. {
  195. if (isshowprogress)
  196. {
  197. progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
  198. progressexcel.ProgressBar1.Value = 0;
  199. Application.DoEvents();
  200. }
  201. int columnCount = mydgv.ColumnCount;
  202. int[] array = new int[columnCount - 1 + 1];
  203. int[] array2 = new int[columnCount - 1 + 1];
  204. int num = 0;
  205. int num2 = 0;
  206. int num3 = columnCount - 1;
  207. for (int i = num2; i <= num3; i++)
  208. {
  209. array[i] = i;
  210. array2[i] = mydgv.Columns[i].DisplayIndex;
  211. if (mydgv.Columns[i].Visible)
  212. {
  213. num++;
  214. }
  215. }
  216. if (num <= 0)
  217. {
  218. if (isshowprogress)
  219. {
  220. progressexcel.Close();
  221. progressexcel.Dispose();
  222. }
  223. result = true;
  224. }
  225. else
  226. {
  227. Array.Sort(array2, array);
  228. int[] array3 = new int[num - 1 + 1];
  229. int num4 = 0;
  230. int num5 = 0;
  231. int num6 = columnCount - 1;
  232. for (int j = num5; j <= num6; j++)
  233. {
  234. if (mydgv.Columns[array[j]].Visible)
  235. {
  236. array3[num4] = array[j];
  237. num4++;
  238. }
  239. }
  240. DGVtoExcel.w_ZDYcolors = 8;
  241. int endcol = num - 1;
  242. SaveFileDialog saveFileDialog = new SaveFileDialog();
  243. saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
  244. saveFileDialog.AddExtension = true;
  245. saveFileDialog.OverwritePrompt = false;
  246. //saveFileDialog.DefaultExt = "xls";
  247. //saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
  248. saveFileDialog.DefaultExt = "xlsx";
  249. saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
  250. saveFileDialog.FileName = FileName;
  251. if (isshowprogress)
  252. {
  253. progressexcel.Visible = false;
  254. }
  255. if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
  256. {
  257. if (isshowprogress)
  258. {
  259. progressexcel.Close();
  260. progressexcel.Dispose();
  261. }
  262. saveFileDialog.Dispose();
  263. return false;
  264. }
  265. FileName = saveFileDialog.FileName;
  266. saveFileDialog.Dispose();
  267. if (isshowprogress)
  268. {
  269. progressexcel.Visible = true;
  270. }
  271. DGVtoExcel.w_font = null;
  272. DGVtoExcel.w_ifont = null;
  273. DGVtoExcel.w_Cellstyle = null;
  274. DGVtoExcel.w_iCellstyle = null;
  275. DGVtoExcel.w_color = null;
  276. if (MyProject.Computer.FileSystem.FileExists(FileName))
  277. {
  278. if (isshowprogress)
  279. {
  280. progressexcel.Visible = false;
  281. }
  282. switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
  283. {
  284. case MsgBoxResult.Cancel:
  285. if (isshowprogress)
  286. {
  287. progressexcel.Close();
  288. progressexcel.Dispose();
  289. }
  290. return false;
  291. case MsgBoxResult.Yes:
  292. try
  293. {
  294. MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
  295. }
  296. catch (Exception projectError)
  297. {
  298. Logger.Error(projectError, "projectError");
  299. MessageBox.Show(projectError.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  300. ProjectData.SetProjectError(projectError);
  301. Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
  302. if (isshowprogress)
  303. {
  304. progressexcel.Close();
  305. progressexcel.Dispose();
  306. }
  307. result = false;
  308. ProjectData.ClearProjectError();
  309. return result;
  310. }
  311. break;
  312. }
  313. if (isshowprogress)
  314. {
  315. progressexcel.Visible = true;
  316. }
  317. }
  318. XSSFWorkbook hSSFWorkbook;
  319. if (MyProject.Computer.FileSystem.FileExists(FileName))
  320. {
  321. FileStream fileStream;
  322. try
  323. {
  324. fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
  325. }
  326. catch (Exception ex)
  327. {
  328. Logger.Error(ex, "ex");
  329. MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  330. ProjectData.SetProjectError(ex);
  331. Exception ex2 = ex;
  332. if (isshowprogress)
  333. {
  334. progressexcel.Close();
  335. progressexcel.Dispose();
  336. }
  337. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
  338. fileStream = null;
  339. result = false;
  340. ProjectData.ClearProjectError();
  341. return result;
  342. }
  343. try
  344. {
  345. hSSFWorkbook = new XSSFWorkbook(fileStream);
  346. }
  347. catch (Exception projectError2)
  348. {
  349. Logger.Error(projectError2, "projectError2");
  350. MessageBox.Show(projectError2.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  351. ProjectData.SetProjectError(projectError2);
  352. if (isshowprogress)
  353. {
  354. progressexcel.Close();
  355. progressexcel.Dispose();
  356. }
  357. fileStream.Dispose();
  358. fileStream = null;
  359. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
  360. hSSFWorkbook = null;
  361. result = false;
  362. ProjectData.ClearProjectError();
  363. return result;
  364. }
  365. }
  366. else
  367. {
  368. hSSFWorkbook = new XSSFWorkbook();
  369. }
  370. string excelNewSheetName = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, sheetname);
  371. XSSFSheet hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
  372. int num7;
  373. if (Operators.CompareString(title, "", TextCompare: false) == 0)
  374. {
  375. num7 = 0;
  376. }
  377. else
  378. {
  379. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, 0, 0, 0, endcol, title, titlefont);
  380. num7 = 1;
  381. }
  382. if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
  383. {
  384. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, Tableheader, tableheaderfont);
  385. num7++;
  386. }
  387. DGVtoExcel.w_gridcolor = DGVtoExcel.GetXLColour(hSSFWorkbook, mydgv.GridColor);
  388. if (mydgv.ColumnHeadersVisible)
  389. {
  390. if (columnheadertree == null)
  391. {
  392. IRow row = hSSFSheet.CreateRow(num7);
  393. row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.ColumnHeadersHeight, isX: false)) * 72f;
  394. int num8 = 0;
  395. int num9 = num - 1;
  396. for (int k = num8; k <= num9; k++)
  397. {
  398. DGVtoExcel.SetCellValue(hSSFSheet, num7, k, mydgv.Columns[array3[k]].HeaderText);
  399. Font dGVHeaderFont = Module1.GetDGVHeaderFont(mydgv.Columns[array3[k]]);
  400. Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[k]]);
  401. Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[k]]);
  402. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num7, k, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
  403. }
  404. }
  405. else
  406. {
  407. mytree mytree = new mytree(columnheadertree, mydgv);
  408. int leves = mytree.GetLeves();
  409. int num10 = 1;
  410. int num11 = leves;
  411. for (int l = num10; l <= num11; l++)
  412. {
  413. IRow row2 = hSSFSheet.CreateRow(num7 + l - 1);
  414. row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)mydgv.ColumnHeadersHeight / (double)leves), isX: false)) * 72f;
  415. }
  416. int num12 = 1;
  417. int num13 = leves;
  418. bool flag = true;
  419. for (int m = num12; m <= num13; m++)
  420. {
  421. if (m != 1)
  422. {
  423. num7++;
  424. }
  425. int num14 = 0;
  426. int num15 = num - 1;
  427. for (int n = num14; n <= num15; n++)
  428. {
  429. string nodeText = mytree.GetNodeText(array3[n], m);
  430. int cellHeightLevels = mytree.GetCellHeightLevels(array3[n], m, isTopNodeFill, out flag);
  431. int cellWidthCols = mytree.GetCellWidthCols(array3[n], m);
  432. if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
  433. {
  434. continue;
  435. }
  436. Font font = mytree.GetNodeFont(array3[n], m);
  437. if (font == null)
  438. {
  439. font = Module1.GetDGVHeaderFont(mydgv.Columns[array3[n]]);
  440. }
  441. Color forcolor = mytree.GetNodeForeColor(array3[n], m);
  442. if (forcolor.IsEmpty)
  443. {
  444. forcolor = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[n]]);
  445. }
  446. Color bkcolor = mytree.GetNodeBackColor(array3[n], m);
  447. if (bkcolor.IsEmpty)
  448. {
  449. bkcolor = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[n]]);
  450. }
  451. if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
  452. {
  453. int num16 = num7;
  454. if (!flag)
  455. {
  456. num16++;
  457. }
  458. DGVtoExcel.SetCellValue(hSSFSheet, num16, n, nodeText);
  459. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num16, n, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  460. continue;
  461. }
  462. DGVtoExcel.SetCellValue(hSSFSheet, num7, n, nodeText);
  463. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num7, n, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  464. int num17 = num7;
  465. int num18 = num7 + cellHeightLevels - 1;
  466. for (int num19 = num17; num19 <= num18; num19++)
  467. {
  468. int num20 = n;
  469. int num21 = n + cellWidthCols - 1;
  470. for (int num22 = num20; num22 <= num21; num22++)
  471. {
  472. if (unchecked(num19 != num7 || num22 != n))
  473. {
  474. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num19, num22, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  475. }
  476. }
  477. }
  478. DGVtoExcel.SetMergeCell(hSSFSheet, num7, n, num7 + cellHeightLevels - 1, n + cellWidthCols - 1);
  479. }
  480. }
  481. mytree.Dispose();
  482. }
  483. num7++;
  484. }
  485. int num23 = 1;
  486. int num24 = 0;
  487. int num25 = 0;
  488. int num26 = 0;
  489. int num27 = 0;
  490. int num28 = 1;
  491. int num29 = 0;
  492. int num30 = mydgv.RowCount - 1;
  493. for (int num31 = num29; num31 <= num30; num31++)
  494. {
  495. IRow row3 = hSSFSheet.CreateRow(num7 + num31);
  496. row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num31].Height, isX: false)) * 72f;
  497. if (!mydgv.Rows[num31].Visible)
  498. {
  499. row3.ZeroHeight = true;
  500. }
  501. if (num31 + num7 == 1048575)
  502. {
  503. break;
  504. }
  505. }
  506. int num32 = 0;
  507. int num33 = 0;
  508. if (isshowprogress)
  509. {
  510. progressexcel.ProgressBar1.Maximum = mydgv.RowCount;
  511. }
  512. int num34 = 0;
  513. int num35 = mydgv.RowCount - 1;
  514. for (int num36 = num34; num36 <= num35; num36++)
  515. {
  516. bool flag2 = false;
  517. if (isshowprogress)
  518. {
  519. progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num36 + 1) + "〗条记录,共〖" + Conversions.ToString(mydgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(num36 + 1) / (double)mydgv.RowCount, "###.##%") + "〗";
  520. progressexcel.ProgressBar1.Value = num36 + 1;
  521. Application.DoEvents();
  522. }
  523. if (Module1.G_CancelExportDGVToExcel)
  524. {
  525. break;
  526. }
  527. int num37 = 0;
  528. int num38 = num - 1;
  529. for (num4 = num37; num4 <= num38; num4++)
  530. {
  531. int num39;
  532. if (mydgv.Columns[array3[num4]].ValueType == null)
  533. {
  534. num39 = 0;
  535. }
  536. else
  537. {
  538. switch (Strings.Left(mydgv.Columns[array3[num4]].ValueType.Name.ToUpper(), 3))
  539. {
  540. case "STR":
  541. num39 = 0;
  542. break;
  543. case "INT":
  544. case "DEC":
  545. num39 = 1;
  546. flag2 = false;
  547. break;
  548. case "DAT":
  549. num39 = 2;
  550. break;
  551. case "BOO":
  552. num39 = 3;
  553. flag2 = false;
  554. break;
  555. default:
  556. num39 = 0;
  557. break;
  558. }
  559. }
  560. num28 = 1;
  561. num23 = 1;
  562. num26 = 0;
  563. num24 = 0;
  564. num27 = 0;
  565. num26 = 0;
  566. num24 = 0;
  567. num25 = 0;
  568. string text = Conversions.ToString(mydgv.Rows[num36].Cells[array3[num4]].Tag);
  569. switch (text)
  570. {
  571. default:
  572. {
  573. float mywidth = 0f;
  574. float num40 = 9f;
  575. int firstcolumn = 0;
  576. int firstrow = 0;
  577. int firstc = 0;
  578. int firstr = 0;
  579. switch (text)
  580. {
  581. default:
  582. num28 = 1;
  583. num23 = 1;
  584. break;
  585. case "合并":
  586. case "左上合并":
  587. case "左下合并":
  588. case "右上合并":
  589. case "右下合并":
  590. case "上边合并":
  591. case "下边合并":
  592. case "左边合并":
  593. case "右边合并":
  594. mymerge.GetHVMergeWidthAndHeight(mydgv, array3[num4], num36, ref mywidth, ref num40, ref num27, ref num26, ref num24, ref num25, ref firstcolumn, ref firstrow, ref firstc, ref firstr);
  595. num28 = num26 + num27 + 1;
  596. num23 = num24 + num25 + 1;
  597. break;
  598. case "水平合并":
  599. case "#水平合并#":
  600. case "左合并":
  601. case "右合并":
  602. mywidth = (int)Math.Round(mymerge.GetHMergeWidth(mydgv, array3[num4], num36, ref num28, ref num26, ref num27, ref firstcolumn, ref firstc, iscancheckheight: true));
  603. break;
  604. }
  605. break;
  606. }
  607. case "垂直合并":
  608. case "#垂直合并#":
  609. case "上合并":
  610. case "下合并":
  611. {
  612. int firstrow = 0;
  613. int firstr = 0;
  614. float num40 = mymerge.GetVMergeHeight(mydgv, array3[num4], num36, ref num23, ref num25, ref num24, ref firstrow, ref firstr);
  615. break;
  616. }
  617. }
  618. if (unchecked(num26 > 0 || num24 > 0))
  619. {
  620. continue;
  621. }
  622. if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewImageColumn", TextCompare: false) == 0)
  623. {
  624. if (mydgv.Rows[num36].Cells[array3[num4]].Value != null)
  625. {
  626. Image img = (Image)mydgv.Rows[num36].Cells[array3[num4]].FormattedValue;
  627. byte[] byteByImage = DGVtoExcel.GetByteByImage(img);
  628. int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
  629. IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
  630. XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, num4, num7 + num36, num4 + 1, num7 + num36 + 1);
  631. drawing.CreatePicture(anchor, pictureIndex);
  632. }
  633. }
  634. else if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
  635. {
  636. if (mydgv.Rows[num36].Cells[array3[num4]].Value != null && mydgv.Rows[num36].Cells[array3[num4]].Value != DBNull.Value && Conversions.ToBoolean(mydgv.Rows[num36].Cells[array3[num4]].Value))
  637. {
  638. DGVtoExcel.SetCellValue(hSSFSheet, num7 + num36, num4, "√");
  639. }
  640. flag2 = false;
  641. }
  642. else
  643. {
  644. string text2 = Conversions.ToString(mydgv.Rows[num36].Cells[array3[num4]].FormattedValue) ?? "";
  645. flag2 = Strings.InStr(text2, "\r\n") > 0;
  646. switch (num39)
  647. {
  648. case 1:
  649. if (Versioned.IsNumeric(text2))
  650. {
  651. DGVtoExcel.SetCellValue(hSSFSheet, num7 + num36, num4, Conversions.ToDouble(text2));
  652. }
  653. else
  654. {
  655. DGVtoExcel.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
  656. }
  657. break;
  658. case 2:
  659. DGVtoExcel.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
  660. break;
  661. default:
  662. DGVtoExcel.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
  663. break;
  664. }
  665. }
  666. DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(mydgv.Rows[num36].Cells[array3[num4]]);
  667. DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
  668. StringAlignment valign = StringAlignment.Center;
  669. StringAlignment halign = StringAlignment.Center;
  670. switch (alignment)
  671. {
  672. case DataGridViewContentAlignment.BottomCenter:
  673. valign = StringAlignment.Far;
  674. halign = StringAlignment.Center;
  675. break;
  676. case DataGridViewContentAlignment.MiddleCenter:
  677. valign = StringAlignment.Center;
  678. halign = StringAlignment.Center;
  679. break;
  680. case DataGridViewContentAlignment.TopCenter:
  681. valign = StringAlignment.Near;
  682. halign = StringAlignment.Center;
  683. break;
  684. case DataGridViewContentAlignment.BottomLeft:
  685. valign = StringAlignment.Far;
  686. halign = StringAlignment.Near;
  687. break;
  688. case DataGridViewContentAlignment.MiddleLeft:
  689. valign = StringAlignment.Center;
  690. halign = StringAlignment.Near;
  691. break;
  692. case DataGridViewContentAlignment.TopLeft:
  693. valign = StringAlignment.Near;
  694. halign = StringAlignment.Near;
  695. break;
  696. case DataGridViewContentAlignment.BottomRight:
  697. valign = StringAlignment.Far;
  698. halign = StringAlignment.Far;
  699. break;
  700. case DataGridViewContentAlignment.MiddleRight:
  701. valign = StringAlignment.Center;
  702. halign = StringAlignment.Far;
  703. break;
  704. case DataGridViewContentAlignment.TopRight:
  705. valign = StringAlignment.Near;
  706. halign = StringAlignment.Far;
  707. break;
  708. }
  709. if (string.Concat(mydgv.Rows[num36].Cells[array3[num4]].Tag).Contains("合并"))
  710. {
  711. valign = StringAlignment.Center;
  712. halign = StringAlignment.Center;
  713. }
  714. if (!flag2 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
  715. {
  716. flag2 = true;
  717. }
  718. Color dGVCellBackColor = Module1.GetDGVCellBackColor(mydgv.Rows[num36].Cells[array3[num4]]);
  719. Color dGVCellForeColor = Module1.GetDGVCellForeColor(mydgv.Rows[num36].Cells[array3[num4]]);
  720. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num7 + num36, num4, leftborder: true, rightborder: true, topborder: true, bottomborder: true, halign, valign, Module1.GetDGVCellFont(mydgv.Rows[num36].Cells[array3[num4]]), dGVCellBackColor, dGVCellForeColor, flag2);
  721. if (unchecked(num28 == 1 && num23 == 1))
  722. {
  723. continue;
  724. }
  725. int num41 = num7 + num36;
  726. int num42 = num7 + num36 + num25;
  727. for (int num43 = num41; num43 <= num42; num43++)
  728. {
  729. int num44 = num4;
  730. int num45 = num4 + num27;
  731. for (int num46 = num44; num46 <= num45; num46++)
  732. {
  733. unchecked
  734. {
  735. if (num43 != checked(num7 + num36) || num46 != num4)
  736. {
  737. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num43, num46, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  738. }
  739. }
  740. }
  741. }
  742. DGVtoExcel.SetMergeCell(hSSFSheet, num7 + num36, num4, num7 + num36 + num25, num4 + num27);
  743. num4 += num27;
  744. }
  745. if (!((num36 + num7 == 1048575) & (num36 != mydgv.RowCount - 1)))
  746. {
  747. continue;
  748. }
  749. int num47 = 0;
  750. int num48 = num - 1;
  751. for (int num49 = num47; num49 <= num48; num49++)
  752. {
  753. hSSFSheet.SetColumnWidth(num49, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num49]].Width / 8.0) * 256.0));
  754. }
  755. num32++;
  756. excelNewSheetName = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, sheetname);
  757. hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
  758. num33 = 1;
  759. int num50 = num36 + 1;
  760. int num51 = mydgv.RowCount - 1;
  761. for (num4 = num50; num4 <= num51; num4++)
  762. {
  763. IRow row4 = hSSFSheet.CreateRow(num33 - 1);
  764. if (!mydgv.Rows[num4].Visible)
  765. {
  766. row4.ZeroHeight = true;
  767. }
  768. row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num4].Height, isX: false)) * 72f;
  769. if (num33 == 1048576)
  770. {
  771. break;
  772. }
  773. num33++;
  774. }
  775. num7 = -num36 - 1;
  776. }
  777. num7 = ((num33 != 0) ? (num33 - 1) : (num7 + mydgv.RowCount));
  778. if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
  779. {
  780. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, TableFooter, tablefooterfont);
  781. }
  782. DGVtoExcel.w_font = null;
  783. DGVtoExcel.w_ifont = null;
  784. DGVtoExcel.w_Cellstyle = null;
  785. DGVtoExcel.w_iCellstyle = null;
  786. DGVtoExcel.w_color = null;
  787. int num52 = 0;
  788. int num53 = num - 1;
  789. for (int num54 = num52; num54 <= num53; num54++)
  790. {
  791. hSSFSheet.SetColumnWidth(num54, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num54]].Width / 8.0) * 256.0));
  792. }
  793. ruler.Dispose();
  794. ruler = null;
  795. if (isshowprogress)
  796. {
  797. progressexcel.Close();
  798. progressexcel.Dispose();
  799. }
  800. hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(excelNewSheetName));
  801. if (!DGVtoExcel.WriteToFile(hSSFWorkbook, FileName))
  802. {
  803. hSSFWorkbook = null;
  804. result = false;
  805. }
  806. else
  807. {
  808. hSSFWorkbook = null;
  809. if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
  810. {
  811. try
  812. {
  813. Process.Start(FileName);
  814. }
  815. catch (Exception projectError3)
  816. {
  817. Logger.Error(projectError3, "projectError3");
  818. MessageBox.Show(projectError3.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  819. ProjectData.SetProjectError(projectError3);
  820. try
  821. {
  822. Thread.Sleep(100);
  823. Process.Start(FileName);
  824. }
  825. catch (Exception projectError4)
  826. {
  827. Logger.Error(projectError4, "projectError4");
  828. MessageBox.Show(projectError4.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  829. ProjectData.SetProjectError(projectError4);
  830. ProjectData.ClearProjectError();
  831. }
  832. ProjectData.ClearProjectError();
  833. }
  834. }
  835. result = true;
  836. }
  837. }
  838. }
  839. catch (Exception projectError5)
  840. {
  841. Logger.Error(projectError5, "projectError5");
  842. MessageBox.Show(projectError5.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  843. ProjectData.SetProjectError(projectError5);
  844. if (isshowprogress)
  845. {
  846. progressexcel.Close();
  847. progressexcel.Dispose();
  848. }
  849. try
  850. {
  851. ruler.Dispose();
  852. ruler = null;
  853. }
  854. catch (Exception projectError6)
  855. {
  856. Logger.Error(projectError6, "projectError6");
  857. MessageBox.Show(projectError6.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  858. ProjectData.SetProjectError(projectError6);
  859. ProjectData.ClearProjectError();
  860. }
  861. if (Operators.CompareString(FileName, "", TextCompare: false) == 0)
  862. {
  863. result = DGVtoExcel.myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: true);
  864. ProjectData.ClearProjectError();
  865. }
  866. else
  867. {
  868. result = DGVtoExcel.myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: false);
  869. ProjectData.ClearProjectError();
  870. }
  871. }
  872. }
  873. return result;
  874. }
  875. }
  876. public bool ExportExcel(List<DataGridView> mydgvs, List<TreeView> columnheadertrees, Font titlefont, string title, string FileName, Font tableheaderfont, string Tableheader, Font tablefooterfont, string TableFooter, List<string> sheetnames, bool isopenfile, bool isshowprogress, bool isshowSheetTitle, bool isTopNodeFill)
  877. {
  878. bool result = true;
  879. checked
  880. {
  881. if (mydgvs == null || mydgvs.Count == 0 || sheetnames == null || sheetnames.Count == 0 || mydgvs.Count != sheetnames.Count)
  882. {
  883. result = false;
  884. }
  885. else
  886. {
  887. if (columnheadertrees != null && columnheadertrees.Count > 0 && mydgvs.Count != columnheadertrees.Count)
  888. {
  889. return false;
  890. }
  891. int num = 0;
  892. int num2 = 0;
  893. for (int i = 0; i < mydgvs.Count; i++)
  894. {
  895. num += mydgvs[i].Rows.Count;
  896. }
  897. if (titlefont == null)
  898. {
  899. titlefont = new Font("黑体", 18f, FontStyle.Bold);
  900. }
  901. if (tableheaderfont == null)
  902. {
  903. tableheaderfont = new Font("宋体", 12f);
  904. }
  905. if (tablefooterfont == null)
  906. {
  907. tablefooterfont = new Font("宋体", 12f);
  908. }
  909. progressexcel progressexcel = null;
  910. Module1.G_CancelExportDGVToExcel = false;
  911. if (isshowprogress)
  912. {
  913. progressexcel = new progressexcel();
  914. progressexcel.TopMost = true;
  915. progressexcel.Visible = true;
  916. }
  917. Ruler ruler = new Ruler();
  918. try
  919. {
  920. if (isshowprogress)
  921. {
  922. progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
  923. progressexcel.ProgressBar1.Value = 0;
  924. Application.DoEvents();
  925. }
  926. bool flag = false;
  927. DataGridView dataGridView = null;
  928. TreeView treeView = null;
  929. XSSFWorkbook hSSFWorkbook = null;
  930. string empty = string.Empty;
  931. for (int j = 0; j < mydgvs.Count; j++)
  932. {
  933. empty = sheetnames[j];
  934. dataGridView = mydgvs[j];
  935. if (columnheadertrees != null && columnheadertrees.Count > 0)
  936. {
  937. treeView = columnheadertrees[j];
  938. }
  939. int columnCount = dataGridView.ColumnCount;
  940. int[] array = new int[columnCount - 1 + 1];
  941. int[] array2 = new int[columnCount - 1 + 1];
  942. int num3 = 0;
  943. int num4 = 0;
  944. int num5 = columnCount - 1;
  945. for (int k = num4; k <= num5; k++)
  946. {
  947. array[k] = k;
  948. array2[k] = dataGridView.Columns[k].DisplayIndex;
  949. if (dataGridView.Columns[k].Visible)
  950. {
  951. num3++;
  952. }
  953. }
  954. if (num3 <= 0)
  955. {
  956. if (isshowprogress)
  957. {
  958. progressexcel.Close();
  959. progressexcel.Dispose();
  960. }
  961. result = true;
  962. }
  963. else
  964. {
  965. Array.Sort(array2, array);
  966. int[] array3 = new int[num3 - 1 + 1];
  967. int num6 = 0;
  968. int num7 = 0;
  969. int num8 = columnCount - 1;
  970. for (int l = num7; l <= num8; l++)
  971. {
  972. if (dataGridView.Columns[array[l]].Visible)
  973. {
  974. array3[num6] = array[l];
  975. num6++;
  976. }
  977. }
  978. DGVtoExcel.w_ZDYcolors = 8;
  979. int endcol = num3 - 1;
  980. if (!flag)
  981. {
  982. SaveFileDialog saveFileDialog = new SaveFileDialog();
  983. saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
  984. saveFileDialog.AddExtension = true;
  985. saveFileDialog.OverwritePrompt = false;
  986. saveFileDialog.DefaultExt = "xls";
  987. saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
  988. saveFileDialog.FileName = FileName;
  989. if (isshowprogress)
  990. {
  991. progressexcel.Visible = false;
  992. }
  993. if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
  994. {
  995. if (isshowprogress)
  996. {
  997. progressexcel.Close();
  998. progressexcel.Dispose();
  999. }
  1000. saveFileDialog.Dispose();
  1001. result = false;
  1002. return result;
  1003. }
  1004. FileName = saveFileDialog.FileName;
  1005. saveFileDialog.Dispose();
  1006. if (isshowprogress)
  1007. {
  1008. progressexcel.Visible = true;
  1009. }
  1010. DGVtoExcel.w_font = null;
  1011. DGVtoExcel.w_ifont = null;
  1012. DGVtoExcel.w_Cellstyle = null;
  1013. DGVtoExcel.w_iCellstyle = null;
  1014. DGVtoExcel.w_color = null;
  1015. if (MyProject.Computer.FileSystem.FileExists(FileName))
  1016. {
  1017. if (isshowprogress)
  1018. {
  1019. progressexcel.Visible = false;
  1020. }
  1021. switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
  1022. {
  1023. case MsgBoxResult.Cancel:
  1024. if (isshowprogress)
  1025. {
  1026. progressexcel.Close();
  1027. progressexcel.Dispose();
  1028. }
  1029. result = false;
  1030. return result;
  1031. case MsgBoxResult.Yes:
  1032. try
  1033. {
  1034. MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
  1035. }
  1036. catch (Exception projectError)
  1037. {
  1038. Logger.Error(projectError, "projectError");
  1039. MessageBox.Show(projectError.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1040. ProjectData.SetProjectError(projectError);
  1041. Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
  1042. if (isshowprogress)
  1043. {
  1044. progressexcel.Close();
  1045. progressexcel.Dispose();
  1046. }
  1047. result = false;
  1048. ProjectData.ClearProjectError();
  1049. return result;
  1050. }
  1051. break;
  1052. }
  1053. if (isshowprogress)
  1054. {
  1055. progressexcel.Visible = true;
  1056. }
  1057. }
  1058. if (MyProject.Computer.FileSystem.FileExists(FileName))
  1059. {
  1060. FileStream fileStream;
  1061. try
  1062. {
  1063. fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
  1064. }
  1065. catch (Exception ex)
  1066. {
  1067. Logger.Error(ex, "list-ex");
  1068. MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1069. ProjectData.SetProjectError(ex);
  1070. Exception ex2 = ex;
  1071. if (isshowprogress)
  1072. {
  1073. progressexcel.Close();
  1074. progressexcel.Dispose();
  1075. }
  1076. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
  1077. fileStream = null;
  1078. result = false;
  1079. ProjectData.ClearProjectError();
  1080. return result;
  1081. }
  1082. try
  1083. {
  1084. hSSFWorkbook = new XSSFWorkbook(fileStream);
  1085. }
  1086. catch (Exception projectError2)
  1087. {
  1088. Logger.Error(projectError2, "list-projectError2");
  1089. MessageBox.Show(projectError2.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1090. ProjectData.SetProjectError(projectError2);
  1091. if (isshowprogress)
  1092. {
  1093. progressexcel.Close();
  1094. progressexcel.Dispose();
  1095. }
  1096. fileStream.Dispose();
  1097. fileStream = null;
  1098. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
  1099. hSSFWorkbook = null;
  1100. result = false;
  1101. ProjectData.ClearProjectError();
  1102. return result;
  1103. }
  1104. }
  1105. else
  1106. {
  1107. hSSFWorkbook = new XSSFWorkbook();
  1108. }
  1109. flag = true;
  1110. }
  1111. string excelNewSheetName = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, empty);
  1112. XSSFSheet hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
  1113. int num9;
  1114. if (Operators.CompareString(title, "", TextCompare: false) == 0)
  1115. {
  1116. num9 = 0;
  1117. }
  1118. else if (isshowSheetTitle)
  1119. {
  1120. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, 0, 0, 0, endcol, empty, titlefont);
  1121. num9 = 1;
  1122. }
  1123. else
  1124. {
  1125. num9 = 0;
  1126. }
  1127. if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
  1128. {
  1129. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, Tableheader, tableheaderfont);
  1130. num9++;
  1131. }
  1132. DGVtoExcel.w_gridcolor = DGVtoExcel.GetXLColour(hSSFWorkbook, dataGridView.GridColor);
  1133. if (dataGridView.ColumnHeadersVisible)
  1134. {
  1135. if (treeView == null)
  1136. {
  1137. IRow row = hSSFSheet.CreateRow(num9);
  1138. row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.ColumnHeadersHeight, isX: false)) * 72f;
  1139. int num10 = 0;
  1140. int num11 = num3 - 1;
  1141. for (int m = num10; m <= num11; m++)
  1142. {
  1143. DGVtoExcel.SetCellValue(hSSFSheet, num9, m, dataGridView.Columns[array3[m]].HeaderText);
  1144. Font dGVHeaderFont = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[m]]);
  1145. Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[m]]);
  1146. Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[m]]);
  1147. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, m, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
  1148. }
  1149. }
  1150. else
  1151. {
  1152. mytree mytree = new mytree(treeView, dataGridView);
  1153. int leves = mytree.GetLeves();
  1154. int num12 = 1;
  1155. int num13 = leves;
  1156. for (int n = num12; n <= num13; n++)
  1157. {
  1158. IRow row2 = hSSFSheet.CreateRow(num9 + n - 1);
  1159. row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)dataGridView.ColumnHeadersHeight / (double)leves), isX: false)) * 72f;
  1160. }
  1161. int num14 = 1;
  1162. int num15 = leves;
  1163. bool flag2 = true;
  1164. for (int num16 = num14; num16 <= num15; num16++)
  1165. {
  1166. if (num16 != 1)
  1167. {
  1168. num9++;
  1169. }
  1170. int num17 = 0;
  1171. int num18 = num3 - 1;
  1172. for (int num19 = num17; num19 <= num18; num19++)
  1173. {
  1174. string nodeText = mytree.GetNodeText(array3[num19], num16);
  1175. int cellHeightLevels = mytree.GetCellHeightLevels(array3[num19], num16, isTopNodeFill, out flag2);
  1176. int cellWidthCols = mytree.GetCellWidthCols(array3[num19], num16);
  1177. if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
  1178. {
  1179. continue;
  1180. }
  1181. Font font = mytree.GetNodeFont(array3[num19], num16);
  1182. if (font == null)
  1183. {
  1184. font = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[num19]]);
  1185. }
  1186. Color forcolor = mytree.GetNodeForeColor(array3[num19], num16);
  1187. if (forcolor.IsEmpty)
  1188. {
  1189. forcolor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[num19]]);
  1190. }
  1191. Color bkcolor = mytree.GetNodeBackColor(array3[num19], num16);
  1192. if (bkcolor.IsEmpty)
  1193. {
  1194. bkcolor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[num19]]);
  1195. }
  1196. if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
  1197. {
  1198. int num20 = num9;
  1199. if (!flag2)
  1200. {
  1201. num20++;
  1202. }
  1203. DGVtoExcel.SetCellValue(hSSFSheet, num20, num19, nodeText);
  1204. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num20, num19, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  1205. continue;
  1206. }
  1207. DGVtoExcel.SetCellValue(hSSFSheet, num9, num19, nodeText);
  1208. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, num19, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  1209. int num21 = num9;
  1210. int num22 = num9 + cellHeightLevels - 1;
  1211. for (int num23 = num21; num23 <= num22; num23++)
  1212. {
  1213. int num24 = num19;
  1214. int num25 = num19 + cellWidthCols - 1;
  1215. for (int num26 = num24; num26 <= num25; num26++)
  1216. {
  1217. if (unchecked(num23 != num9 || num26 != num19))
  1218. {
  1219. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num23, num26, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  1220. }
  1221. }
  1222. }
  1223. DGVtoExcel.SetMergeCell(hSSFSheet, num9, num19, num9 + cellHeightLevels - 1, num19 + cellWidthCols - 1);
  1224. }
  1225. }
  1226. mytree.Dispose();
  1227. }
  1228. num9++;
  1229. }
  1230. int num27 = 1;
  1231. int num28 = 0;
  1232. int num29 = 0;
  1233. int num30 = 0;
  1234. int num31 = 0;
  1235. int num32 = 1;
  1236. int num33 = 0;
  1237. int num34 = dataGridView.RowCount - 1;
  1238. for (int num35 = num33; num35 <= num34; num35++)
  1239. {
  1240. IRow row3 = hSSFSheet.CreateRow(num9 + num35);
  1241. row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num35].Height, isX: false)) * 72f;
  1242. if (!dataGridView.Rows[num35].Visible)
  1243. {
  1244. row3.ZeroHeight = true;
  1245. }
  1246. //if (num35 + num9 == 1048575)
  1247. {
  1248. break;
  1249. }
  1250. }
  1251. int num36 = 0;
  1252. int num37 = 0;
  1253. if (isshowprogress)
  1254. {
  1255. progressexcel.ProgressBar1.Maximum = num;
  1256. }
  1257. int num38 = 0;
  1258. int num39 = dataGridView.RowCount - 1;
  1259. for (int num40 = num38; num40 <= num39; num40++)
  1260. {
  1261. bool flag3 = false;
  1262. if (isshowprogress)
  1263. {
  1264. progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num40 + num2 + 1) + "〗条记录,共〖" + Conversions.ToString(num) + "〗条记录,已完成〖" + Strings.Format((double)(num40 + num2 + 1) / (double)num, "###.##%") + "〗";
  1265. progressexcel.ProgressBar1.Value = num40 + 1;
  1266. Application.DoEvents();
  1267. }
  1268. if (Module1.G_CancelExportDGVToExcel)
  1269. {
  1270. break;
  1271. }
  1272. int num41 = 0;
  1273. int num42 = num3 - 1;
  1274. for (num6 = num41; num6 <= num42; num6++)
  1275. {
  1276. int num43;
  1277. if (dataGridView.Columns[array3[num6]].ValueType == null)
  1278. {
  1279. num43 = 0;
  1280. }
  1281. else
  1282. {
  1283. switch (Strings.Left(dataGridView.Columns[array3[num6]].ValueType.Name.ToUpper(), 3))
  1284. {
  1285. case "STR":
  1286. num43 = 0;
  1287. break;
  1288. case "INT":
  1289. case "DEC":
  1290. num43 = 1;
  1291. flag3 = false;
  1292. break;
  1293. case "DAT":
  1294. num43 = 2;
  1295. break;
  1296. case "BOO":
  1297. num43 = 3;
  1298. flag3 = false;
  1299. break;
  1300. default:
  1301. num43 = 0;
  1302. break;
  1303. }
  1304. }
  1305. num32 = 1;
  1306. num27 = 1;
  1307. num30 = 0;
  1308. num28 = 0;
  1309. num31 = 0;
  1310. num30 = 0;
  1311. num28 = 0;
  1312. num29 = 0;
  1313. string text = Conversions.ToString(dataGridView.Rows[num40].Cells[array3[num6]].Tag);
  1314. switch (text)
  1315. {
  1316. default:
  1317. {
  1318. float mywidth = 0f;
  1319. float num44 = 9f;
  1320. int firstcolumn = 0;
  1321. int firstrow = 0;
  1322. int firstc = 0;
  1323. int firstr = 0;
  1324. switch (text)
  1325. {
  1326. default:
  1327. num32 = 1;
  1328. num27 = 1;
  1329. break;
  1330. case "合并":
  1331. case "左上合并":
  1332. case "左下合并":
  1333. case "右上合并":
  1334. case "右下合并":
  1335. case "上边合并":
  1336. case "下边合并":
  1337. case "左边合并":
  1338. case "右边合并":
  1339. mymerge.GetHVMergeWidthAndHeight(dataGridView, array3[num6], num40, ref mywidth, ref num44, ref num31, ref num30, ref num28, ref num29, ref firstcolumn, ref firstrow, ref firstc, ref firstr);
  1340. num32 = num30 + num31 + 1;
  1341. num27 = num28 + num29 + 1;
  1342. break;
  1343. case "水平合并":
  1344. case "#水平合并#":
  1345. case "左合并":
  1346. case "右合并":
  1347. mywidth = (int)Math.Round(mymerge.GetHMergeWidth(dataGridView, array3[num6], num40, ref num32, ref num30, ref num31, ref firstcolumn, ref firstc, iscancheckheight: true));
  1348. break;
  1349. }
  1350. break;
  1351. }
  1352. case "垂直合并":
  1353. case "#垂直合并#":
  1354. case "上合并":
  1355. case "下合并":
  1356. {
  1357. int firstrow = 0;
  1358. int firstr = 0;
  1359. float num44 = mymerge.GetVMergeHeight(dataGridView, array3[num6], num40, ref num27, ref num29, ref num28, ref firstrow, ref firstr);
  1360. break;
  1361. }
  1362. }
  1363. if (unchecked(num30 > 0 || num28 > 0))
  1364. {
  1365. continue;
  1366. }
  1367. if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewImageColumn", TextCompare: false) == 0)
  1368. {
  1369. if (dataGridView.Rows[num40].Cells[array3[num6]].Value != null)
  1370. {
  1371. Image img = (Image)dataGridView.Rows[num40].Cells[array3[num6]].FormattedValue;
  1372. byte[] byteByImage = DGVtoExcel.GetByteByImage(img);
  1373. int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
  1374. IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
  1375. XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, num6, num9 + num40, num6 + 1, num9 + num40 + 1);
  1376. drawing.CreatePicture(anchor, pictureIndex);
  1377. }
  1378. }
  1379. else if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
  1380. {
  1381. if (dataGridView.Rows[num40].Cells[array3[num6]].Value != null && dataGridView.Rows[num40].Cells[array3[num6]].Value != DBNull.Value && Conversions.ToBoolean(dataGridView.Rows[num40].Cells[array3[num6]].FormattedValue))
  1382. {
  1383. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num40, num6, "√");
  1384. }
  1385. flag3 = false;
  1386. }
  1387. else
  1388. {
  1389. string text2 = Conversions.ToString(dataGridView.Rows[num40].Cells[array3[num6]].FormattedValue) ?? "";
  1390. flag3 = Strings.InStr(text2, "\r\n") > 0;
  1391. switch (num43)
  1392. {
  1393. case 1:
  1394. if (Versioned.IsNumeric(text2))
  1395. {
  1396. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num40, num6, Conversions.ToDouble(text2));
  1397. }
  1398. else
  1399. {
  1400. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
  1401. }
  1402. break;
  1403. case 2:
  1404. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
  1405. break;
  1406. default:
  1407. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
  1408. break;
  1409. }
  1410. }
  1411. DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(dataGridView.Rows[num40].Cells[array3[num6]]);
  1412. DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
  1413. StringAlignment valign = StringAlignment.Center;
  1414. StringAlignment halign = StringAlignment.Center;
  1415. switch (alignment)
  1416. {
  1417. case DataGridViewContentAlignment.BottomCenter:
  1418. valign = StringAlignment.Far;
  1419. halign = StringAlignment.Center;
  1420. break;
  1421. case DataGridViewContentAlignment.MiddleCenter:
  1422. valign = StringAlignment.Center;
  1423. halign = StringAlignment.Center;
  1424. break;
  1425. case DataGridViewContentAlignment.TopCenter:
  1426. valign = StringAlignment.Near;
  1427. halign = StringAlignment.Center;
  1428. break;
  1429. case DataGridViewContentAlignment.BottomLeft:
  1430. valign = StringAlignment.Far;
  1431. halign = StringAlignment.Near;
  1432. break;
  1433. case DataGridViewContentAlignment.MiddleLeft:
  1434. valign = StringAlignment.Center;
  1435. halign = StringAlignment.Near;
  1436. break;
  1437. case DataGridViewContentAlignment.TopLeft:
  1438. valign = StringAlignment.Near;
  1439. halign = StringAlignment.Near;
  1440. break;
  1441. case DataGridViewContentAlignment.BottomRight:
  1442. valign = StringAlignment.Far;
  1443. halign = StringAlignment.Far;
  1444. break;
  1445. case DataGridViewContentAlignment.MiddleRight:
  1446. valign = StringAlignment.Center;
  1447. halign = StringAlignment.Far;
  1448. break;
  1449. case DataGridViewContentAlignment.TopRight:
  1450. valign = StringAlignment.Near;
  1451. halign = StringAlignment.Far;
  1452. break;
  1453. }
  1454. if (string.Concat(dataGridView.Rows[num40].Cells[array3[num6]].Tag).Contains("合并"))
  1455. {
  1456. valign = StringAlignment.Center;
  1457. halign = StringAlignment.Center;
  1458. }
  1459. if (!flag3 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
  1460. {
  1461. flag3 = true;
  1462. }
  1463. Color dGVCellBackColor = Module1.GetDGVCellBackColor(dataGridView.Rows[num40].Cells[array3[num6]]);
  1464. Color dGVCellForeColor = Module1.GetDGVCellForeColor(dataGridView.Rows[num40].Cells[array3[num6]]);
  1465. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9 + num40, num6, leftborder: true, rightborder: true, topborder: true, bottomborder: true, halign, valign, Module1.GetDGVCellFont(dataGridView.Rows[num40].Cells[array3[num6]]), dGVCellBackColor, dGVCellForeColor, flag3);
  1466. if (unchecked(num32 == 1 && num27 == 1))
  1467. {
  1468. continue;
  1469. }
  1470. int num45 = num9 + num40;
  1471. int num46 = num9 + num40 + num29;
  1472. for (int num47 = num45; num47 <= num46; num47++)
  1473. {
  1474. int num48 = num6;
  1475. int num49 = num6 + num31;
  1476. for (int num50 = num48; num50 <= num49; num50++)
  1477. {
  1478. unchecked
  1479. {
  1480. if (num47 != checked(num9 + num40) || num50 != num6)
  1481. {
  1482. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num47, num50, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  1483. }
  1484. }
  1485. }
  1486. }
  1487. DGVtoExcel.SetMergeCell(hSSFSheet, num9 + num40, num6, num9 + num40 + num29, num6 + num31);
  1488. num6 += num31;
  1489. }
  1490. if (!((num40 + num9 == 1048575) & (num40 != dataGridView.RowCount - 1)))
  1491. {
  1492. continue;
  1493. }
  1494. int num51 = 0;
  1495. int num52 = num3 - 1;
  1496. for (int num53 = num51; num53 <= num52; num53++)
  1497. {
  1498. hSSFSheet.SetColumnWidth(num53, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num53]].Width / 8.0) * 256.0));
  1499. }
  1500. num36++;
  1501. excelNewSheetName = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, empty);
  1502. hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
  1503. num37 = 1;
  1504. int num54 = num40 + 1;
  1505. int num55 = dataGridView.RowCount - 1;
  1506. for (num6 = num54; num6 <= num55; num6++)
  1507. {
  1508. IRow row4 = hSSFSheet.CreateRow(num37 - 1);
  1509. if (!dataGridView.Rows[num6].Visible)
  1510. {
  1511. row4.ZeroHeight = true;
  1512. }
  1513. row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num6].Height, isX: false)) * 72f;
  1514. if (num37 == 1048576)
  1515. {
  1516. break;
  1517. }
  1518. num37++;
  1519. }
  1520. num9 = -num40 - 1;
  1521. }
  1522. num9 = ((num37 != 0) ? (num37 - 1) : (num9 + dataGridView.RowCount));
  1523. if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
  1524. {
  1525. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, TableFooter, tablefooterfont);
  1526. }
  1527. DGVtoExcel.w_font = null;
  1528. DGVtoExcel.w_ifont = null;
  1529. DGVtoExcel.w_Cellstyle = null;
  1530. DGVtoExcel.w_iCellstyle = null;
  1531. DGVtoExcel.w_color = null;
  1532. int num56 = 0;
  1533. int num57 = num3 - 1;
  1534. for (int num58 = num56; num58 <= num57; num58++)
  1535. {
  1536. hSSFSheet.SetColumnWidth(num58, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num58]].Width / 8.0) * 256.0));
  1537. }
  1538. hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(excelNewSheetName));
  1539. }
  1540. num2 += mydgvs[j].Rows.Count;
  1541. }
  1542. ruler.Dispose();
  1543. ruler = null;
  1544. if (isshowprogress)
  1545. {
  1546. progressexcel.Close();
  1547. progressexcel.Dispose();
  1548. }
  1549. if (!DGVtoExcel.WriteToFile(hSSFWorkbook, FileName))
  1550. {
  1551. hSSFWorkbook = null;
  1552. result = false;
  1553. }
  1554. else
  1555. {
  1556. hSSFWorkbook = null;
  1557. if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
  1558. {
  1559. try
  1560. {
  1561. Process.Start(FileName);
  1562. }
  1563. catch (Exception projectError3)
  1564. {
  1565. Logger.Error(projectError3, "list-projectError3");
  1566. MessageBox.Show(projectError3.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1567. ProjectData.SetProjectError(projectError3);
  1568. try
  1569. {
  1570. Thread.Sleep(100);
  1571. Process.Start(FileName);
  1572. }
  1573. catch (Exception projectError4)
  1574. {
  1575. Logger.Error(projectError4, "list-projectError4");
  1576. MessageBox.Show(projectError4.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1577. ProjectData.SetProjectError(projectError4);
  1578. ProjectData.ClearProjectError();
  1579. }
  1580. ProjectData.ClearProjectError();
  1581. }
  1582. }
  1583. result = true;
  1584. }
  1585. }
  1586. catch (Exception projectError5)
  1587. {
  1588. Logger.Error(projectError5, "list-projectError5");
  1589. MessageBox.Show(projectError5.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1590. ProjectData.SetProjectError(projectError5);
  1591. if (isshowprogress)
  1592. {
  1593. progressexcel.Close();
  1594. progressexcel.Dispose();
  1595. }
  1596. try
  1597. {
  1598. ruler.Dispose();
  1599. ruler = null;
  1600. }
  1601. catch (Exception projectError6)
  1602. {
  1603. Logger.Error(projectError6, "list-projectError6");
  1604. MessageBox.Show(projectError6.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1605. ProjectData.SetProjectError(projectError6);
  1606. ProjectData.ClearProjectError();
  1607. }
  1608. }
  1609. }
  1610. return result;
  1611. }
  1612. }
  1613. /*
  1614. public bool ExportExcelOneSheet(List<DataGridView> mydgvs, List<TreeView> columnheadertrees, Font titlefont, string title, string FileName, Font tableheaderfont, string Tableheader, Font tablefooterfont, string TableFooter, List<string> sheetnames, bool isopenfile, bool isshowprogress, bool isTopNodeFill)
  1615. {
  1616. bool result = true;
  1617. int num = 0;
  1618. checked
  1619. {
  1620. if (mydgvs == null || mydgvs.Count == 0 || sheetnames == null || sheetnames.Count == 0 || mydgvs.Count != sheetnames.Count)
  1621. {
  1622. result = false;
  1623. }
  1624. else
  1625. {
  1626. if (columnheadertrees != null && columnheadertrees.Count > 0 && mydgvs.Count != columnheadertrees.Count)
  1627. {
  1628. return false;
  1629. }
  1630. int num2 = 0;
  1631. if (titlefont == null)
  1632. {
  1633. titlefont = new Font("黑体", 18f, FontStyle.Bold);
  1634. }
  1635. if (tableheaderfont == null)
  1636. {
  1637. tableheaderfont = new Font("宋体", 12f);
  1638. }
  1639. if (tablefooterfont == null)
  1640. {
  1641. tablefooterfont = new Font("宋体", 12f);
  1642. }
  1643. progressexcel progressexcel = null;
  1644. Module1.G_CancelExportDGVToExcel = false;
  1645. if (isshowprogress)
  1646. {
  1647. progressexcel = new progressexcel();
  1648. progressexcel.TopMost = true;
  1649. progressexcel.Visible = true;
  1650. }
  1651. Ruler ruler = new Ruler();
  1652. try
  1653. {
  1654. if (isshowprogress)
  1655. {
  1656. progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
  1657. progressexcel.ProgressBar1.Value = 0;
  1658. Application.DoEvents();
  1659. }
  1660. bool flag = false;
  1661. DataGridView dataGridView = null;
  1662. TreeView treeView = null;
  1663. XSSFWorkbook hSSFWorkbook = null;
  1664. XSSFSheet hSSFSheet = null;
  1665. string empty = string.Empty;
  1666. string text = string.Empty;
  1667. for (int i = 0; i < mydgvs.Count; i++)
  1668. {
  1669. empty = sheetnames[i];
  1670. dataGridView = mydgvs[i];
  1671. if (columnheadertrees != null && columnheadertrees.Count > 0)
  1672. {
  1673. treeView = columnheadertrees[i];
  1674. }
  1675. int columnCount = dataGridView.ColumnCount;
  1676. int[] array = new int[columnCount - 1 + 1];
  1677. int[] array2 = new int[columnCount - 1 + 1];
  1678. int num3 = 0;
  1679. int num4 = 0;
  1680. int num5 = columnCount - 1;
  1681. for (int j = num4; j <= num5; j++)
  1682. {
  1683. array[j] = j;
  1684. array2[j] = dataGridView.Columns[j].DisplayIndex;
  1685. if (dataGridView.Columns[j].Visible)
  1686. {
  1687. num3++;
  1688. }
  1689. }
  1690. if (num3 <= 0)
  1691. {
  1692. if (isshowprogress)
  1693. {
  1694. progressexcel.Close();
  1695. progressexcel.Dispose();
  1696. }
  1697. result = true;
  1698. }
  1699. else
  1700. {
  1701. Array.Sort(array2, array);
  1702. int[] array3 = new int[num3 - 1 + 1];
  1703. int num6 = 0;
  1704. int num7 = 0;
  1705. int num8 = columnCount - 1;
  1706. for (int k = num7; k <= num8; k++)
  1707. {
  1708. if (dataGridView.Columns[array[k]].Visible)
  1709. {
  1710. array3[num6] = array[k];
  1711. num6++;
  1712. }
  1713. }
  1714. DGVtoExcel.w_ZDYcolors = 8;
  1715. int endcol = num3 - 1;
  1716. if (!flag)
  1717. {
  1718. SaveFileDialog saveFileDialog = new SaveFileDialog();
  1719. saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
  1720. saveFileDialog.AddExtension = true;
  1721. saveFileDialog.OverwritePrompt = false;
  1722. saveFileDialog.DefaultExt = "xls";
  1723. saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
  1724. saveFileDialog.FileName = FileName;
  1725. if (isshowprogress)
  1726. {
  1727. progressexcel.Visible = false;
  1728. }
  1729. if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
  1730. {
  1731. if (isshowprogress)
  1732. {
  1733. progressexcel.Close();
  1734. progressexcel.Dispose();
  1735. }
  1736. saveFileDialog.Dispose();
  1737. result = false;
  1738. return result;
  1739. }
  1740. FileName = saveFileDialog.FileName;
  1741. saveFileDialog.Dispose();
  1742. if (isshowprogress)
  1743. {
  1744. progressexcel.Visible = true;
  1745. }
  1746. DGVtoExcel.w_font = null;
  1747. DGVtoExcel.w_ifont = null;
  1748. DGVtoExcel.w_Cellstyle = null;
  1749. DGVtoExcel.w_iCellstyle = null;
  1750. DGVtoExcel.w_color = null;
  1751. if (MyProject.Computer.FileSystem.FileExists(FileName))
  1752. {
  1753. if (isshowprogress)
  1754. {
  1755. progressexcel.Visible = false;
  1756. }
  1757. switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
  1758. {
  1759. case MsgBoxResult.Cancel:
  1760. if (isshowprogress)
  1761. {
  1762. progressexcel.Close();
  1763. progressexcel.Dispose();
  1764. }
  1765. result = false;
  1766. return result;
  1767. case MsgBoxResult.Yes:
  1768. try
  1769. {
  1770. MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
  1771. }
  1772. catch (Exception projectError)
  1773. {
  1774. ProjectData.SetProjectError(projectError);
  1775. Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
  1776. if (isshowprogress)
  1777. {
  1778. progressexcel.Close();
  1779. progressexcel.Dispose();
  1780. }
  1781. result = false;
  1782. ProjectData.ClearProjectError();
  1783. return result;
  1784. }
  1785. break;
  1786. }
  1787. if (isshowprogress)
  1788. {
  1789. progressexcel.Visible = true;
  1790. }
  1791. }
  1792. if (MyProject.Computer.FileSystem.FileExists(FileName))
  1793. {
  1794. FileStream fileStream;
  1795. try
  1796. {
  1797. fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
  1798. }
  1799. catch (Exception ex)
  1800. {
  1801. ProjectData.SetProjectError(ex);
  1802. Exception ex2 = ex;
  1803. if (isshowprogress)
  1804. {
  1805. progressexcel.Close();
  1806. progressexcel.Dispose();
  1807. }
  1808. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
  1809. fileStream = null;
  1810. result = false;
  1811. ProjectData.ClearProjectError();
  1812. return result;
  1813. }
  1814. try
  1815. {
  1816. hSSFWorkbook = new XSSFWorkbook(fileStream);
  1817. }
  1818. catch (Exception projectError2)
  1819. {
  1820. ProjectData.SetProjectError(projectError2);
  1821. if (isshowprogress)
  1822. {
  1823. progressexcel.Close();
  1824. progressexcel.Dispose();
  1825. }
  1826. fileStream.Dispose();
  1827. fileStream = null;
  1828. Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
  1829. hSSFWorkbook = null;
  1830. result = false;
  1831. ProjectData.ClearProjectError();
  1832. return result;
  1833. }
  1834. }
  1835. else
  1836. {
  1837. hSSFWorkbook = new XSSFWorkbook();
  1838. }
  1839. flag = true;
  1840. }
  1841. if (hSSFSheet == null)
  1842. {
  1843. text = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, title);
  1844. hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(text);
  1845. }
  1846. int num9;
  1847. if (Operators.CompareString(title, "", TextCompare: false) == 0)
  1848. {
  1849. num9 = 0;
  1850. }
  1851. else
  1852. {
  1853. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num2, num2, 0, endcol, empty, titlefont);
  1854. num9 = 1;
  1855. }
  1856. if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
  1857. {
  1858. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num9 + num2, num9 + num2, 0, endcol, Tableheader, tableheaderfont);
  1859. num9++;
  1860. }
  1861. num9 += num2;
  1862. DGVtoExcel.w_gridcolor = DGVtoExcel.GetXLColour(hSSFWorkbook, dataGridView.GridColor);
  1863. if (dataGridView.ColumnHeadersVisible)
  1864. {
  1865. if (treeView == null)
  1866. {
  1867. IRow row = hSSFSheet.CreateRow(num9);
  1868. row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.ColumnHeadersHeight, isX: false)) * 72f;
  1869. int num10 = 0;
  1870. int num11 = num3 - 1;
  1871. for (int l = num10; l <= num11; l++)
  1872. {
  1873. DGVtoExcel.SetCellValue(hSSFSheet, num9, l, dataGridView.Columns[array3[l]].HeaderText);
  1874. Font dGVHeaderFont = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[l]]);
  1875. Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[l]]);
  1876. Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[l]]);
  1877. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, l, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
  1878. }
  1879. }
  1880. else
  1881. {
  1882. mytree mytree = new mytree(treeView, dataGridView);
  1883. num = mytree.GetLeves();
  1884. int num12 = 1;
  1885. int num13 = num;
  1886. for (int m = num12; m <= num13; m++)
  1887. {
  1888. IRow row2 = hSSFSheet.CreateRow(num9 + m - 1);
  1889. row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)dataGridView.ColumnHeadersHeight / (double)num), isX: false)) * 72f;
  1890. }
  1891. int num14 = 1;
  1892. int num15 = num;
  1893. for (int n = num14; n <= num15; n++)
  1894. {
  1895. if (n != 1)
  1896. {
  1897. num9++;
  1898. }
  1899. int num16 = 0;
  1900. int num17 = num3 - 1;
  1901. bool flag2 = true;
  1902. for (int num18 = num16; num18 <= num17; num18++)
  1903. {
  1904. string nodeText = mytree.GetNodeText(array3[num18], n);
  1905. int cellHeightLevels = mytree.GetCellHeightLevels(array3[num18], n, isTopNodeFill, out flag2);
  1906. int cellWidthCols = mytree.GetCellWidthCols(array3[num18], n);
  1907. if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
  1908. {
  1909. continue;
  1910. }
  1911. Font font = mytree.GetNodeFont(array3[num18], n);
  1912. if (font == null)
  1913. {
  1914. font = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[num18]]);
  1915. }
  1916. Color forcolor = mytree.GetNodeForeColor(array3[num18], n);
  1917. if (forcolor.IsEmpty)
  1918. {
  1919. forcolor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[num18]]);
  1920. }
  1921. Color bkcolor = mytree.GetNodeBackColor(array3[num18], n);
  1922. if (bkcolor.IsEmpty)
  1923. {
  1924. bkcolor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[num18]]);
  1925. }
  1926. if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
  1927. {
  1928. int num19 = num9;
  1929. if (!flag2)
  1930. {
  1931. num19++;
  1932. }
  1933. DGVtoExcel.SetCellValue(hSSFSheet, num19, num18, nodeText);
  1934. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num19, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  1935. continue;
  1936. }
  1937. DGVtoExcel.SetCellValue(hSSFSheet, num9, num18, nodeText);
  1938. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
  1939. int num20 = num9;
  1940. int num21 = num9 + cellHeightLevels - 1;
  1941. for (int num22 = num20; num22 <= num21; num22++)
  1942. {
  1943. int num23 = num18;
  1944. int num24 = num18 + cellWidthCols - 1;
  1945. for (int num25 = num23; num25 <= num24; num25++)
  1946. {
  1947. if (unchecked(num22 != num9 || num25 != num18))
  1948. {
  1949. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num22, num25, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  1950. }
  1951. }
  1952. }
  1953. DGVtoExcel.SetMergeCell(hSSFSheet, num9, num18, num9 + cellHeightLevels - 1, num18 + cellWidthCols - 1);
  1954. }
  1955. }
  1956. mytree.Dispose();
  1957. }
  1958. num9++;
  1959. }
  1960. int num26 = 1;
  1961. int num27 = 0;
  1962. int num28 = 0;
  1963. int num29 = 0;
  1964. int num30 = 0;
  1965. int num31 = 1;
  1966. int num32 = 0;
  1967. int num33 = dataGridView.RowCount - 1;
  1968. for (int num34 = num32; num34 <= num33; num34++)
  1969. {
  1970. IRow row3 = hSSFSheet.CreateRow(num9 + num34);
  1971. row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num34].Height, isX: false)) * 72f;
  1972. if (!dataGridView.Rows[num34].Visible)
  1973. {
  1974. row3.ZeroHeight = true;
  1975. }
  1976. if (num34 + num9 == 1048575)
  1977. {
  1978. break;
  1979. }
  1980. }
  1981. int num35 = 0;
  1982. int num36 = 0;
  1983. if (isshowprogress)
  1984. {
  1985. progressexcel.ProgressBar1.Maximum = dataGridView.RowCount;
  1986. }
  1987. int num37 = 0;
  1988. int num38 = dataGridView.RowCount - 1;
  1989. for (int num39 = num37; num39 <= num38; num39++)
  1990. {
  1991. bool flag3 = false;
  1992. if (isshowprogress)
  1993. {
  1994. progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num39 + 1) + "〗条记录,共〖" + Conversions.ToString(dataGridView.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(num39 + 1) / (double)dataGridView.RowCount, "###.##%") + "〗";
  1995. progressexcel.ProgressBar1.Value = num39 + 1;
  1996. Application.DoEvents();
  1997. }
  1998. if (Module1.G_CancelExportDGVToExcel)
  1999. {
  2000. break;
  2001. }
  2002. int num40 = 0;
  2003. int num41 = num3 - 1;
  2004. for (num6 = num40; num6 <= num41; num6++)
  2005. {
  2006. int num42;
  2007. if (dataGridView.Columns[array3[num6]].ValueType == null)
  2008. {
  2009. num42 = 0;
  2010. }
  2011. else
  2012. {
  2013. switch (Strings.Left(dataGridView.Columns[array3[num6]].ValueType.Name.ToUpper(), 3))
  2014. {
  2015. case "STR":
  2016. num42 = 0;
  2017. break;
  2018. case "INT":
  2019. case "DEC":
  2020. num42 = 1;
  2021. flag3 = false;
  2022. break;
  2023. case "DAT":
  2024. num42 = 2;
  2025. break;
  2026. case "BOO":
  2027. num42 = 3;
  2028. flag3 = false;
  2029. break;
  2030. default:
  2031. num42 = 0;
  2032. break;
  2033. }
  2034. }
  2035. num31 = 1;
  2036. num26 = 1;
  2037. num29 = 0;
  2038. num27 = 0;
  2039. num30 = 0;
  2040. num29 = 0;
  2041. num27 = 0;
  2042. num28 = 0;
  2043. string text2 = Conversions.ToString(dataGridView.Rows[num39].Cells[array3[num6]].Tag);
  2044. switch (text2)
  2045. {
  2046. default:
  2047. {
  2048. float mywidth = 0f;
  2049. float num43 = 9f;
  2050. int firstcolumn = 0;
  2051. int firstrow = 0;
  2052. int firstc = 0;
  2053. int firstr = 0;
  2054. switch (text2)
  2055. {
  2056. default:
  2057. num31 = 1;
  2058. num26 = 1;
  2059. break;
  2060. case "合并":
  2061. case "左上合并":
  2062. case "左下合并":
  2063. case "右上合并":
  2064. case "右下合并":
  2065. case "上边合并":
  2066. case "下边合并":
  2067. case "左边合并":
  2068. case "右边合并":
  2069. mymerge.GetHVMergeWidthAndHeight(dataGridView, array3[num6], num39, ref mywidth, ref num43, ref num30, ref num29, ref num27, ref num28, ref firstcolumn, ref firstrow, ref firstc, ref firstr);
  2070. num31 = num29 + num30 + 1;
  2071. num26 = num27 + num28 + 1;
  2072. break;
  2073. case "水平合并":
  2074. case "#水平合并#":
  2075. case "左合并":
  2076. case "右合并":
  2077. mywidth = (int)Math.Round(mymerge.GetHMergeWidth(dataGridView, array3[num6], num39, ref num31, ref num29, ref num30, ref firstcolumn, ref firstc, iscancheckheight: true));
  2078. break;
  2079. }
  2080. break;
  2081. }
  2082. case "垂直合并":
  2083. case "#垂直合并#":
  2084. case "上合并":
  2085. case "下合并":
  2086. {
  2087. int firstrow = 0;
  2088. int firstr = 0;
  2089. float num43 = mymerge.GetVMergeHeight(dataGridView, array3[num6], num39, ref num26, ref num28, ref num27, ref firstrow, ref firstr);
  2090. break;
  2091. }
  2092. }
  2093. if (unchecked(num29 > 0 || num27 > 0))
  2094. {
  2095. continue;
  2096. }
  2097. if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewImageColumn", TextCompare: false) == 0)
  2098. {
  2099. if (dataGridView.Rows[num39].Cells[array3[num6]].Value != null)
  2100. {
  2101. Image img = (Image)dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue;
  2102. byte[] byteByImage = DGVtoExcel.GetByteByImage(img);
  2103. int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
  2104. IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
  2105. XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, num6, num9 + num39, num6 + 1, num9 + num39 + 1);
  2106. drawing.CreatePicture(anchor, pictureIndex);
  2107. }
  2108. }
  2109. else if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
  2110. {
  2111. if (dataGridView.Rows[num39].Cells[array3[num6]].Value != null && Conversions.ToBoolean(dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue))
  2112. {
  2113. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num39, num6, "√");
  2114. }
  2115. flag3 = false;
  2116. }
  2117. else
  2118. {
  2119. string text3 = Conversions.ToString(dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue) ?? "";
  2120. flag3 = Strings.InStr(text3, "\r\n") > 0;
  2121. switch (num42)
  2122. {
  2123. case 1:
  2124. if (Versioned.IsNumeric(text3))
  2125. {
  2126. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num39, num6, Conversions.ToDouble(text3));
  2127. }
  2128. else
  2129. {
  2130. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
  2131. }
  2132. break;
  2133. case 2:
  2134. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
  2135. break;
  2136. default:
  2137. DGVtoExcel.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
  2138. break;
  2139. }
  2140. }
  2141. DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(dataGridView.Rows[num39].Cells[array3[num6]]);
  2142. DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
  2143. StringAlignment valign = StringAlignment.Center;
  2144. StringAlignment halign = StringAlignment.Center;
  2145. switch (alignment)
  2146. {
  2147. case DataGridViewContentAlignment.BottomCenter:
  2148. valign = StringAlignment.Far;
  2149. halign = StringAlignment.Center;
  2150. break;
  2151. case DataGridViewContentAlignment.MiddleCenter:
  2152. valign = StringAlignment.Center;
  2153. halign = StringAlignment.Center;
  2154. break;
  2155. case DataGridViewContentAlignment.TopCenter:
  2156. valign = StringAlignment.Near;
  2157. halign = StringAlignment.Center;
  2158. break;
  2159. case DataGridViewContentAlignment.BottomLeft:
  2160. valign = StringAlignment.Far;
  2161. halign = StringAlignment.Near;
  2162. break;
  2163. case DataGridViewContentAlignment.MiddleLeft:
  2164. valign = StringAlignment.Center;
  2165. halign = StringAlignment.Near;
  2166. break;
  2167. case DataGridViewContentAlignment.TopLeft:
  2168. valign = StringAlignment.Near;
  2169. halign = StringAlignment.Near;
  2170. break;
  2171. case DataGridViewContentAlignment.BottomRight:
  2172. valign = StringAlignment.Far;
  2173. halign = StringAlignment.Far;
  2174. break;
  2175. case DataGridViewContentAlignment.MiddleRight:
  2176. valign = StringAlignment.Center;
  2177. halign = StringAlignment.Far;
  2178. break;
  2179. case DataGridViewContentAlignment.TopRight:
  2180. valign = StringAlignment.Near;
  2181. halign = StringAlignment.Far;
  2182. break;
  2183. }
  2184. if (string.Concat(dataGridView.Rows[num39].Cells[array3[num6]].Tag).Contains("合并"))
  2185. {
  2186. valign = StringAlignment.Center;
  2187. halign = StringAlignment.Center;
  2188. }
  2189. if (!flag3 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
  2190. {
  2191. flag3 = true;
  2192. }
  2193. Color dGVCellBackColor = Module1.GetDGVCellBackColor(dataGridView.Rows[num39].Cells[array3[num6]]);
  2194. Color dGVCellForeColor = Module1.GetDGVCellForeColor(dataGridView.Rows[num39].Cells[array3[num6]]);
  2195. DGVtoExcel.SetCellStyle(hSSFWorkbook, hSSFSheet, num9 + num39, num6, leftborder: true, rightborder: true, topborder: true, bottomborder: true, halign, valign, Module1.GetDGVCellFont(dataGridView.Rows[num39].Cells[array3[num6]]), dGVCellBackColor, dGVCellForeColor, flag3);
  2196. if (unchecked(num31 == 1 && num26 == 1))
  2197. {
  2198. continue;
  2199. }
  2200. int num44 = num9 + num39;
  2201. int num45 = num9 + num39 + num28;
  2202. for (int num46 = num44; num46 <= num45; num46++)
  2203. {
  2204. int num47 = num6;
  2205. int num48 = num6 + num30;
  2206. for (int num49 = num47; num49 <= num48; num49++)
  2207. {
  2208. unchecked
  2209. {
  2210. if (num46 != checked(num9 + num39) || num49 != num6)
  2211. {
  2212. DGVtoExcel.SetCellBorder(hSSFWorkbook, hSSFSheet, num46, num49, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
  2213. }
  2214. }
  2215. }
  2216. }
  2217. DGVtoExcel.SetMergeCell(hSSFSheet, num9 + num39, num6, num9 + num39 + num28, num6 + num30);
  2218. num6 += num30;
  2219. }
  2220. if (!((num39 + num9 == 1048575) & (num39 != dataGridView.RowCount - 1)))
  2221. {
  2222. continue;
  2223. }
  2224. int num50 = 0;
  2225. int num51 = num3 - 1;
  2226. for (int num52 = num50; num52 <= num51; num52++)
  2227. {
  2228. hSSFSheet.SetColumnWidth(num52, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num52]].Width / 8.0) * 256.0));
  2229. }
  2230. num35++;
  2231. text = DGVtoExcel.GetExcelNewSheetName(hSSFWorkbook, empty);
  2232. hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(text);
  2233. num36 = 1;
  2234. int num53 = num39 + 1;
  2235. int num54 = dataGridView.RowCount - 1;
  2236. for (num6 = num53; num6 <= num54; num6++)
  2237. {
  2238. IRow row4 = hSSFSheet.CreateRow(num36 - 1);
  2239. if (!dataGridView.Rows[num6].Visible)
  2240. {
  2241. row4.ZeroHeight = true;
  2242. }
  2243. row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num6].Height, isX: false)) * 72f;
  2244. if (num36 == 1048576)
  2245. {
  2246. break;
  2247. }
  2248. num36++;
  2249. }
  2250. num9 = -num39 - 1;
  2251. }
  2252. num9 = ((num36 != 0) ? (num36 - 1) : (num9 + dataGridView.RowCount));
  2253. if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
  2254. {
  2255. DGVtoExcel.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, TableFooter, tablefooterfont);
  2256. }
  2257. DGVtoExcel.w_font = null;
  2258. DGVtoExcel.w_ifont = null;
  2259. DGVtoExcel.w_Cellstyle = null;
  2260. DGVtoExcel.w_iCellstyle = null;
  2261. DGVtoExcel.w_color = null;
  2262. int num55 = 0;
  2263. int num56 = num3 - 1;
  2264. for (int num57 = num55; num57 <= num56; num57++)
  2265. {
  2266. hSSFSheet.SetColumnWidth(num57, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num57]].Width / 8.0) * 256.0));
  2267. }
  2268. hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(text));
  2269. }
  2270. num2 += mydgvs[i].Rows.Count + ((num == 0) ? 1 : num) + 4;
  2271. }
  2272. ruler.Dispose();
  2273. ruler = null;
  2274. if (isshowprogress)
  2275. {
  2276. progressexcel.Close();
  2277. progressexcel.Dispose();
  2278. }
  2279. if (!DGVtoExcel.WriteToFile(hSSFWorkbook, FileName))
  2280. {
  2281. hSSFWorkbook = null;
  2282. result = false;
  2283. }
  2284. else
  2285. {
  2286. hSSFWorkbook = null;
  2287. if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
  2288. {
  2289. try
  2290. {
  2291. Process.Start(FileName);
  2292. }
  2293. catch (Exception projectError3)
  2294. {
  2295. ProjectData.SetProjectError(projectError3);
  2296. try
  2297. {
  2298. Thread.Sleep(100);
  2299. Process.Start(FileName);
  2300. }
  2301. catch (Exception projectError4)
  2302. {
  2303. ProjectData.SetProjectError(projectError4);
  2304. ProjectData.ClearProjectError();
  2305. }
  2306. ProjectData.ClearProjectError();
  2307. }
  2308. }
  2309. result = true;
  2310. }
  2311. }
  2312. catch (Exception projectError5)
  2313. {
  2314. ProjectData.SetProjectError(projectError5);
  2315. if (isshowprogress)
  2316. {
  2317. progressexcel.Close();
  2318. progressexcel.Dispose();
  2319. }
  2320. try
  2321. {
  2322. ruler.Dispose();
  2323. ruler = null;
  2324. }
  2325. catch (Exception projectError6)
  2326. {
  2327. ProjectData.SetProjectError(projectError6);
  2328. ProjectData.ClearProjectError();
  2329. }
  2330. }
  2331. }
  2332. return result;
  2333. }
  2334. }
  2335. public void ExportDGVToPDF(DataGridView dgv, TreeView tv, string title, Font titlefont, Font tableheaderfont, string tableheader, Font tablefooterfont, string tablefooter, string filename, bool isshowprogress)
  2336. {
  2337. //IL_012f: Unknown result type (might be due to invalid IL or missing references)
  2338. //IL_0139: Expected O, but got Unknown
  2339. //IL_021a: Unknown result type (might be due to invalid IL or missing references)
  2340. //IL_0221: Expected O, but got Unknown
  2341. //IL_03c9: Unknown result type (might be due to invalid IL or missing references)
  2342. //IL_03d0: Expected O, but got Unknown
  2343. //IL_0300: Unknown result type (might be due to invalid IL or missing references)
  2344. //IL_0307: Expected O, but got Unknown
  2345. //IL_04ed: Unknown result type (might be due to invalid IL or missing references)
  2346. //IL_04f4: Expected O, but got Unknown
  2347. //IL_05a8: Unknown result type (might be due to invalid IL or missing references)
  2348. //IL_05af: Expected O, but got Unknown
  2349. //IL_0550: Unknown result type (might be due to invalid IL or missing references)
  2350. //IL_0557: Expected O, but got Unknown
  2351. //IL_0702: Unknown result type (might be due to invalid IL or missing references)
  2352. //IL_0709: Expected O, but got Unknown
  2353. _paperwidth = 210f;
  2354. _paperheight = 297f;
  2355. _paperlandscape = false;
  2356. _margin = new Margins(10, 10, 10, 10);
  2357. _repeatheader = true;
  2358. if (dgv == null || dgv.ColumnCount <= 0)
  2359. {
  2360. return;
  2361. }
  2362. progressexcel progressexcel = null;
  2363. SaveFileDialog saveFileDialog = new SaveFileDialog();
  2364. saveFileDialog.Title = "请选择要导出PDF文件的路径和文件名";
  2365. saveFileDialog.AddExtension = true;
  2366. saveFileDialog.OverwritePrompt = false;
  2367. saveFileDialog.DefaultExt = "PDF";
  2368. saveFileDialog.Filter = "PDF文件(*.pdf)|*.pdf";
  2369. saveFileDialog.FileName = filename;
  2370. if (isshowprogress)
  2371. {
  2372. progressexcel = new progressexcel();
  2373. progressexcel.Text = "导出PDF文件";
  2374. progressexcel.TopMost = true;
  2375. progressexcel.Visible = true;
  2376. Module1.G_CancelExportDGVToExcel = false;
  2377. progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
  2378. Application.DoEvents();
  2379. }
  2380. if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
  2381. {
  2382. if (isshowprogress)
  2383. {
  2384. progressexcel.Close();
  2385. progressexcel.Dispose();
  2386. }
  2387. saveFileDialog.Dispose();
  2388. return;
  2389. }
  2390. filename = saveFileDialog.FileName;
  2391. saveFileDialog.Dispose();
  2392. if (mydoc != null)
  2393. {
  2394. mydoc.Dispose();
  2395. mydoc = null;
  2396. }
  2397. if (mywriter != null)
  2398. {
  2399. ((DocWriter)mywriter).Dispose();
  2400. mywriter = null;
  2401. }
  2402. FontFactory.RegisterDirectories();
  2403. mydoc = new Document();
  2404. float totalwidth = 0f;
  2405. int[] dGVColWidth = GetDGVColWidth(dgv, ref totalwidth);
  2406. checked
  2407. {
  2408. float[] array = new float[dGVColWidth.Length - 1 + 1];
  2409. int num = 0;
  2410. int num2 = dGVColWidth.Length - 1;
  2411. for (int i = num; i <= num2; i++)
  2412. {
  2413. array[i] = Module1.G_ruler.ConvertToPoint(dGVColWidth[i], isx: true);
  2414. }
  2415. mywriter = PdfWriter.GetInstance(mydoc, (Stream)new FileStream(filename, FileMode.Create));
  2416. mywriter.PageEvent = (IPdfPageEvent)(object)new testevent();
  2417. mydoc.Open();
  2418. if (PaperWidth == 0f)
  2419. {
  2420. totalwidth = Module1.G_ruler.ConvertToMM(totalwidth, isX: true) + (float)PaperMargin.Left + (float)PaperMargin.Right;
  2421. totalwidth = Module1.G_ruler.ConvertToPointFromMM(totalwidth);
  2422. Rectangle pageSize = new Rectangle(0f, 0f, totalwidth, Module1.G_ruler.ConvertToPointFromMM(PaperHeight));
  2423. mydoc.SetPageSize(pageSize);
  2424. mydoc.SetMargins(Module1.G_ruler.ConvertToPointFromMM(_margin.Left), Module1.G_ruler.ConvertToPointFromMM(_margin.Right), Module1.G_ruler.ConvertToPointFromMM(_margin.Top), Module1.G_ruler.ConvertToPointFromMM(_margin.Bottom));
  2425. myratio = 1f;
  2426. }
  2427. else
  2428. {
  2429. totalwidth = Module1.G_ruler.ConvertToMM(totalwidth, isX: true);
  2430. totalwidth = Module1.G_ruler.ConvertToPointFromMM(totalwidth);
  2431. float num3 = Module1.G_ruler.ConvertToPointFromMM(PaperWidth);
  2432. float num4 = Module1.G_ruler.ConvertToPointFromMM(PaperHeight);
  2433. if (unchecked(PaperLandScape || num3 > num4))
  2434. {
  2435. Rectangle val = new Rectangle(0f, 0f, num3, num4);
  2436. mydoc.SetMargins(Module1.G_ruler.ConvertToPointFromMM(_margin.Left), Module1.G_ruler.ConvertToPointFromMM(_margin.Right), Module1.G_ruler.ConvertToPointFromMM(_margin.Top), Module1.G_ruler.ConvertToPointFromMM(_margin.Bottom));
  2437. mydoc.SetPageSize(val.Rotate());
  2438. myratio = (num4 - Module1.G_ruler.ConvertToPointFromMM(_margin.Left) - Module1.G_ruler.ConvertToPointFromMM(_margin.Right)) / totalwidth;
  2439. }
  2440. else
  2441. {
  2442. Rectangle pageSize2 = new Rectangle(0f, 0f, num3, num4);
  2443. mydoc.SetMargins(Module1.G_ruler.ConvertToPointFromMM(_margin.Left), Module1.G_ruler.ConvertToPointFromMM(_margin.Right), Module1.G_ruler.ConvertToPointFromMM(_margin.Top), Module1.G_ruler.ConvertToPointFromMM(_margin.Bottom));
  2444. mydoc.SetPageSize(pageSize2);
  2445. myratio = (num3 - Module1.G_ruler.ConvertToPointFromMM(_margin.Left) - Module1.G_ruler.ConvertToPointFromMM(_margin.Right)) / totalwidth;
  2446. }
  2447. }
  2448. int num5 = 0;
  2449. int num6 = dGVColWidth.Length - 1;
  2450. for (int j = num5; j <= num6; j++)
  2451. {
  2452. array[j] *= myratio;
  2453. }
  2454. mydoc.NewPage();
  2455. if (titlefont == null)
  2456. {
  2457. titlefont = new Font("黑体", 18f, FontStyle.Bold);
  2458. }
  2459. if (title != null && Operators.CompareString(title, "", TextCompare: false) != 0)
  2460. {
  2461. Paragraph val2 = new Paragraph(title, GetPDFFont(titlefont));
  2462. val2.Alignment = 1;
  2463. val2.SpacingAfter = 5f;
  2464. val2.SpacingBefore = 0f;
  2465. mydoc.Add((IElement)(object)val2);
  2466. }
  2467. if (Operators.CompareString(tableheader, "", TextCompare: false) != 0)
  2468. {
  2469. if (tableheaderfont == null)
  2470. {
  2471. tableheaderfont = new Font("宋体", 12f);
  2472. }
  2473. Paragraph val3 = new Paragraph(tableheader, GetPDFFont(tableheaderfont));
  2474. val3.SpacingAfter = 5f;
  2475. mydoc.Add((IElement)(object)val3);
  2476. }
  2477. Module1.G_CancelExportDGVToExcel = false;
  2478. if (isshowprogress)
  2479. {
  2480. progressexcel.ProgressBar1.Maximum = dgv.RowCount;
  2481. progressexcel.Label1.Text = "正导出表头,请稍侯……";
  2482. Application.DoEvents();
  2483. }
  2484. PdfPTable val4 = new PdfPTable(GetDGVVisibleCols(dgv));
  2485. val4.SetWidths(array);
  2486. if (dgv.ColumnHeadersVisible)
  2487. {
  2488. AddHead(dgv, tv, val4);
  2489. }
  2490. int num7 = 0;
  2491. int num8 = dgv.RowCount - 1;
  2492. for (int k = num7; k <= num8; k++)
  2493. {
  2494. if (isshowprogress)
  2495. {
  2496. progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(k + 1) + "〗条记录,共〖" + Conversions.ToString(dgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(k + 1) / (double)dgv.RowCount, "###.##%") + "〗";
  2497. progressexcel.ProgressBar1.Value = k + 1;
  2498. Application.DoEvents();
  2499. if (Module1.G_CancelExportDGVToExcel)
  2500. {
  2501. break;
  2502. }
  2503. }
  2504. if (dgv.Rows[k].Visible)
  2505. {
  2506. AddRow(dgv, k, val4);
  2507. }
  2508. }
  2509. if (isshowprogress)
  2510. {
  2511. progressexcel.Close();
  2512. progressexcel.Dispose();
  2513. }
  2514. val4.WidthPercentage = 100f;
  2515. mydoc.Add((IElement)(object)val4);
  2516. if (Operators.CompareString(tablefooter, "", TextCompare: false) != 0)
  2517. {
  2518. if (tablefooterfont == null)
  2519. {
  2520. tablefooterfont = new Font("宋体", 12f);
  2521. }
  2522. Paragraph val5 = new Paragraph(tablefooter, GetPDFFont(tablefooterfont));
  2523. val5.SpacingAfter = 5f;
  2524. mydoc.Add((IElement)(object)val5);
  2525. }
  2526. mydoc.Close();
  2527. ((DocWriter)mywriter).Dispose();
  2528. mywriter = null;
  2529. mydoc.Dispose();
  2530. mydoc = null;
  2531. }
  2532. }
  2533. private int[] GetDGVColWidth(DataGridView dgv, ref float totalwidth)
  2534. {
  2535. totalwidth = 0f;
  2536. if (dgv == null)
  2537. {
  2538. return null;
  2539. }
  2540. int columnCount = dgv.ColumnCount;
  2541. checked
  2542. {
  2543. int[] array = new int[columnCount - 1 + 1];
  2544. int[] array2 = new int[columnCount - 1 + 1];
  2545. int num = 0;
  2546. int num2 = columnCount - 1;
  2547. for (int i = num; i <= num2; i++)
  2548. {
  2549. array[i] = i;
  2550. array2[i] = dgv.Columns[i].DisplayIndex;
  2551. }
  2552. Array.Sort(array2, array);
  2553. int dGVVisibleCols = GetDGVVisibleCols(dgv);
  2554. if (dGVVisibleCols == 0)
  2555. {
  2556. return null;
  2557. }
  2558. int num3 = 0;
  2559. int[] array3 = new int[dGVVisibleCols - 1 + 1];
  2560. int num4 = 0;
  2561. int num5 = columnCount - 1;
  2562. for (int j = num4; j <= num5; j++)
  2563. {
  2564. if (dgv.Columns[array[j]].Visible)
  2565. {
  2566. array3[num3] = dgv.Columns[array[j]].Width;
  2567. totalwidth += array3[num3];
  2568. num3++;
  2569. }
  2570. }
  2571. return array3;
  2572. }
  2573. }
  2574. private int GetDGVVisibleCols(DataGridView dgv)
  2575. {
  2576. if (dgv == null)
  2577. {
  2578. return 0;
  2579. }
  2580. int num = 0;
  2581. int num2 = 0;
  2582. checked
  2583. {
  2584. int num3 = dgv.ColumnCount - 1;
  2585. for (int i = num2; i <= num3; i++)
  2586. {
  2587. if (dgv.Columns[i].Visible)
  2588. {
  2589. num++;
  2590. }
  2591. }
  2592. return num;
  2593. }
  2594. }
  2595. private Font GetPDFFont(Font f)
  2596. {
  2597. if (f == null)
  2598. {
  2599. return null;
  2600. }
  2601. float size = f.Size;
  2602. int num = 0;
  2603. if (f.Bold)
  2604. {
  2605. num |= 1;
  2606. }
  2607. if (f.Italic)
  2608. {
  2609. num |= 2;
  2610. }
  2611. if (f.Underline)
  2612. {
  2613. num |= 4;
  2614. }
  2615. if (f.Strikeout)
  2616. {
  2617. num |= 8;
  2618. }
  2619. return GetPDFFont(f.Name, size, num);
  2620. }
  2621. private Font GetPDFFont(Font f, Color cc)
  2622. {
  2623. if (f == null)
  2624. {
  2625. return null;
  2626. }
  2627. float size = f.Size;
  2628. int num = 0;
  2629. if (f.Bold)
  2630. {
  2631. num |= 1;
  2632. }
  2633. if (f.Italic)
  2634. {
  2635. num |= 2;
  2636. }
  2637. if (f.Underline)
  2638. {
  2639. num |= 4;
  2640. }
  2641. if (f.Strikeout)
  2642. {
  2643. num |= 8;
  2644. }
  2645. return GetPDFFont(f.Name, size, num, GetPDFColor(cc));
  2646. }
  2647. private Font GetPDFFont(string fontname, float size, int style)
  2648. {
  2649. return FontFactory.GetFont(fontname, "Identity-H", false, size, style);
  2650. }
  2651. private Font GetPDFFont(string fontname, float size, int style, BaseColor cc)
  2652. {
  2653. return FontFactory.GetFont(fontname, "Identity-H", false, size, style, cc);
  2654. }
  2655. private void AddHead(DataGridView dgv, PdfPTable tb)
  2656. {
  2657. //IL_010e: Unknown result type (might be due to invalid IL or missing references)
  2658. //IL_0115: Expected O, but got Unknown
  2659. //IL_0117: Unknown result type (might be due to invalid IL or missing references)
  2660. //IL_011e: Expected O, but got Unknown
  2661. if (dgv == null || dgv.ColumnCount <= 0)
  2662. {
  2663. return;
  2664. }
  2665. int columnCount = dgv.ColumnCount;
  2666. checked
  2667. {
  2668. int[] array = new int[columnCount - 1 + 1];
  2669. int[] array2 = new int[columnCount - 1 + 1];
  2670. int num = 0;
  2671. int num2 = columnCount - 1;
  2672. for (int i = num; i <= num2; i++)
  2673. {
  2674. array[i] = i;
  2675. array2[i] = dgv.Columns[i].DisplayIndex;
  2676. }
  2677. Array.Sort(array2, array);
  2678. int num3 = 0;
  2679. int num4 = dgv.ColumnCount - 1;
  2680. for (int j = num3; j <= num4; j++)
  2681. {
  2682. if (dgv.Columns[array[j]].Visible)
  2683. {
  2684. _ = dgv.Columns[array[j]].Width;
  2685. Font dGVHeaderFont = Module1.GetDGVHeaderFont(dgv.Columns[array[j]]);
  2686. Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dgv.Columns[array[j]]);
  2687. Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dgv.Columns[array[j]]);
  2688. Phrase val = new Phrase(dgv.Columns[array[j]].HeaderText, GetPDFFont(dGVHeaderFont, dGVHeaderForeColor));
  2689. PdfPCell val2 = new PdfPCell(val);
  2690. val2.Padding = 1f;
  2691. ((Rectangle)val2).BorderColor = GetPDFColor(dgv.GridColor);
  2692. val2.HorizontalAlignment = 1;
  2693. val2.VerticalAlignment = 5;
  2694. ((Rectangle)val2).BackgroundColor = GetPDFColor(dGVHeaderBackColor);
  2695. if (myratio >= 1f)
  2696. {
  2697. val2.FixedHeight = Module1.G_ruler.ConvertToPoint(dgv.ColumnHeadersHeight, isx: false);
  2698. }
  2699. else
  2700. {
  2701. val2.MinimumHeight = Module1.G_ruler.ConvertToPoint(dgv.ColumnHeadersHeight, isx: false);
  2702. }
  2703. tb.AddCell(val2);
  2704. }
  2705. }
  2706. }
  2707. }
  2708. private void AddHead(DataGridView dgv, TreeView tv, PdfPTable tb)
  2709. {
  2710. if (dgv == null)
  2711. {
  2712. return;
  2713. }
  2714. if (tv == null)
  2715. {
  2716. if (RepeatHeader)
  2717. {
  2718. tb.HeaderRows = 1;
  2719. }
  2720. AddHead(dgv, tb);
  2721. return;
  2722. }
  2723. checked
  2724. {
  2725. float[] array = new float[dgv.ColumnCount - 1 + 1];
  2726. int num = 0;
  2727. int num2 = dgv.ColumnCount - 1;
  2728. for (int i = num; i <= num2; i++)
  2729. {
  2730. array[i] = dgv.Columns[i].Width;
  2731. }
  2732. mytree mytree = new mytree(tv, dgv, array);
  2733. int leves = mytree.GetLeves();
  2734. if (mytree.GetColumns() != dgv.ColumnCount)
  2735. {
  2736. if (RepeatHeader)
  2737. {
  2738. tb.HeaderRows = 1;
  2739. }
  2740. AddHead(dgv, tb);
  2741. }
  2742. else
  2743. {
  2744. if (RepeatHeader)
  2745. {
  2746. tb.HeaderRows = leves;
  2747. }
  2748. int num3 = 1;
  2749. int num4 = leves;
  2750. for (int j = num3; j <= num4; j++)
  2751. {
  2752. AddHead(dgv, mytree, j, tb);
  2753. }
  2754. }
  2755. mytree.Dispose();
  2756. }
  2757. }
  2758. private void AddHead(DataGridView dgv, mytree myheader, int curlevel, PdfPTable tb)
  2759. {
  2760. //IL_01b8: Unknown result type (might be due to invalid IL or missing references)
  2761. //IL_01bf: Expected O, but got Unknown
  2762. //IL_01c1: Unknown result type (might be due to invalid IL or missing references)
  2763. //IL_01c8: Expected O, but got Unknown
  2764. if (dgv == null || dgv.ColumnCount <= 0)
  2765. {
  2766. return;
  2767. }
  2768. int columnCount = dgv.ColumnCount;
  2769. checked
  2770. {
  2771. int[] array = new int[columnCount - 1 + 1];
  2772. int[] array2 = new int[columnCount - 1 + 1];
  2773. int num = 0;
  2774. int num2 = columnCount - 1;
  2775. for (int i = num; i <= num2; i++)
  2776. {
  2777. array[i] = i;
  2778. array2[i] = dgv.Columns[i].DisplayIndex;
  2779. }
  2780. Array.Sort(array2, array);
  2781. int num3 = (int)Math.Round((double)dgv.ColumnHeadersHeight / (double)myheader.GetLeves());
  2782. int num4 = 0;
  2783. int num5 = dgv.ColumnCount - 1;
  2784. bool flag = true;
  2785. for (int j = num4; j <= num5; j++)
  2786. {
  2787. if (!dgv.Columns[array[j]].Visible)
  2788. {
  2789. continue;
  2790. }
  2791. int num6 = myheader.GetCellHeightLevels(array[j], curlevel, isTopNodeFill: false, out flag);
  2792. int cellWidthCols = myheader.GetCellWidthCols(array[j], curlevel);
  2793. if (unchecked(num6 == 0 || cellWidthCols == 0))
  2794. {
  2795. if (num6 != 0)
  2796. {
  2797. }
  2798. continue;
  2799. }
  2800. string nodeText = myheader.GetNodeText(array[j], curlevel);
  2801. Font font = myheader.GetNodeFont(array[j], curlevel);
  2802. if (font == null)
  2803. {
  2804. font = Module1.GetDGVHeaderFont(dgv.Columns[array[j]]);
  2805. }
  2806. Color cc = myheader.GetNodeForeColor(array[j], curlevel);
  2807. if (cc.IsEmpty)
  2808. {
  2809. cc = Module1.GetDGVHeaderForeColor(dgv.Columns[array[j]]);
  2810. }
  2811. Color cc2 = myheader.GetNodeBackColor(array[j], curlevel);
  2812. if (cc2.IsEmpty)
  2813. {
  2814. cc2 = Module1.GetDGVHeaderBackColor(dgv.Columns[array[j]]);
  2815. }
  2816. if (unchecked(cellWidthCols == 1 && num6 == 1))
  2817. {
  2818. _ = dgv.Columns[array[j]].Width;
  2819. }
  2820. else
  2821. {
  2822. _ = (int)Math.Round(myheader.GetCellWidth2(array[j], curlevel));
  2823. }
  2824. Phrase val = new Phrase(nodeText, GetPDFFont(font, cc));
  2825. PdfPCell val2 = new PdfPCell(val);
  2826. if (cellWidthCols != 0)
  2827. {
  2828. val2.Colspan = cellWidthCols;
  2829. }
  2830. if (num6 != 0)
  2831. {
  2832. val2.Rowspan = num6;
  2833. }
  2834. val2.Padding = 1f;
  2835. ((Rectangle)val2).BorderColor = GetPDFColor(dgv.GridColor);
  2836. val2.HorizontalAlignment = 1;
  2837. val2.VerticalAlignment = 5;
  2838. ((Rectangle)val2).BackgroundColor = GetPDFColor(cc2);
  2839. if (num6 <= 0)
  2840. {
  2841. num6 = 1;
  2842. }
  2843. if (myratio >= 1f)
  2844. {
  2845. val2.FixedHeight = Module1.G_ruler.ConvertToPoint(num3 * num6, isx: false);
  2846. }
  2847. else
  2848. {
  2849. val2.MinimumHeight = Module1.G_ruler.ConvertToPoint(num3 * num6, isx: false);
  2850. }
  2851. tb.AddCell(val2);
  2852. }
  2853. }
  2854. }
  2855. private void AddRow(DataGridView dgv, int rowindex, PdfPTable tb)
  2856. {
  2857. //IL_0750: Unknown result type (might be due to invalid IL or missing references)
  2858. //IL_0757: Expected O, but got Unknown
  2859. //IL_0759: Unknown result type (might be due to invalid IL or missing references)
  2860. //IL_0760: Expected O, but got Unknown
  2861. //IL_065a: Unknown result type (might be due to invalid IL or missing references)
  2862. //IL_0661: Expected O, but got Unknown
  2863. //IL_0602: Unknown result type (might be due to invalid IL or missing references)
  2864. //IL_0609: Expected O, but got Unknown
  2865. //IL_05f6: Unknown result type (might be due to invalid IL or missing references)
  2866. //IL_05fd: Expected O, but got Unknown
  2867. //IL_0536: Unknown result type (might be due to invalid IL or missing references)
  2868. //IL_053d: Expected O, but got Unknown
  2869. if (dgv == null || dgv.RowCount <= 0 || rowindex >= dgv.RowCount || dgv.ColumnCount <= 0 || !dgv.Rows[rowindex].Visible)
  2870. {
  2871. return;
  2872. }
  2873. int columnCount = dgv.ColumnCount;
  2874. checked
  2875. {
  2876. int[] array = new int[columnCount - 1 + 1];
  2877. int[] array2 = new int[columnCount - 1 + 1];
  2878. int num = 0;
  2879. int num2 = columnCount - 1;
  2880. for (int i = num; i <= num2; i++)
  2881. {
  2882. array[i] = i;
  2883. array2[i] = dgv.Columns[i].DisplayIndex;
  2884. }
  2885. Array.Sort(array2, array);
  2886. int height = dgv.Rows[rowindex].Height;
  2887. int num3 = 1;
  2888. int num4 = 0;
  2889. int num5 = 0;
  2890. int num6 = 0;
  2891. int num7 = 0;
  2892. int num8 = 1;
  2893. int num9 = 0;
  2894. int num10 = dgv.ColumnCount - 1;
  2895. for (int j = num9; j <= num10; j++)
  2896. {
  2897. if (!dgv.Columns[array[j]].Visible)
  2898. {
  2899. continue;
  2900. }
  2901. num8 = 1;
  2902. num3 = 1;
  2903. num6 = 0;
  2904. num4 = 0;
  2905. num7 = 0;
  2906. num6 = 0;
  2907. num4 = 0;
  2908. num5 = 0;
  2909. float myheight = height;
  2910. string text = Conversions.ToString(dgv.Rows[rowindex].Cells[array[j]].Tag);
  2911. int num11 = 0;
  2912. int firstrow = 0;
  2913. int firstr = 0;
  2914. switch (text)
  2915. {
  2916. default:
  2917. {
  2918. int firstcolumn = 0;
  2919. int firstc = 0;
  2920. switch (text)
  2921. {
  2922. default:
  2923. num8 = 1;
  2924. num3 = 1;
  2925. num11 = dgv.Columns[array[j]].Width;
  2926. break;
  2927. case "合并":
  2928. case "左上合并":
  2929. case "左下合并":
  2930. case "右上合并":
  2931. case "右下合并":
  2932. case "上边合并":
  2933. case "下边合并":
  2934. case "左边合并":
  2935. case "右边合并":
  2936. {
  2937. float mywidth = 0f;
  2938. mymerge.GetHVMergeWidthAndHeight(dgv, array[j], rowindex, ref mywidth, ref myheight, ref num7, ref num6, ref num4, ref num5, ref firstcolumn, ref firstrow, ref firstc, ref firstr);
  2939. num8 = num6 + num7 + 1;
  2940. num3 = num4 + num5 + 1;
  2941. num11 = (int)Math.Round(mywidth);
  2942. unchecked
  2943. {
  2944. if (num3 > 1 && num8 > 1)
  2945. {
  2946. if (!(num4 == 0 && num6 == 0))
  2947. {
  2948. if (num6 != 0)
  2949. {
  2950. }
  2951. continue;
  2952. }
  2953. }
  2954. else if (num3 > 1)
  2955. {
  2956. if (num4 != 0)
  2957. {
  2958. continue;
  2959. }
  2960. }
  2961. else if (num8 > 1 && num6 != 0)
  2962. {
  2963. continue;
  2964. }
  2965. break;
  2966. }
  2967. }
  2968. case "水平合并":
  2969. case "#水平合并#":
  2970. case "左合并":
  2971. case "右合并":
  2972. num11 = (int)Math.Round(mymerge.GetHMergeWidth(dgv, array[j], rowindex, ref num8, ref num6, ref num7, ref firstcolumn, ref firstc, iscancheckheight: true));
  2973. if (num8 > 1 && num6 != 0)
  2974. {
  2975. continue;
  2976. }
  2977. break;
  2978. }
  2979. break;
  2980. }
  2981. case "垂直合并":
  2982. case "#垂直合并#":
  2983. case "上合并":
  2984. case "下合并":
  2985. myheight = mymerge.GetVMergeHeight(dgv, array[j], rowindex, ref num3, ref num5, ref num4, ref firstrow, ref firstr);
  2986. num11 = dgv.Columns[array[j]].Width;
  2987. if (num3 > 1 && num4 != 0)
  2988. {
  2989. continue;
  2990. }
  2991. break;
  2992. }
  2993. Font dGVCellFont = Module1.GetDGVCellFont(dgv.Rows[rowindex].Cells[array[j]]);
  2994. Color dGVCellBackColor = Module1.GetDGVCellBackColor(dgv.Rows[rowindex].Cells[array[j]]);
  2995. Color dGVCellForeColor = Module1.GetDGVCellForeColor(dgv.Rows[rowindex].Cells[array[j]]);
  2996. Margins dGVCellPadding = Module1.GetDGVCellPadding(dgv.Rows[rowindex].Cells[j]);
  2997. PdfPCell val;
  2998. if (dgv.Columns[array[j]] is DataGridViewImageColumn)
  2999. {
  3000. if (dgv.Rows[rowindex].Cells[array[j]].Value != null)
  3001. {
  3002. if (dgv.Rows[rowindex].Cells[array[j]].FormattedValue != null)
  3003. {
  3004. Image instance = Image.GetInstance((Image)dgv.Rows[rowindex].Cells[array[j]].FormattedValue, BaseColor.WHITE);
  3005. DataGridViewImageCell dataGridViewImageCell = (DataGridViewImageCell)dgv.Rows[rowindex].Cells[array[j]];
  3006. DataGridViewImageCellLayout imageLayout = dataGridViewImageCell.ImageLayout;
  3007. if (imageLayout == DataGridViewImageCellLayout.Stretch)
  3008. {
  3009. if (myratio >= 1f)
  3010. {
  3011. instance.ScaleAbsolute(Module1.G_ruler.ConvertToPoint(num11 - 1, isx: true) * myratio, Module1.G_ruler.ConvertToPoint(myheight - 1f, isx: false));
  3012. }
  3013. else
  3014. {
  3015. instance.ScaleAbsoluteWidth(Module1.G_ruler.ConvertToPoint(num11, isx: true) * myratio);
  3016. }
  3017. }
  3018. else
  3019. {
  3020. instance.ScaleToFit(Module1.G_ruler.ConvertToPoint(num11, isx: true) * myratio, Module1.G_ruler.ConvertToPoint(myheight, isx: false));
  3021. }
  3022. val = new PdfPCell(instance);
  3023. val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
  3024. val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
  3025. val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
  3026. val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
  3027. if ((val.PaddingRight == 0f) & (val.PaddingLeft == 0f) & (val.PaddingBottom == 0f) & (val.PaddingTop == 0f))
  3028. {
  3029. val.Padding = 0.5f;
  3030. }
  3031. }
  3032. else
  3033. {
  3034. val = new PdfPCell();
  3035. }
  3036. }
  3037. else
  3038. {
  3039. val = new PdfPCell();
  3040. }
  3041. }
  3042. else if (dgv.Columns[array[j]] is DataGridViewCheckBoxColumn)
  3043. {
  3044. Bitmap dGVCheckBoxCellImage = Module1.getDGVCheckBoxCellImage(dgv.Rows[rowindex].Cells[array[j]], dGVCellBackColor);
  3045. Image instance2 = Image.GetInstance((Image)dGVCheckBoxCellImage, BaseColor.WHITE);
  3046. val = new PdfPCell(instance2);
  3047. val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
  3048. val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
  3049. val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
  3050. val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
  3051. if ((val.PaddingRight == 0f) & (val.PaddingLeft == 0f) & (val.PaddingBottom == 0f) & (val.PaddingTop == 0f))
  3052. {
  3053. val.Padding = 0.5f;
  3054. }
  3055. dGVCheckBoxCellImage.Dispose();
  3056. }
  3057. else
  3058. {
  3059. string text2 = Conversions.ToString(dgv.Rows[rowindex].Cells[array[j]].FormattedValue);
  3060. Phrase val2 = new Phrase(text2, GetPDFFont(dGVCellFont, dGVCellForeColor));
  3061. val = new PdfPCell(val2);
  3062. val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
  3063. val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
  3064. val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
  3065. val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
  3066. }
  3067. ((Rectangle)val).BorderColor = GetPDFColor(dgv.GridColor);
  3068. if (myratio >= 1f)
  3069. {
  3070. val.FixedHeight = Module1.G_ruler.ConvertToPoint(myheight, isx: false);
  3071. }
  3072. else
  3073. {
  3074. val.MinimumHeight = Module1.G_ruler.ConvertToPoint(myheight, isx: false);
  3075. }
  3076. ((Rectangle)val).BackgroundColor = GetPDFColor(dGVCellBackColor);
  3077. val.Rowspan = num3;
  3078. val.Colspan = num8;
  3079. DataGridViewContentAlignment alignment = Module1.GetDGVCellStyle(dgv.Rows[rowindex].Cells[array[j]]).Alignment;
  3080. if (alignment != DataGridViewContentAlignment.BottomCenter && alignment != DataGridViewContentAlignment.BottomLeft && alignment != DataGridViewContentAlignment.BottomRight)
  3081. {
  3082. if (alignment != DataGridViewContentAlignment.MiddleCenter && alignment != DataGridViewContentAlignment.MiddleLeft && alignment != DataGridViewContentAlignment.MiddleRight && alignment != 0)
  3083. {
  3084. val.VerticalAlignment = 4;
  3085. }
  3086. else
  3087. {
  3088. val.VerticalAlignment = 5;
  3089. }
  3090. }
  3091. else
  3092. {
  3093. val.VerticalAlignment = 6;
  3094. }
  3095. DataGridViewContentAlignment alignment2 = Module1.GetDGVCellStyle(dgv.Rows[rowindex].Cells[array[j]]).Alignment;
  3096. if (alignment2 != DataGridViewContentAlignment.BottomLeft && alignment2 != DataGridViewContentAlignment.MiddleLeft && alignment2 != DataGridViewContentAlignment.TopLeft && alignment2 != 0)
  3097. {
  3098. if (alignment2 != DataGridViewContentAlignment.BottomCenter && alignment2 != DataGridViewContentAlignment.MiddleCenter && alignment2 != DataGridViewContentAlignment.TopCenter)
  3099. {
  3100. val.HorizontalAlignment = 2;
  3101. }
  3102. else
  3103. {
  3104. val.HorizontalAlignment = 1;
  3105. }
  3106. }
  3107. else
  3108. {
  3109. val.HorizontalAlignment = 0;
  3110. }
  3111. tb.AddCell(val);
  3112. }
  3113. }
  3114. }
  3115. private static BaseColor GetPDFColor(Color cc)
  3116. {
  3117. //IL_0001: Unknown result type (might be due to invalid IL or missing references)
  3118. //IL_0007: Expected O, but got Unknown
  3119. return new BaseColor(cc);
  3120. }
  3121. */
  3122. }
  3123. }