MES_Common2003.cs 106 KB

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