| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using Dongke.IBOSS.Framework.Controls;
- /*using iTextSharp.text;
- using iTextSharp.text.pdf;*/
- using Microsoft.VisualBasic;
- using Microsoft.VisualBasic.CompilerServices;
- using Microsoft.VisualBasic.FileIO;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- namespace Dongke.IBOSS.PRD.Framework.Controls
- {
- public class MES_Common2003
- {
- /* private class testevent : IPdfPageEvent
- {
- private PdfContentByte cb;
- private PdfTemplate template;
- public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
- {
- }
- public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
- {
- }
- public void OnCloseDocument(PdfWriter writer, Document document)
- {
- ((PdfContentByte)template).BeginText();
- ((PdfContentByte)template).SetFontAndSize(BaseFont.CreateFont(), 10f);
- ((PdfContentByte)template).SetTextMatrix(0f, 0f);
- ((PdfContentByte)template).ShowText(Conversions.ToString(checked(writer.PageNumber - 1)));
- ((PdfContentByte)template).EndText();
- }
- public void OnEndPage(PdfWriter writer, Document document)
- {
- string text = " - " + writer.PageNumber;
- BaseFont val = BaseFont.CreateFont();
- float widthPoint = val.GetWidthPoint("1", 10f);
- Rectangle pageSize = document.PageSize;
- cb.SetRGBColorFill(100, 100, 100);
- cb.BeginText();
- cb.SetFontAndSize(val, 10f);
- cb.SetTextMatrix(document.LeftMargin + (document.PageSize.Width - document.RightMargin - document.LeftMargin) / 2f, pageSize.GetBottom(document.BottomMargin) - document.BottomMargin / 3f);
- cb.ShowText(text);
- cb.EndText();
- cb.AddTemplate(template, document.LeftMargin - widthPoint + (document.PageSize.Width - document.RightMargin - document.LeftMargin) / 2f, pageSize.GetBottom(document.BottomMargin) - document.BottomMargin / 3f);
- }
- public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text)
- {
- }
- public void OnOpenDocument(PdfWriter writer, Document document)
- {
- cb = writer.DirectContent;
- template = cb.CreateTemplate(50f, 50f);
- }
- public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
- {
- }
- public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
- {
- }
- public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
- {
- }
- public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
- {
- }
- public void OnStartPage(PdfWriter writer, Document document)
- {
- }
- }
- */
- private static MES_Common2003 _instance;
- /*private Document mydoc;
- private PdfWriter mywriter;*/
- private float _paperwidth;
- private float _paperheight;
- private bool _paperlandscape;
- private Margins _margin;
- private float myratio;
- private bool _repeatheader;
- private bool disposedValue;
- public static MES_Common2003 Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new MES_Common2003();
- }
- return _instance;
- }
- }
- public bool RepeatHeader
- {
- get
- {
- return _repeatheader;
- }
- set
- {
- _repeatheader = value;
- }
- }
- public Margins PaperMargin
- {
- get
- {
- return _margin;
- }
- set
- {
- _margin = value;
- }
- }
- public float PaperWidth
- {
- get
- {
- return _paperwidth;
- }
- set
- {
- _paperwidth = value;
- }
- }
- public float PaperHeight
- {
- get
- {
- return _paperheight;
- }
- set
- {
- if (value > 0f)
- {
- _paperheight = value;
- }
- }
- }
- public bool PaperLandScape
- {
- get
- {
- return _paperlandscape;
- }
- set
- {
- _paperlandscape = value;
- }
- }
- private MES_Common2003()
- {
- }
- 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)
- {
- checked
- {
- bool result;
- if (mydgv == null)
- {
- result = false;
- }
- else
- {
- if (titlefont == null)
- {
- titlefont = new Font("黑体", 18f, FontStyle.Bold);
- }
- if (tableheaderfont == null)
- {
- tableheaderfont = new Font("宋体", 12f);
- }
- if (tablefooterfont == null)
- {
- tablefooterfont = new Font("宋体", 12f);
- }
- progressexcel progressexcel = null;
- Module1.G_CancelExportDGVToExcel = false;
- if (isshowprogress)
- {
- progressexcel = new progressexcel();
- progressexcel.TopMost = true;
- progressexcel.Visible = true;
- }
- Ruler ruler = new Ruler();
- try
- {
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
- progressexcel.ProgressBar1.Value = 0;
- Application.DoEvents();
- }
- int columnCount = mydgv.ColumnCount;
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num = 0;
- int num2 = 0;
- int num3 = columnCount - 1;
- for (int i = num2; i <= num3; i++)
- {
- array[i] = i;
- array2[i] = mydgv.Columns[i].DisplayIndex;
- if (mydgv.Columns[i].Visible)
- {
- num++;
- }
- }
- if (num <= 0)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = true;
- }
- else
- {
- Array.Sort(array2, array);
- int[] array3 = new int[num - 1 + 1];
- int num4 = 0;
- int num5 = 0;
- int num6 = columnCount - 1;
- for (int j = num5; j <= num6; j++)
- {
- if (mydgv.Columns[array[j]].Visible)
- {
- array3[num4] = array[j];
- num4++;
- }
- }
- DGVtoExcel2003.w_ZDYcolors = 8;
- int endcol = num - 1;
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
- saveFileDialog.AddExtension = true;
- saveFileDialog.OverwritePrompt = false;
- //saveFileDialog.DefaultExt = "xls";
- //saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
- saveFileDialog.DefaultExt = "xls";
- saveFileDialog.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
- saveFileDialog.FileName = FileName;
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- saveFileDialog.Dispose();
- return false;
- }
- FileName = saveFileDialog.FileName;
- saveFileDialog.Dispose();
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
- {
- case MsgBoxResult.Cancel:
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- return false;
- case MsgBoxResult.Yes:
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
- }
- catch (Exception projectError)
- {
- ProjectData.SetProjectError(projectError);
- Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- break;
- }
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- }
- HSSFWorkbook hSSFWorkbook;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- FileStream fileStream;
- try
- {
- fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
- }
- catch (Exception ex)
- {
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
- fileStream = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- try
- {
- hSSFWorkbook = new HSSFWorkbook(fileStream);
- }
- catch (Exception projectError2)
- {
- ProjectData.SetProjectError(projectError2);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- fileStream.Dispose();
- fileStream = null;
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
- hSSFWorkbook = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- }
- else
- {
- hSSFWorkbook = new HSSFWorkbook();
- }
- string excelNewSheetName = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, sheetname);
- HSSFSheet hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- int num7;
- if (Operators.CompareString(title, "", TextCompare: false) == 0)
- {
- num7 = 0;
- }
- else
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, 0, 0, 0, endcol, title, titlefont);
- num7 = 1;
- }
- if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, Tableheader, tableheaderfont);
- num7++;
- }
- DGVtoExcel2003.w_gridcolor = DGVtoExcel2003.GetXLColour(hSSFWorkbook, mydgv.GridColor);
- if (mydgv.ColumnHeadersVisible)
- {
- if (columnheadertree == null)
- {
- IRow row = hSSFSheet.CreateRow(num7);
- row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.ColumnHeadersHeight, isX: false)) * 72f;
- int num8 = 0;
- int num9 = num - 1;
- for (int k = num8; k <= num9; k++)
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7, k, mydgv.Columns[array3[k]].HeaderText);
- Font dGVHeaderFont = Module1.GetDGVHeaderFont(mydgv.Columns[array3[k]]);
- Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[k]]);
- Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[k]]);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num7, k, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
- }
- }
- else
- {
- mytree mytree = new mytree(columnheadertree, mydgv);
- int leves = mytree.GetLeves();
- int num10 = 1;
- int num11 = leves;
- for (int l = num10; l <= num11; l++)
- {
- IRow row2 = hSSFSheet.CreateRow(num7 + l - 1);
- row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)mydgv.ColumnHeadersHeight / (double)leves), isX: false)) * 72f;
- }
- int num12 = 1;
- int num13 = leves;
- bool flag = true;
- for (int m = num12; m <= num13; m++)
- {
- if (m != 1)
- {
- num7++;
- }
- int num14 = 0;
- int num15 = num - 1;
- for (int n = num14; n <= num15; n++)
- {
- string nodeText = mytree.GetNodeText(array3[n], m);
- int cellHeightLevels = mytree.GetCellHeightLevels(array3[n], m, isTopNodeFill, out flag);
- int cellWidthCols = mytree.GetCellWidthCols(array3[n], m);
- if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
- {
- continue;
- }
- Font font = mytree.GetNodeFont(array3[n], m);
- if (font == null)
- {
- font = Module1.GetDGVHeaderFont(mydgv.Columns[array3[n]]);
- }
- Color forcolor = mytree.GetNodeForeColor(array3[n], m);
- if (forcolor.IsEmpty)
- {
- forcolor = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[n]]);
- }
- Color bkcolor = mytree.GetNodeBackColor(array3[n], m);
- if (bkcolor.IsEmpty)
- {
- bkcolor = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[n]]);
- }
- if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
- {
- int num16 = num7;
- if (!flag)
- {
- num16++;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num16, n, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num16, n, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- continue;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7, n, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num7, n, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- int num17 = num7;
- int num18 = num7 + cellHeightLevels - 1;
- for (int num19 = num17; num19 <= num18; num19++)
- {
- int num20 = n;
- int num21 = n + cellWidthCols - 1;
- for (int num22 = num20; num22 <= num21; num22++)
- {
- if (unchecked(num19 != num7 || num22 != n))
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num19, num22, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num7, n, num7 + cellHeightLevels - 1, n + cellWidthCols - 1);
- }
- }
- mytree.Dispose();
- }
- num7++;
- }
- int num23 = 1;
- int num24 = 0;
- int num25 = 0;
- int num26 = 0;
- int num27 = 0;
- int num28 = 1;
- int num29 = 0;
- int num30 = mydgv.RowCount - 1;
- for (int num31 = num29; num31 <= num30; num31++)
- {
- IRow row3 = hSSFSheet.CreateRow(num7 + num31);
- row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num31].Height, isX: false)) * 72f;
- if (!mydgv.Rows[num31].Visible)
- {
- row3.ZeroHeight = true;
- }
- if (num31 + num7 == 65535)
- {
- break;
- }
- }
- int num32 = 0;
- int num33 = 0;
- if (isshowprogress)
- {
- progressexcel.ProgressBar1.Maximum = mydgv.RowCount;
- }
- int num34 = 0;
- int num35 = mydgv.RowCount - 1;
- for (int num36 = num34; num36 <= num35; num36++)
- {
- bool flag2 = false;
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num36 + 1) + "〗条记录,共〖" + Conversions.ToString(mydgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(num36 + 1) / (double)mydgv.RowCount, "###.##%") + "〗";
- progressexcel.ProgressBar1.Value = num36 + 1;
- Application.DoEvents();
- }
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- int num37 = 0;
- int num38 = num - 1;
- for (num4 = num37; num4 <= num38; num4++)
- {
- int num39;
- if (mydgv.Columns[array3[num4]].ValueType == null)
- {
- num39 = 0;
- }
- else
- {
- switch (Strings.Left(mydgv.Columns[array3[num4]].ValueType.Name.ToUpper(), 3))
- {
- case "STR":
- num39 = 0;
- break;
- case "INT":
- case "DEC":
- num39 = 1;
- flag2 = false;
- break;
- case "DAT":
- num39 = 2;
- break;
- case "BOO":
- num39 = 3;
- flag2 = false;
- break;
- default:
- num39 = 0;
- break;
- }
- }
- num28 = 1;
- num23 = 1;
- num26 = 0;
- num24 = 0;
- num27 = 0;
- num26 = 0;
- num24 = 0;
- num25 = 0;
- string text = Conversions.ToString(mydgv.Rows[num36].Cells[array3[num4]].Tag);
- switch (text)
- {
- default:
- {
- float mywidth = 0f;
- float num40 = 9f;
- int firstcolumn = 0;
- int firstrow = 0;
- int firstc = 0;
- int firstr = 0;
- switch (text)
- {
- default:
- num28 = 1;
- num23 = 1;
- break;
- case "合并":
- case "左上合并":
- case "左下合并":
- case "右上合并":
- case "右下合并":
- case "上边合并":
- case "下边合并":
- case "左边合并":
- case "右边合并":
- 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);
- num28 = num26 + num27 + 1;
- num23 = num24 + num25 + 1;
- break;
- case "水平合并":
- case "#水平合并#":
- case "左合并":
- case "右合并":
- mywidth = (int)Math.Round(mymerge.GetHMergeWidth(mydgv, array3[num4], num36, ref num28, ref num26, ref num27, ref firstcolumn, ref firstc, iscancheckheight: true));
- break;
- }
- break;
- }
- case "垂直合并":
- case "#垂直合并#":
- case "上合并":
- case "下合并":
- {
- int firstrow = 0;
- int firstr = 0;
- float num40 = mymerge.GetVMergeHeight(mydgv, array3[num4], num36, ref num23, ref num25, ref num24, ref firstrow, ref firstr);
- break;
- }
- }
- if (unchecked(num26 > 0 || num24 > 0))
- {
- continue;
- }
- if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewImageColumn", TextCompare: false) == 0)
- {
- if (mydgv.Rows[num36].Cells[array3[num4]].Value != null)
- {
- Image img = (Image)mydgv.Rows[num36].Cells[array3[num4]].FormattedValue;
- byte[] byteByImage = DGVtoExcel2003.GetByteByImage(img);
- int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
- IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, num4, num7 + num36, num4 + 1, num7 + num36 + 1);
- drawing.CreatePicture(anchor, pictureIndex);
- }
- }
- else if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
- {
- 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))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7 + num36, num4, "√");
- }
- flag2 = false;
- }
- else
- {
- string text2 = Conversions.ToString(mydgv.Rows[num36].Cells[array3[num4]].FormattedValue) ?? "";
- flag2 = Strings.InStr(text2, "\r\n") > 0;
- switch (num39)
- {
- case 1:
- if (Versioned.IsNumeric(text2))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7 + num36, num4, Conversions.ToDouble(text2));
- }
- else
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
- }
- break;
- case 2:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
- break;
- default:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num7 + num36, num4, text2);
- break;
- }
- }
- DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(mydgv.Rows[num36].Cells[array3[num4]]);
- DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
- StringAlignment valign = StringAlignment.Center;
- StringAlignment halign = StringAlignment.Center;
- switch (alignment)
- {
- case DataGridViewContentAlignment.BottomCenter:
- valign = StringAlignment.Far;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.MiddleCenter:
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.TopCenter:
- valign = StringAlignment.Near;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.BottomLeft:
- valign = StringAlignment.Far;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.MiddleLeft:
- valign = StringAlignment.Center;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.TopLeft:
- valign = StringAlignment.Near;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.BottomRight:
- valign = StringAlignment.Far;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.MiddleRight:
- valign = StringAlignment.Center;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.TopRight:
- valign = StringAlignment.Near;
- halign = StringAlignment.Far;
- break;
- }
- if (string.Concat(mydgv.Rows[num36].Cells[array3[num4]].Tag).Contains("合并"))
- {
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- }
- if (!flag2 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
- {
- flag2 = true;
- }
- Color dGVCellBackColor = Module1.GetDGVCellBackColor(mydgv.Rows[num36].Cells[array3[num4]]);
- Color dGVCellForeColor = Module1.GetDGVCellForeColor(mydgv.Rows[num36].Cells[array3[num4]]);
- 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);
- if (unchecked(num28 == 1 && num23 == 1))
- {
- continue;
- }
- int num41 = num7 + num36;
- int num42 = num7 + num36 + num25;
- for (int num43 = num41; num43 <= num42; num43++)
- {
- int num44 = num4;
- int num45 = num4 + num27;
- for (int num46 = num44; num46 <= num45; num46++)
- {
- unchecked
- {
- if (num43 != checked(num7 + num36) || num46 != num4)
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num43, num46, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num7 + num36, num4, num7 + num36 + num25, num4 + num27);
- num4 += num27;
- }
- if (!((num36 + num7 == 65535) & (num36 != mydgv.RowCount - 1)))
- {
- continue;
- }
- int num47 = 0;
- int num48 = num - 1;
- for (int num49 = num47; num49 <= num48; num49++)
- {
- hSSFSheet.SetColumnWidth(num49, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num49]].Width / 8.0) * 256.0));
- }
- num32++;
- excelNewSheetName = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, sheetname);
- hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- num33 = 1;
- int num50 = num36 + 1;
- int num51 = mydgv.RowCount - 1;
- for (num4 = num50; num4 <= num51; num4++)
- {
- IRow row4 = hSSFSheet.CreateRow(num33 - 1);
- if (!mydgv.Rows[num4].Visible)
- {
- row4.ZeroHeight = true;
- }
- row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num4].Height, isX: false)) * 72f;
- if (num33 == 65536)
- {
- break;
- }
- num33++;
- }
- num7 = -num36 - 1;
- }
- num7 = ((num33 != 0) ? (num33 - 1) : (num7 + mydgv.RowCount));
- if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, TableFooter, tablefooterfont);
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- int num52 = 0;
- int num53 = num - 1;
- for (int num54 = num52; num54 <= num53; num54++)
- {
- hSSFSheet.SetColumnWidth(num54, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num54]].Width / 8.0) * 256.0));
- }
- ruler.Dispose();
- ruler = null;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(excelNewSheetName));
- if (!DGVtoExcel2003.WriteToFile(hSSFWorkbook, FileName))
- {
- hSSFWorkbook = null;
- result = false;
- }
- else
- {
- hSSFWorkbook = null;
- if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
- {
- try
- {
- Process.Start(FileName);
- }
- catch (Exception projectError3)
- {
- ProjectData.SetProjectError(projectError3);
- try
- {
- Thread.Sleep(100);
- Process.Start(FileName);
- }
- catch (Exception projectError4)
- {
- ProjectData.SetProjectError(projectError4);
- ProjectData.ClearProjectError();
- }
- ProjectData.ClearProjectError();
- }
- }
- result = true;
- }
- }
- }
- catch (Exception projectError5)
- {
- ProjectData.SetProjectError(projectError5);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- try
- {
- ruler.Dispose();
- ruler = null;
- }
- catch (Exception projectError6)
- {
- ProjectData.SetProjectError(projectError6);
- ProjectData.ClearProjectError();
- }
- if (Operators.CompareString(FileName, "", TextCompare: false) == 0)
- {
- result = DGVtoExcel2003.myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: true);
- ProjectData.ClearProjectError();
- }
- else
- {
- result = DGVtoExcel2003.myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: false);
- ProjectData.ClearProjectError();
- }
- }
- }
- return result;
- }
- }
- 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)
- {
- bool result = true;
- checked
- {
- if (mydgvs == null || mydgvs.Count == 0 || sheetnames == null || sheetnames.Count == 0 || mydgvs.Count != sheetnames.Count)
- {
- result = false;
- }
- else
- {
- if (columnheadertrees != null && columnheadertrees.Count > 0 && mydgvs.Count != columnheadertrees.Count)
- {
- return false;
- }
- int num = 0;
- int num2 = 0;
- for (int i = 0; i < mydgvs.Count; i++)
- {
- num += mydgvs[i].Rows.Count;
- }
- if (titlefont == null)
- {
- titlefont = new Font("黑体", 18f, FontStyle.Bold);
- }
- if (tableheaderfont == null)
- {
- tableheaderfont = new Font("宋体", 12f);
- }
- if (tablefooterfont == null)
- {
- tablefooterfont = new Font("宋体", 12f);
- }
- progressexcel progressexcel = null;
- Module1.G_CancelExportDGVToExcel = false;
- if (isshowprogress)
- {
- progressexcel = new progressexcel();
- progressexcel.TopMost = true;
- progressexcel.Visible = true;
- }
- Ruler ruler = new Ruler();
- try
- {
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
- progressexcel.ProgressBar1.Value = 0;
- Application.DoEvents();
- }
- bool flag = false;
- DataGridView dataGridView = null;
- TreeView treeView = null;
- HSSFWorkbook hSSFWorkbook = null;
- string empty = string.Empty;
- for (int j = 0; j < mydgvs.Count; j++)
- {
- empty = sheetnames[j];
- dataGridView = mydgvs[j];
- if (columnheadertrees != null && columnheadertrees.Count > 0)
- {
- treeView = columnheadertrees[j];
- }
- int columnCount = dataGridView.ColumnCount;
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num3 = 0;
- int num4 = 0;
- int num5 = columnCount - 1;
- for (int k = num4; k <= num5; k++)
- {
- array[k] = k;
- array2[k] = dataGridView.Columns[k].DisplayIndex;
- if (dataGridView.Columns[k].Visible)
- {
- num3++;
- }
- }
- if (num3 <= 0)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = true;
- }
- else
- {
- Array.Sort(array2, array);
- int[] array3 = new int[num3 - 1 + 1];
- int num6 = 0;
- int num7 = 0;
- int num8 = columnCount - 1;
- for (int l = num7; l <= num8; l++)
- {
- if (dataGridView.Columns[array[l]].Visible)
- {
- array3[num6] = array[l];
- num6++;
- }
- }
- DGVtoExcel2003.w_ZDYcolors = 8;
- int endcol = num3 - 1;
- if (!flag)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
- saveFileDialog.AddExtension = true;
- saveFileDialog.OverwritePrompt = false;
- saveFileDialog.DefaultExt = "xls";
- saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
- saveFileDialog.FileName = FileName;
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- saveFileDialog.Dispose();
- result = false;
- return result;
- }
- FileName = saveFileDialog.FileName;
- saveFileDialog.Dispose();
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
- {
- case MsgBoxResult.Cancel:
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = false;
- return result;
- case MsgBoxResult.Yes:
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
- }
- catch (Exception projectError)
- {
- ProjectData.SetProjectError(projectError);
- Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- break;
- }
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- }
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- FileStream fileStream;
- try
- {
- fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
- }
- catch (Exception ex)
- {
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
- fileStream = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- try
- {
- hSSFWorkbook = new HSSFWorkbook(fileStream);
- }
- catch (Exception projectError2)
- {
- ProjectData.SetProjectError(projectError2);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- fileStream.Dispose();
- fileStream = null;
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
- hSSFWorkbook = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- }
- else
- {
- hSSFWorkbook = new HSSFWorkbook();
- }
- flag = true;
- }
- string excelNewSheetName = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, empty);
- HSSFSheet hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- int num9;
- if (Operators.CompareString(title, "", TextCompare: false) == 0)
- {
- num9 = 0;
- }
- else if (isshowSheetTitle)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, 0, 0, 0, endcol, empty, titlefont);
- num9 = 1;
- }
- else
- {
- num9 = 0;
- }
- if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, Tableheader, tableheaderfont);
- num9++;
- }
- DGVtoExcel2003.w_gridcolor = DGVtoExcel2003.GetXLColour(hSSFWorkbook, dataGridView.GridColor);
- if (dataGridView.ColumnHeadersVisible)
- {
- if (treeView == null)
- {
- IRow row = hSSFSheet.CreateRow(num9);
- row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.ColumnHeadersHeight, isX: false)) * 72f;
- int num10 = 0;
- int num11 = num3 - 1;
- for (int m = num10; m <= num11; m++)
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9, m, dataGridView.Columns[array3[m]].HeaderText);
- Font dGVHeaderFont = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[m]]);
- Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[m]]);
- Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[m]]);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, m, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
- }
- }
- else
- {
- mytree mytree = new mytree(treeView, dataGridView);
- int leves = mytree.GetLeves();
- int num12 = 1;
- int num13 = leves;
- for (int n = num12; n <= num13; n++)
- {
- IRow row2 = hSSFSheet.CreateRow(num9 + n - 1);
- row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)dataGridView.ColumnHeadersHeight / (double)leves), isX: false)) * 72f;
- }
- int num14 = 1;
- int num15 = leves;
- bool flag2 = true;
- for (int num16 = num14; num16 <= num15; num16++)
- {
- if (num16 != 1)
- {
- num9++;
- }
- int num17 = 0;
- int num18 = num3 - 1;
- for (int num19 = num17; num19 <= num18; num19++)
- {
- string nodeText = mytree.GetNodeText(array3[num19], num16);
- int cellHeightLevels = mytree.GetCellHeightLevels(array3[num19], num16, isTopNodeFill, out flag2);
- int cellWidthCols = mytree.GetCellWidthCols(array3[num19], num16);
- if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
- {
- continue;
- }
- Font font = mytree.GetNodeFont(array3[num19], num16);
- if (font == null)
- {
- font = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[num19]]);
- }
- Color forcolor = mytree.GetNodeForeColor(array3[num19], num16);
- if (forcolor.IsEmpty)
- {
- forcolor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[num19]]);
- }
- Color bkcolor = mytree.GetNodeBackColor(array3[num19], num16);
- if (bkcolor.IsEmpty)
- {
- bkcolor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[num19]]);
- }
- if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
- {
- int num20 = num9;
- if (!flag2)
- {
- num20++;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num20, num19, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num20, num19, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- continue;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9, num19, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, num19, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- int num21 = num9;
- int num22 = num9 + cellHeightLevels - 1;
- for (int num23 = num21; num23 <= num22; num23++)
- {
- int num24 = num19;
- int num25 = num19 + cellWidthCols - 1;
- for (int num26 = num24; num26 <= num25; num26++)
- {
- if (unchecked(num23 != num9 || num26 != num19))
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num23, num26, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num9, num19, num9 + cellHeightLevels - 1, num19 + cellWidthCols - 1);
- }
- }
- mytree.Dispose();
- }
- num9++;
- }
- int num27 = 1;
- int num28 = 0;
- int num29 = 0;
- int num30 = 0;
- int num31 = 0;
- int num32 = 1;
- int num33 = 0;
- int num34 = dataGridView.RowCount - 1;
- for (int num35 = num33; num35 <= num34; num35++)
- {
- IRow row3 = hSSFSheet.CreateRow(num9 + num35);
- row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num35].Height, isX: false)) * 72f;
- if (!dataGridView.Rows[num35].Visible)
- {
- row3.ZeroHeight = true;
- }
- if (num35 + num9 == 65535)
- {
- break;
- }
- }
- int num36 = 0;
- int num37 = 0;
- if (isshowprogress)
- {
- progressexcel.ProgressBar1.Maximum = num;
- }
- int num38 = 0;
- int num39 = dataGridView.RowCount - 1;
- for (int num40 = num38; num40 <= num39; num40++)
- {
- bool flag3 = false;
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num40 + num2 + 1) + "〗条记录,共〖" + Conversions.ToString(num) + "〗条记录,已完成〖" + Strings.Format((double)(num40 + num2 + 1) / (double)num, "###.##%") + "〗";
- progressexcel.ProgressBar1.Value = num40 + 1;
- Application.DoEvents();
- }
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- int num41 = 0;
- int num42 = num3 - 1;
- for (num6 = num41; num6 <= num42; num6++)
- {
- int num43;
- if (dataGridView.Columns[array3[num6]].ValueType == null)
- {
- num43 = 0;
- }
- else
- {
- switch (Strings.Left(dataGridView.Columns[array3[num6]].ValueType.Name.ToUpper(), 3))
- {
- case "STR":
- num43 = 0;
- break;
- case "INT":
- case "DEC":
- num43 = 1;
- flag3 = false;
- break;
- case "DAT":
- num43 = 2;
- break;
- case "BOO":
- num43 = 3;
- flag3 = false;
- break;
- default:
- num43 = 0;
- break;
- }
- }
- num32 = 1;
- num27 = 1;
- num30 = 0;
- num28 = 0;
- num31 = 0;
- num30 = 0;
- num28 = 0;
- num29 = 0;
- string text = Conversions.ToString(dataGridView.Rows[num40].Cells[array3[num6]].Tag);
- switch (text)
- {
- default:
- {
- float mywidth = 0f;
- float num44 = 9f;
- int firstcolumn = 0;
- int firstrow = 0;
- int firstc = 0;
- int firstr = 0;
- switch (text)
- {
- default:
- num32 = 1;
- num27 = 1;
- break;
- case "合并":
- case "左上合并":
- case "左下合并":
- case "右上合并":
- case "右下合并":
- case "上边合并":
- case "下边合并":
- case "左边合并":
- case "右边合并":
- 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);
- num32 = num30 + num31 + 1;
- num27 = num28 + num29 + 1;
- break;
- case "水平合并":
- case "#水平合并#":
- case "左合并":
- case "右合并":
- mywidth = (int)Math.Round(mymerge.GetHMergeWidth(dataGridView, array3[num6], num40, ref num32, ref num30, ref num31, ref firstcolumn, ref firstc, iscancheckheight: true));
- break;
- }
- break;
- }
- case "垂直合并":
- case "#垂直合并#":
- case "上合并":
- case "下合并":
- {
- int firstrow = 0;
- int firstr = 0;
- float num44 = mymerge.GetVMergeHeight(dataGridView, array3[num6], num40, ref num27, ref num29, ref num28, ref firstrow, ref firstr);
- break;
- }
- }
- if (unchecked(num30 > 0 || num28 > 0))
- {
- continue;
- }
- if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewImageColumn", TextCompare: false) == 0)
- {
- if (dataGridView.Rows[num40].Cells[array3[num6]].Value != null)
- {
- Image img = (Image)dataGridView.Rows[num40].Cells[array3[num6]].FormattedValue;
- byte[] byteByImage = DGVtoExcel2003.GetByteByImage(img);
- int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
- IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, num6, num9 + num40, num6 + 1, num9 + num40 + 1);
- drawing.CreatePicture(anchor, pictureIndex);
- }
- }
- else if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
- {
- 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))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num40, num6, "√");
- }
- flag3 = false;
- }
- else
- {
- string text2 = Conversions.ToString(dataGridView.Rows[num40].Cells[array3[num6]].FormattedValue) ?? "";
- flag3 = Strings.InStr(text2, "\r\n") > 0;
- switch (num43)
- {
- case 1:
- if (Versioned.IsNumeric(text2))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num40, num6, Conversions.ToDouble(text2));
- }
- else
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
- }
- break;
- case 2:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
- break;
- default:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num40, num6, text2);
- break;
- }
- }
- DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(dataGridView.Rows[num40].Cells[array3[num6]]);
- DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
- StringAlignment valign = StringAlignment.Center;
- StringAlignment halign = StringAlignment.Center;
- switch (alignment)
- {
- case DataGridViewContentAlignment.BottomCenter:
- valign = StringAlignment.Far;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.MiddleCenter:
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.TopCenter:
- valign = StringAlignment.Near;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.BottomLeft:
- valign = StringAlignment.Far;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.MiddleLeft:
- valign = StringAlignment.Center;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.TopLeft:
- valign = StringAlignment.Near;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.BottomRight:
- valign = StringAlignment.Far;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.MiddleRight:
- valign = StringAlignment.Center;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.TopRight:
- valign = StringAlignment.Near;
- halign = StringAlignment.Far;
- break;
- }
- if (string.Concat(dataGridView.Rows[num40].Cells[array3[num6]].Tag).Contains("合并"))
- {
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- }
- if (!flag3 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
- {
- flag3 = true;
- }
- Color dGVCellBackColor = Module1.GetDGVCellBackColor(dataGridView.Rows[num40].Cells[array3[num6]]);
- Color dGVCellForeColor = Module1.GetDGVCellForeColor(dataGridView.Rows[num40].Cells[array3[num6]]);
- 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);
- if (unchecked(num32 == 1 && num27 == 1))
- {
- continue;
- }
- int num45 = num9 + num40;
- int num46 = num9 + num40 + num29;
- for (int num47 = num45; num47 <= num46; num47++)
- {
- int num48 = num6;
- int num49 = num6 + num31;
- for (int num50 = num48; num50 <= num49; num50++)
- {
- unchecked
- {
- if (num47 != checked(num9 + num40) || num50 != num6)
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num47, num50, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num9 + num40, num6, num9 + num40 + num29, num6 + num31);
- num6 += num31;
- }
- if (!((num40 + num9 == 65535) & (num40 != dataGridView.RowCount - 1)))
- {
- continue;
- }
- int num51 = 0;
- int num52 = num3 - 1;
- for (int num53 = num51; num53 <= num52; num53++)
- {
- hSSFSheet.SetColumnWidth(num53, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num53]].Width / 8.0) * 256.0));
- }
- num36++;
- excelNewSheetName = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, empty);
- hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- num37 = 1;
- int num54 = num40 + 1;
- int num55 = dataGridView.RowCount - 1;
- for (num6 = num54; num6 <= num55; num6++)
- {
- IRow row4 = hSSFSheet.CreateRow(num37 - 1);
- if (!dataGridView.Rows[num6].Visible)
- {
- row4.ZeroHeight = true;
- }
- row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num6].Height, isX: false)) * 72f;
- if (num37 == 65536)
- {
- break;
- }
- num37++;
- }
- num9 = -num40 - 1;
- }
- num9 = ((num37 != 0) ? (num37 - 1) : (num9 + dataGridView.RowCount));
- if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, TableFooter, tablefooterfont);
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- int num56 = 0;
- int num57 = num3 - 1;
- for (int num58 = num56; num58 <= num57; num58++)
- {
- hSSFSheet.SetColumnWidth(num58, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num58]].Width / 8.0) * 256.0));
- }
- hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(excelNewSheetName));
- }
- num2 += mydgvs[j].Rows.Count;
- }
- ruler.Dispose();
- ruler = null;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- if (!DGVtoExcel2003.WriteToFile(hSSFWorkbook, FileName))
- {
- hSSFWorkbook = null;
- result = false;
- }
- else
- {
- hSSFWorkbook = null;
- if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
- {
- try
- {
- Process.Start(FileName);
- }
- catch (Exception projectError3)
- {
- ProjectData.SetProjectError(projectError3);
- try
- {
- Thread.Sleep(100);
- Process.Start(FileName);
- }
- catch (Exception projectError4)
- {
- ProjectData.SetProjectError(projectError4);
- ProjectData.ClearProjectError();
- }
- ProjectData.ClearProjectError();
- }
- }
- result = true;
- }
- }
- catch (Exception projectError5)
- {
- ProjectData.SetProjectError(projectError5);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- try
- {
- ruler.Dispose();
- ruler = null;
- }
- catch (Exception projectError6)
- {
- ProjectData.SetProjectError(projectError6);
- ProjectData.ClearProjectError();
- }
- }
- }
- return result;
- }
- }
- /*
- 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)
- {
- bool result = true;
- int num = 0;
- checked
- {
- if (mydgvs == null || mydgvs.Count == 0 || sheetnames == null || sheetnames.Count == 0 || mydgvs.Count != sheetnames.Count)
- {
- result = false;
- }
- else
- {
- if (columnheadertrees != null && columnheadertrees.Count > 0 && mydgvs.Count != columnheadertrees.Count)
- {
- return false;
- }
- int num2 = 0;
- if (titlefont == null)
- {
- titlefont = new Font("黑体", 18f, FontStyle.Bold);
- }
- if (tableheaderfont == null)
- {
- tableheaderfont = new Font("宋体", 12f);
- }
- if (tablefooterfont == null)
- {
- tablefooterfont = new Font("宋体", 12f);
- }
- progressexcel progressexcel = null;
- Module1.G_CancelExportDGVToExcel = false;
- if (isshowprogress)
- {
- progressexcel = new progressexcel();
- progressexcel.TopMost = true;
- progressexcel.Visible = true;
- }
- Ruler ruler = new Ruler();
- try
- {
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
- progressexcel.ProgressBar1.Value = 0;
- Application.DoEvents();
- }
- bool flag = false;
- DataGridView dataGridView = null;
- TreeView treeView = null;
- HSSFWorkbook hSSFWorkbook = null;
- HSSFSheet hSSFSheet = null;
- string empty = string.Empty;
- string text = string.Empty;
- for (int i = 0; i < mydgvs.Count; i++)
- {
- empty = sheetnames[i];
- dataGridView = mydgvs[i];
- if (columnheadertrees != null && columnheadertrees.Count > 0)
- {
- treeView = columnheadertrees[i];
- }
- int columnCount = dataGridView.ColumnCount;
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num3 = 0;
- int num4 = 0;
- int num5 = columnCount - 1;
- for (int j = num4; j <= num5; j++)
- {
- array[j] = j;
- array2[j] = dataGridView.Columns[j].DisplayIndex;
- if (dataGridView.Columns[j].Visible)
- {
- num3++;
- }
- }
- if (num3 <= 0)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = true;
- }
- else
- {
- Array.Sort(array2, array);
- int[] array3 = new int[num3 - 1 + 1];
- int num6 = 0;
- int num7 = 0;
- int num8 = columnCount - 1;
- for (int k = num7; k <= num8; k++)
- {
- if (dataGridView.Columns[array[k]].Visible)
- {
- array3[num6] = array[k];
- num6++;
- }
- }
- DGVtoExcel2003.w_ZDYcolors = 8;
- int endcol = num3 - 1;
- if (!flag)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "请选择要导出Excel文件的路径和文件名";
- saveFileDialog.AddExtension = true;
- saveFileDialog.OverwritePrompt = false;
- saveFileDialog.DefaultExt = "xls";
- saveFileDialog.Filter = "97-2003 Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
- saveFileDialog.FileName = FileName;
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- saveFileDialog.Dispose();
- result = false;
- return result;
- }
- FileName = saveFileDialog.FileName;
- saveFileDialog.Dispose();
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- if (isshowprogress)
- {
- progressexcel.Visible = false;
- }
- switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
- {
- case MsgBoxResult.Cancel:
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = false;
- return result;
- case MsgBoxResult.Yes:
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
- }
- catch (Exception projectError)
- {
- ProjectData.SetProjectError(projectError);
- Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- break;
- }
- if (isshowprogress)
- {
- progressexcel.Visible = true;
- }
- }
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- FileStream fileStream;
- try
- {
- fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
- }
- catch (Exception ex)
- {
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
- fileStream = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- try
- {
- hSSFWorkbook = new HSSFWorkbook(fileStream);
- }
- catch (Exception projectError2)
- {
- ProjectData.SetProjectError(projectError2);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- fileStream.Dispose();
- fileStream = null;
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败,可能不是有效的Office97-2003 Excel文件格式,因此无法向该文件中追加导出的数据", MsgBoxStyle.Exclamation, "提示信息");
- hSSFWorkbook = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- }
- else
- {
- hSSFWorkbook = new HSSFWorkbook();
- }
- flag = true;
- }
- if (hSSFSheet == null)
- {
- text = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, title);
- hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(text);
- }
- int num9;
- if (Operators.CompareString(title, "", TextCompare: false) == 0)
- {
- num9 = 0;
- }
- else
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num2, num2, 0, endcol, empty, titlefont);
- num9 = 1;
- }
- if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num9 + num2, num9 + num2, 0, endcol, Tableheader, tableheaderfont);
- num9++;
- }
- num9 += num2;
- DGVtoExcel2003.w_gridcolor = DGVtoExcel2003.GetXLColour(hSSFWorkbook, dataGridView.GridColor);
- if (dataGridView.ColumnHeadersVisible)
- {
- if (treeView == null)
- {
- IRow row = hSSFSheet.CreateRow(num9);
- row.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.ColumnHeadersHeight, isX: false)) * 72f;
- int num10 = 0;
- int num11 = num3 - 1;
- for (int l = num10; l <= num11; l++)
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9, l, dataGridView.Columns[array3[l]].HeaderText);
- Font dGVHeaderFont = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[l]]);
- Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[l]]);
- Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[l]]);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, l, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
- }
- }
- else
- {
- mytree mytree = new mytree(treeView, dataGridView);
- num = mytree.GetLeves();
- int num12 = 1;
- int num13 = num;
- for (int m = num12; m <= num13; m++)
- {
- IRow row2 = hSSFSheet.CreateRow(num9 + m - 1);
- row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)dataGridView.ColumnHeadersHeight / (double)num), isX: false)) * 72f;
- }
- int num14 = 1;
- int num15 = num;
- for (int n = num14; n <= num15; n++)
- {
- if (n != 1)
- {
- num9++;
- }
- int num16 = 0;
- int num17 = num3 - 1;
- bool flag2 = true;
- for (int num18 = num16; num18 <= num17; num18++)
- {
- string nodeText = mytree.GetNodeText(array3[num18], n);
- int cellHeightLevels = mytree.GetCellHeightLevels(array3[num18], n, isTopNodeFill, out flag2);
- int cellWidthCols = mytree.GetCellWidthCols(array3[num18], n);
- if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
- {
- continue;
- }
- Font font = mytree.GetNodeFont(array3[num18], n);
- if (font == null)
- {
- font = Module1.GetDGVHeaderFont(dataGridView.Columns[array3[num18]]);
- }
- Color forcolor = mytree.GetNodeForeColor(array3[num18], n);
- if (forcolor.IsEmpty)
- {
- forcolor = Module1.GetDGVHeaderForeColor(dataGridView.Columns[array3[num18]]);
- }
- Color bkcolor = mytree.GetNodeBackColor(array3[num18], n);
- if (bkcolor.IsEmpty)
- {
- bkcolor = Module1.GetDGVHeaderBackColor(dataGridView.Columns[array3[num18]]);
- }
- if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
- {
- int num19 = num9;
- if (!flag2)
- {
- num19++;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num19, num18, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num19, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- continue;
- }
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9, num18, nodeText);
- DGVtoExcel2003.SetCellStyle(hSSFWorkbook, hSSFSheet, num9, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- int num20 = num9;
- int num21 = num9 + cellHeightLevels - 1;
- for (int num22 = num20; num22 <= num21; num22++)
- {
- int num23 = num18;
- int num24 = num18 + cellWidthCols - 1;
- for (int num25 = num23; num25 <= num24; num25++)
- {
- if (unchecked(num22 != num9 || num25 != num18))
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num22, num25, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num9, num18, num9 + cellHeightLevels - 1, num18 + cellWidthCols - 1);
- }
- }
- mytree.Dispose();
- }
- num9++;
- }
- int num26 = 1;
- int num27 = 0;
- int num28 = 0;
- int num29 = 0;
- int num30 = 0;
- int num31 = 1;
- int num32 = 0;
- int num33 = dataGridView.RowCount - 1;
- for (int num34 = num32; num34 <= num33; num34++)
- {
- IRow row3 = hSSFSheet.CreateRow(num9 + num34);
- row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num34].Height, isX: false)) * 72f;
- if (!dataGridView.Rows[num34].Visible)
- {
- row3.ZeroHeight = true;
- }
- if (num34 + num9 == 65535)
- {
- break;
- }
- }
- int num35 = 0;
- int num36 = 0;
- if (isshowprogress)
- {
- progressexcel.ProgressBar1.Maximum = dataGridView.RowCount;
- }
- int num37 = 0;
- int num38 = dataGridView.RowCount - 1;
- for (int num39 = num37; num39 <= num38; num39++)
- {
- bool flag3 = false;
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(num39 + 1) + "〗条记录,共〖" + Conversions.ToString(dataGridView.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(num39 + 1) / (double)dataGridView.RowCount, "###.##%") + "〗";
- progressexcel.ProgressBar1.Value = num39 + 1;
- Application.DoEvents();
- }
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- int num40 = 0;
- int num41 = num3 - 1;
- for (num6 = num40; num6 <= num41; num6++)
- {
- int num42;
- if (dataGridView.Columns[array3[num6]].ValueType == null)
- {
- num42 = 0;
- }
- else
- {
- switch (Strings.Left(dataGridView.Columns[array3[num6]].ValueType.Name.ToUpper(), 3))
- {
- case "STR":
- num42 = 0;
- break;
- case "INT":
- case "DEC":
- num42 = 1;
- flag3 = false;
- break;
- case "DAT":
- num42 = 2;
- break;
- case "BOO":
- num42 = 3;
- flag3 = false;
- break;
- default:
- num42 = 0;
- break;
- }
- }
- num31 = 1;
- num26 = 1;
- num29 = 0;
- num27 = 0;
- num30 = 0;
- num29 = 0;
- num27 = 0;
- num28 = 0;
- string text2 = Conversions.ToString(dataGridView.Rows[num39].Cells[array3[num6]].Tag);
- switch (text2)
- {
- default:
- {
- float mywidth = 0f;
- float num43 = 9f;
- int firstcolumn = 0;
- int firstrow = 0;
- int firstc = 0;
- int firstr = 0;
- switch (text2)
- {
- default:
- num31 = 1;
- num26 = 1;
- break;
- case "合并":
- case "左上合并":
- case "左下合并":
- case "右上合并":
- case "右下合并":
- case "上边合并":
- case "下边合并":
- case "左边合并":
- case "右边合并":
- 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);
- num31 = num29 + num30 + 1;
- num26 = num27 + num28 + 1;
- break;
- case "水平合并":
- case "#水平合并#":
- case "左合并":
- case "右合并":
- mywidth = (int)Math.Round(mymerge.GetHMergeWidth(dataGridView, array3[num6], num39, ref num31, ref num29, ref num30, ref firstcolumn, ref firstc, iscancheckheight: true));
- break;
- }
- break;
- }
- case "垂直合并":
- case "#垂直合并#":
- case "上合并":
- case "下合并":
- {
- int firstrow = 0;
- int firstr = 0;
- float num43 = mymerge.GetVMergeHeight(dataGridView, array3[num6], num39, ref num26, ref num28, ref num27, ref firstrow, ref firstr);
- break;
- }
- }
- if (unchecked(num29 > 0 || num27 > 0))
- {
- continue;
- }
- if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewImageColumn", TextCompare: false) == 0)
- {
- if (dataGridView.Rows[num39].Cells[array3[num6]].Value != null)
- {
- Image img = (Image)dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue;
- byte[] byteByImage = DGVtoExcel2003.GetByteByImage(img);
- int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
- IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, num6, num9 + num39, num6 + 1, num9 + num39 + 1);
- drawing.CreatePicture(anchor, pictureIndex);
- }
- }
- else if (Operators.CompareString(Versioned.TypeName(dataGridView.Columns[array3[num6]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
- {
- if (dataGridView.Rows[num39].Cells[array3[num6]].Value != null && Conversions.ToBoolean(dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num39, num6, "√");
- }
- flag3 = false;
- }
- else
- {
- string text3 = Conversions.ToString(dataGridView.Rows[num39].Cells[array3[num6]].FormattedValue) ?? "";
- flag3 = Strings.InStr(text3, "\r\n") > 0;
- switch (num42)
- {
- case 1:
- if (Versioned.IsNumeric(text3))
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num39, num6, Conversions.ToDouble(text3));
- }
- else
- {
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
- }
- break;
- case 2:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
- break;
- default:
- DGVtoExcel2003.SetCellValue(hSSFSheet, num9 + num39, num6, text3);
- break;
- }
- }
- DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(dataGridView.Rows[num39].Cells[array3[num6]]);
- DataGridViewContentAlignment alignment = dGVCellStyle.Alignment;
- StringAlignment valign = StringAlignment.Center;
- StringAlignment halign = StringAlignment.Center;
- switch (alignment)
- {
- case DataGridViewContentAlignment.BottomCenter:
- valign = StringAlignment.Far;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.MiddleCenter:
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.TopCenter:
- valign = StringAlignment.Near;
- halign = StringAlignment.Center;
- break;
- case DataGridViewContentAlignment.BottomLeft:
- valign = StringAlignment.Far;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.MiddleLeft:
- valign = StringAlignment.Center;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.TopLeft:
- valign = StringAlignment.Near;
- halign = StringAlignment.Near;
- break;
- case DataGridViewContentAlignment.BottomRight:
- valign = StringAlignment.Far;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.MiddleRight:
- valign = StringAlignment.Center;
- halign = StringAlignment.Far;
- break;
- case DataGridViewContentAlignment.TopRight:
- valign = StringAlignment.Near;
- halign = StringAlignment.Far;
- break;
- }
- if (string.Concat(dataGridView.Rows[num39].Cells[array3[num6]].Tag).Contains("合并"))
- {
- valign = StringAlignment.Center;
- halign = StringAlignment.Center;
- }
- if (!flag3 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
- {
- flag3 = true;
- }
- Color dGVCellBackColor = Module1.GetDGVCellBackColor(dataGridView.Rows[num39].Cells[array3[num6]]);
- Color dGVCellForeColor = Module1.GetDGVCellForeColor(dataGridView.Rows[num39].Cells[array3[num6]]);
- 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);
- if (unchecked(num31 == 1 && num26 == 1))
- {
- continue;
- }
- int num44 = num9 + num39;
- int num45 = num9 + num39 + num28;
- for (int num46 = num44; num46 <= num45; num46++)
- {
- int num47 = num6;
- int num48 = num6 + num30;
- for (int num49 = num47; num49 <= num48; num49++)
- {
- unchecked
- {
- if (num46 != checked(num9 + num39) || num49 != num6)
- {
- DGVtoExcel2003.SetCellBorder(hSSFWorkbook, hSSFSheet, num46, num49, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- }
- DGVtoExcel2003.SetMergeCell(hSSFSheet, num9 + num39, num6, num9 + num39 + num28, num6 + num30);
- num6 += num30;
- }
- if (!((num39 + num9 == 65535) & (num39 != dataGridView.RowCount - 1)))
- {
- continue;
- }
- int num50 = 0;
- int num51 = num3 - 1;
- for (int num52 = num50; num52 <= num51; num52++)
- {
- hSSFSheet.SetColumnWidth(num52, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num52]].Width / 8.0) * 256.0));
- }
- num35++;
- text = DGVtoExcel2003.GetExcelNewSheetName(hSSFWorkbook, empty);
- hSSFSheet = (HSSFSheet)hSSFWorkbook.CreateSheet(text);
- num36 = 1;
- int num53 = num39 + 1;
- int num54 = dataGridView.RowCount - 1;
- for (num6 = num53; num6 <= num54; num6++)
- {
- IRow row4 = hSSFSheet.CreateRow(num36 - 1);
- if (!dataGridView.Rows[num6].Visible)
- {
- row4.ZeroHeight = true;
- }
- row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(dataGridView.Rows[num6].Height, isX: false)) * 72f;
- if (num36 == 65536)
- {
- break;
- }
- num36++;
- }
- num9 = -num39 - 1;
- }
- num9 = ((num36 != 0) ? (num36 - 1) : (num9 + dataGridView.RowCount));
- if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
- {
- DGVtoExcel2003.SetTitle(hSSFWorkbook, hSSFSheet, num9, num9, 0, endcol, TableFooter, tablefooterfont);
- }
- DGVtoExcel2003.w_font = null;
- DGVtoExcel2003.w_ifont = null;
- DGVtoExcel2003.w_Cellstyle = null;
- DGVtoExcel2003.w_iCellstyle = null;
- DGVtoExcel2003.w_color = null;
- int num55 = 0;
- int num56 = num3 - 1;
- for (int num57 = num55; num57 <= num56; num57++)
- {
- hSSFSheet.SetColumnWidth(num57, (int)Math.Round(Math.Ceiling((double)dataGridView.Columns[array3[num57]].Width / 8.0) * 256.0));
- }
- hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(text));
- }
- num2 += mydgvs[i].Rows.Count + ((num == 0) ? 1 : num) + 4;
- }
- ruler.Dispose();
- ruler = null;
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- if (!DGVtoExcel2003.WriteToFile(hSSFWorkbook, FileName))
- {
- hSSFWorkbook = null;
- result = false;
- }
- else
- {
- hSSFWorkbook = null;
- if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + FileName + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
- {
- try
- {
- Process.Start(FileName);
- }
- catch (Exception projectError3)
- {
- ProjectData.SetProjectError(projectError3);
- try
- {
- Thread.Sleep(100);
- Process.Start(FileName);
- }
- catch (Exception projectError4)
- {
- ProjectData.SetProjectError(projectError4);
- ProjectData.ClearProjectError();
- }
- ProjectData.ClearProjectError();
- }
- }
- result = true;
- }
- }
- catch (Exception projectError5)
- {
- ProjectData.SetProjectError(projectError5);
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- try
- {
- ruler.Dispose();
- ruler = null;
- }
- catch (Exception projectError6)
- {
- ProjectData.SetProjectError(projectError6);
- ProjectData.ClearProjectError();
- }
- }
- }
- return result;
- }
- }
- public void ExportDGVToPDF(DataGridView dgv, TreeView tv, string title, Font titlefont, Font tableheaderfont, string tableheader, Font tablefooterfont, string tablefooter, string filename, bool isshowprogress)
- {
- //IL_012f: Unknown result type (might be due to invalid IL or missing references)
- //IL_0139: Expected O, but got Unknown
- //IL_021a: Unknown result type (might be due to invalid IL or missing references)
- //IL_0221: Expected O, but got Unknown
- //IL_03c9: Unknown result type (might be due to invalid IL or missing references)
- //IL_03d0: Expected O, but got Unknown
- //IL_0300: Unknown result type (might be due to invalid IL or missing references)
- //IL_0307: Expected O, but got Unknown
- //IL_04ed: Unknown result type (might be due to invalid IL or missing references)
- //IL_04f4: Expected O, but got Unknown
- //IL_05a8: Unknown result type (might be due to invalid IL or missing references)
- //IL_05af: Expected O, but got Unknown
- //IL_0550: Unknown result type (might be due to invalid IL or missing references)
- //IL_0557: Expected O, but got Unknown
- //IL_0702: Unknown result type (might be due to invalid IL or missing references)
- //IL_0709: Expected O, but got Unknown
- _paperwidth = 210f;
- _paperheight = 297f;
- _paperlandscape = false;
- _margin = new Margins(10, 10, 10, 10);
- _repeatheader = true;
- if (dgv == null || dgv.ColumnCount <= 0)
- {
- return;
- }
- progressexcel progressexcel = null;
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "请选择要导出PDF文件的路径和文件名";
- saveFileDialog.AddExtension = true;
- saveFileDialog.OverwritePrompt = false;
- saveFileDialog.DefaultExt = "PDF";
- saveFileDialog.Filter = "PDF文件(*.pdf)|*.pdf";
- saveFileDialog.FileName = filename;
- if (isshowprogress)
- {
- progressexcel = new progressexcel();
- progressexcel.Text = "导出PDF文件";
- progressexcel.TopMost = true;
- progressexcel.Visible = true;
- Module1.G_CancelExportDGVToExcel = false;
- progressexcel.Label1.Text = "正在做导出前的准备,请稍侯……";
- Application.DoEvents();
- }
- if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
- {
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- saveFileDialog.Dispose();
- return;
- }
- filename = saveFileDialog.FileName;
- saveFileDialog.Dispose();
- if (mydoc != null)
- {
- mydoc.Dispose();
- mydoc = null;
- }
- if (mywriter != null)
- {
- ((DocWriter)mywriter).Dispose();
- mywriter = null;
- }
- FontFactory.RegisterDirectories();
- mydoc = new Document();
- float totalwidth = 0f;
- int[] dGVColWidth = GetDGVColWidth(dgv, ref totalwidth);
- checked
- {
- float[] array = new float[dGVColWidth.Length - 1 + 1];
- int num = 0;
- int num2 = dGVColWidth.Length - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = Module1.G_ruler.ConvertToPoint(dGVColWidth[i], isx: true);
- }
- mywriter = PdfWriter.GetInstance(mydoc, (Stream)new FileStream(filename, FileMode.Create));
- mywriter.PageEvent = (IPdfPageEvent)(object)new testevent();
- mydoc.Open();
- if (PaperWidth == 0f)
- {
- totalwidth = Module1.G_ruler.ConvertToMM(totalwidth, isX: true) + (float)PaperMargin.Left + (float)PaperMargin.Right;
- totalwidth = Module1.G_ruler.ConvertToPointFromMM(totalwidth);
- Rectangle pageSize = new Rectangle(0f, 0f, totalwidth, Module1.G_ruler.ConvertToPointFromMM(PaperHeight));
- mydoc.SetPageSize(pageSize);
- 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));
- myratio = 1f;
- }
- else
- {
- totalwidth = Module1.G_ruler.ConvertToMM(totalwidth, isX: true);
- totalwidth = Module1.G_ruler.ConvertToPointFromMM(totalwidth);
- float num3 = Module1.G_ruler.ConvertToPointFromMM(PaperWidth);
- float num4 = Module1.G_ruler.ConvertToPointFromMM(PaperHeight);
- if (unchecked(PaperLandScape || num3 > num4))
- {
- Rectangle val = new Rectangle(0f, 0f, num3, num4);
- 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));
- mydoc.SetPageSize(val.Rotate());
- myratio = (num4 - Module1.G_ruler.ConvertToPointFromMM(_margin.Left) - Module1.G_ruler.ConvertToPointFromMM(_margin.Right)) / totalwidth;
- }
- else
- {
- Rectangle pageSize2 = new Rectangle(0f, 0f, num3, num4);
- 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));
- mydoc.SetPageSize(pageSize2);
- myratio = (num3 - Module1.G_ruler.ConvertToPointFromMM(_margin.Left) - Module1.G_ruler.ConvertToPointFromMM(_margin.Right)) / totalwidth;
- }
- }
- int num5 = 0;
- int num6 = dGVColWidth.Length - 1;
- for (int j = num5; j <= num6; j++)
- {
- array[j] *= myratio;
- }
- mydoc.NewPage();
- if (titlefont == null)
- {
- titlefont = new Font("黑体", 18f, FontStyle.Bold);
- }
- if (title != null && Operators.CompareString(title, "", TextCompare: false) != 0)
- {
- Paragraph val2 = new Paragraph(title, GetPDFFont(titlefont));
- val2.Alignment = 1;
- val2.SpacingAfter = 5f;
- val2.SpacingBefore = 0f;
- mydoc.Add((IElement)(object)val2);
- }
- if (Operators.CompareString(tableheader, "", TextCompare: false) != 0)
- {
- if (tableheaderfont == null)
- {
- tableheaderfont = new Font("宋体", 12f);
- }
- Paragraph val3 = new Paragraph(tableheader, GetPDFFont(tableheaderfont));
- val3.SpacingAfter = 5f;
- mydoc.Add((IElement)(object)val3);
- }
- Module1.G_CancelExportDGVToExcel = false;
- if (isshowprogress)
- {
- progressexcel.ProgressBar1.Maximum = dgv.RowCount;
- progressexcel.Label1.Text = "正导出表头,请稍侯……";
- Application.DoEvents();
- }
- PdfPTable val4 = new PdfPTable(GetDGVVisibleCols(dgv));
- val4.SetWidths(array);
- if (dgv.ColumnHeadersVisible)
- {
- AddHead(dgv, tv, val4);
- }
- int num7 = 0;
- int num8 = dgv.RowCount - 1;
- for (int k = num7; k <= num8; k++)
- {
- if (isshowprogress)
- {
- progressexcel.Label1.Text = "正在导出第〖" + Conversions.ToString(k + 1) + "〗条记录,共〖" + Conversions.ToString(dgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(k + 1) / (double)dgv.RowCount, "###.##%") + "〗";
- progressexcel.ProgressBar1.Value = k + 1;
- Application.DoEvents();
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- }
- if (dgv.Rows[k].Visible)
- {
- AddRow(dgv, k, val4);
- }
- }
- if (isshowprogress)
- {
- progressexcel.Close();
- progressexcel.Dispose();
- }
- val4.WidthPercentage = 100f;
- mydoc.Add((IElement)(object)val4);
- if (Operators.CompareString(tablefooter, "", TextCompare: false) != 0)
- {
- if (tablefooterfont == null)
- {
- tablefooterfont = new Font("宋体", 12f);
- }
- Paragraph val5 = new Paragraph(tablefooter, GetPDFFont(tablefooterfont));
- val5.SpacingAfter = 5f;
- mydoc.Add((IElement)(object)val5);
- }
- mydoc.Close();
- ((DocWriter)mywriter).Dispose();
- mywriter = null;
- mydoc.Dispose();
- mydoc = null;
- }
- }
- private int[] GetDGVColWidth(DataGridView dgv, ref float totalwidth)
- {
- totalwidth = 0f;
- if (dgv == null)
- {
- return null;
- }
- int columnCount = dgv.ColumnCount;
- checked
- {
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num = 0;
- int num2 = columnCount - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = i;
- array2[i] = dgv.Columns[i].DisplayIndex;
- }
- Array.Sort(array2, array);
- int dGVVisibleCols = GetDGVVisibleCols(dgv);
- if (dGVVisibleCols == 0)
- {
- return null;
- }
- int num3 = 0;
- int[] array3 = new int[dGVVisibleCols - 1 + 1];
- int num4 = 0;
- int num5 = columnCount - 1;
- for (int j = num4; j <= num5; j++)
- {
- if (dgv.Columns[array[j]].Visible)
- {
- array3[num3] = dgv.Columns[array[j]].Width;
- totalwidth += array3[num3];
- num3++;
- }
- }
- return array3;
- }
- }
- private int GetDGVVisibleCols(DataGridView dgv)
- {
- if (dgv == null)
- {
- return 0;
- }
- int num = 0;
- int num2 = 0;
- checked
- {
- int num3 = dgv.ColumnCount - 1;
- for (int i = num2; i <= num3; i++)
- {
- if (dgv.Columns[i].Visible)
- {
- num++;
- }
- }
- return num;
- }
- }
- private Font GetPDFFont(Font f)
- {
- if (f == null)
- {
- return null;
- }
- float size = f.Size;
- int num = 0;
- if (f.Bold)
- {
- num |= 1;
- }
- if (f.Italic)
- {
- num |= 2;
- }
- if (f.Underline)
- {
- num |= 4;
- }
- if (f.Strikeout)
- {
- num |= 8;
- }
- return GetPDFFont(f.Name, size, num);
- }
- private Font GetPDFFont(Font f, Color cc)
- {
- if (f == null)
- {
- return null;
- }
- float size = f.Size;
- int num = 0;
- if (f.Bold)
- {
- num |= 1;
- }
- if (f.Italic)
- {
- num |= 2;
- }
- if (f.Underline)
- {
- num |= 4;
- }
- if (f.Strikeout)
- {
- num |= 8;
- }
- return GetPDFFont(f.Name, size, num, GetPDFColor(cc));
- }
- private Font GetPDFFont(string fontname, float size, int style)
- {
- return FontFactory.GetFont(fontname, "Identity-H", false, size, style);
- }
- private Font GetPDFFont(string fontname, float size, int style, BaseColor cc)
- {
- return FontFactory.GetFont(fontname, "Identity-H", false, size, style, cc);
- }
- private void AddHead(DataGridView dgv, PdfPTable tb)
- {
- //IL_010e: Unknown result type (might be due to invalid IL or missing references)
- //IL_0115: Expected O, but got Unknown
- //IL_0117: Unknown result type (might be due to invalid IL or missing references)
- //IL_011e: Expected O, but got Unknown
- if (dgv == null || dgv.ColumnCount <= 0)
- {
- return;
- }
- int columnCount = dgv.ColumnCount;
- checked
- {
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num = 0;
- int num2 = columnCount - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = i;
- array2[i] = dgv.Columns[i].DisplayIndex;
- }
- Array.Sort(array2, array);
- int num3 = 0;
- int num4 = dgv.ColumnCount - 1;
- for (int j = num3; j <= num4; j++)
- {
- if (dgv.Columns[array[j]].Visible)
- {
- _ = dgv.Columns[array[j]].Width;
- Font dGVHeaderFont = Module1.GetDGVHeaderFont(dgv.Columns[array[j]]);
- Color dGVHeaderForeColor = Module1.GetDGVHeaderForeColor(dgv.Columns[array[j]]);
- Color dGVHeaderBackColor = Module1.GetDGVHeaderBackColor(dgv.Columns[array[j]]);
- Phrase val = new Phrase(dgv.Columns[array[j]].HeaderText, GetPDFFont(dGVHeaderFont, dGVHeaderForeColor));
- PdfPCell val2 = new PdfPCell(val);
- val2.Padding = 1f;
- ((Rectangle)val2).BorderColor = GetPDFColor(dgv.GridColor);
- val2.HorizontalAlignment = 1;
- val2.VerticalAlignment = 5;
- ((Rectangle)val2).BackgroundColor = GetPDFColor(dGVHeaderBackColor);
- if (myratio >= 1f)
- {
- val2.FixedHeight = Module1.G_ruler.ConvertToPoint(dgv.ColumnHeadersHeight, isx: false);
- }
- else
- {
- val2.MinimumHeight = Module1.G_ruler.ConvertToPoint(dgv.ColumnHeadersHeight, isx: false);
- }
- tb.AddCell(val2);
- }
- }
- }
- }
- private void AddHead(DataGridView dgv, TreeView tv, PdfPTable tb)
- {
- if (dgv == null)
- {
- return;
- }
- if (tv == null)
- {
- if (RepeatHeader)
- {
- tb.HeaderRows = 1;
- }
- AddHead(dgv, tb);
- return;
- }
- checked
- {
- float[] array = new float[dgv.ColumnCount - 1 + 1];
- int num = 0;
- int num2 = dgv.ColumnCount - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = dgv.Columns[i].Width;
- }
- mytree mytree = new mytree(tv, dgv, array);
- int leves = mytree.GetLeves();
- if (mytree.GetColumns() != dgv.ColumnCount)
- {
- if (RepeatHeader)
- {
- tb.HeaderRows = 1;
- }
- AddHead(dgv, tb);
- }
- else
- {
- if (RepeatHeader)
- {
- tb.HeaderRows = leves;
- }
- int num3 = 1;
- int num4 = leves;
- for (int j = num3; j <= num4; j++)
- {
- AddHead(dgv, mytree, j, tb);
- }
- }
- mytree.Dispose();
- }
- }
- private void AddHead(DataGridView dgv, mytree myheader, int curlevel, PdfPTable tb)
- {
- //IL_01b8: Unknown result type (might be due to invalid IL or missing references)
- //IL_01bf: Expected O, but got Unknown
- //IL_01c1: Unknown result type (might be due to invalid IL or missing references)
- //IL_01c8: Expected O, but got Unknown
- if (dgv == null || dgv.ColumnCount <= 0)
- {
- return;
- }
- int columnCount = dgv.ColumnCount;
- checked
- {
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num = 0;
- int num2 = columnCount - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = i;
- array2[i] = dgv.Columns[i].DisplayIndex;
- }
- Array.Sort(array2, array);
- int num3 = (int)Math.Round((double)dgv.ColumnHeadersHeight / (double)myheader.GetLeves());
- int num4 = 0;
- int num5 = dgv.ColumnCount - 1;
- bool flag = true;
- for (int j = num4; j <= num5; j++)
- {
- if (!dgv.Columns[array[j]].Visible)
- {
- continue;
- }
- int num6 = myheader.GetCellHeightLevels(array[j], curlevel, isTopNodeFill: false, out flag);
- int cellWidthCols = myheader.GetCellWidthCols(array[j], curlevel);
- if (unchecked(num6 == 0 || cellWidthCols == 0))
- {
- if (num6 != 0)
- {
- }
- continue;
- }
- string nodeText = myheader.GetNodeText(array[j], curlevel);
- Font font = myheader.GetNodeFont(array[j], curlevel);
- if (font == null)
- {
- font = Module1.GetDGVHeaderFont(dgv.Columns[array[j]]);
- }
- Color cc = myheader.GetNodeForeColor(array[j], curlevel);
- if (cc.IsEmpty)
- {
- cc = Module1.GetDGVHeaderForeColor(dgv.Columns[array[j]]);
- }
- Color cc2 = myheader.GetNodeBackColor(array[j], curlevel);
- if (cc2.IsEmpty)
- {
- cc2 = Module1.GetDGVHeaderBackColor(dgv.Columns[array[j]]);
- }
- if (unchecked(cellWidthCols == 1 && num6 == 1))
- {
- _ = dgv.Columns[array[j]].Width;
- }
- else
- {
- _ = (int)Math.Round(myheader.GetCellWidth2(array[j], curlevel));
- }
- Phrase val = new Phrase(nodeText, GetPDFFont(font, cc));
- PdfPCell val2 = new PdfPCell(val);
- if (cellWidthCols != 0)
- {
- val2.Colspan = cellWidthCols;
- }
- if (num6 != 0)
- {
- val2.Rowspan = num6;
- }
- val2.Padding = 1f;
- ((Rectangle)val2).BorderColor = GetPDFColor(dgv.GridColor);
- val2.HorizontalAlignment = 1;
- val2.VerticalAlignment = 5;
- ((Rectangle)val2).BackgroundColor = GetPDFColor(cc2);
- if (num6 <= 0)
- {
- num6 = 1;
- }
- if (myratio >= 1f)
- {
- val2.FixedHeight = Module1.G_ruler.ConvertToPoint(num3 * num6, isx: false);
- }
- else
- {
- val2.MinimumHeight = Module1.G_ruler.ConvertToPoint(num3 * num6, isx: false);
- }
- tb.AddCell(val2);
- }
- }
- }
- private void AddRow(DataGridView dgv, int rowindex, PdfPTable tb)
- {
- //IL_0750: Unknown result type (might be due to invalid IL or missing references)
- //IL_0757: Expected O, but got Unknown
- //IL_0759: Unknown result type (might be due to invalid IL or missing references)
- //IL_0760: Expected O, but got Unknown
- //IL_065a: Unknown result type (might be due to invalid IL or missing references)
- //IL_0661: Expected O, but got Unknown
- //IL_0602: Unknown result type (might be due to invalid IL or missing references)
- //IL_0609: Expected O, but got Unknown
- //IL_05f6: Unknown result type (might be due to invalid IL or missing references)
- //IL_05fd: Expected O, but got Unknown
- //IL_0536: Unknown result type (might be due to invalid IL or missing references)
- //IL_053d: Expected O, but got Unknown
- if (dgv == null || dgv.RowCount <= 0 || rowindex >= dgv.RowCount || dgv.ColumnCount <= 0 || !dgv.Rows[rowindex].Visible)
- {
- return;
- }
- int columnCount = dgv.ColumnCount;
- checked
- {
- int[] array = new int[columnCount - 1 + 1];
- int[] array2 = new int[columnCount - 1 + 1];
- int num = 0;
- int num2 = columnCount - 1;
- for (int i = num; i <= num2; i++)
- {
- array[i] = i;
- array2[i] = dgv.Columns[i].DisplayIndex;
- }
- Array.Sort(array2, array);
- int height = dgv.Rows[rowindex].Height;
- int num3 = 1;
- int num4 = 0;
- int num5 = 0;
- int num6 = 0;
- int num7 = 0;
- int num8 = 1;
- int num9 = 0;
- int num10 = dgv.ColumnCount - 1;
- for (int j = num9; j <= num10; j++)
- {
- if (!dgv.Columns[array[j]].Visible)
- {
- continue;
- }
- num8 = 1;
- num3 = 1;
- num6 = 0;
- num4 = 0;
- num7 = 0;
- num6 = 0;
- num4 = 0;
- num5 = 0;
- float myheight = height;
- string text = Conversions.ToString(dgv.Rows[rowindex].Cells[array[j]].Tag);
- int num11 = 0;
- int firstrow = 0;
- int firstr = 0;
- switch (text)
- {
- default:
- {
- int firstcolumn = 0;
- int firstc = 0;
- switch (text)
- {
- default:
- num8 = 1;
- num3 = 1;
- num11 = dgv.Columns[array[j]].Width;
- break;
- case "合并":
- case "左上合并":
- case "左下合并":
- case "右上合并":
- case "右下合并":
- case "上边合并":
- case "下边合并":
- case "左边合并":
- case "右边合并":
- {
- float mywidth = 0f;
- 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);
- num8 = num6 + num7 + 1;
- num3 = num4 + num5 + 1;
- num11 = (int)Math.Round(mywidth);
- unchecked
- {
- if (num3 > 1 && num8 > 1)
- {
- if (!(num4 == 0 && num6 == 0))
- {
- if (num6 != 0)
- {
- }
- continue;
- }
- }
- else if (num3 > 1)
- {
- if (num4 != 0)
- {
- continue;
- }
- }
- else if (num8 > 1 && num6 != 0)
- {
- continue;
- }
- break;
- }
- }
- case "水平合并":
- case "#水平合并#":
- case "左合并":
- case "右合并":
- num11 = (int)Math.Round(mymerge.GetHMergeWidth(dgv, array[j], rowindex, ref num8, ref num6, ref num7, ref firstcolumn, ref firstc, iscancheckheight: true));
- if (num8 > 1 && num6 != 0)
- {
- continue;
- }
- break;
- }
- break;
- }
- case "垂直合并":
- case "#垂直合并#":
- case "上合并":
- case "下合并":
- myheight = mymerge.GetVMergeHeight(dgv, array[j], rowindex, ref num3, ref num5, ref num4, ref firstrow, ref firstr);
- num11 = dgv.Columns[array[j]].Width;
- if (num3 > 1 && num4 != 0)
- {
- continue;
- }
- break;
- }
- Font dGVCellFont = Module1.GetDGVCellFont(dgv.Rows[rowindex].Cells[array[j]]);
- Color dGVCellBackColor = Module1.GetDGVCellBackColor(dgv.Rows[rowindex].Cells[array[j]]);
- Color dGVCellForeColor = Module1.GetDGVCellForeColor(dgv.Rows[rowindex].Cells[array[j]]);
- Margins dGVCellPadding = Module1.GetDGVCellPadding(dgv.Rows[rowindex].Cells[j]);
- PdfPCell val;
- if (dgv.Columns[array[j]] is DataGridViewImageColumn)
- {
- if (dgv.Rows[rowindex].Cells[array[j]].Value != null)
- {
- if (dgv.Rows[rowindex].Cells[array[j]].FormattedValue != null)
- {
- Image instance = Image.GetInstance((Image)dgv.Rows[rowindex].Cells[array[j]].FormattedValue, BaseColor.WHITE);
- DataGridViewImageCell dataGridViewImageCell = (DataGridViewImageCell)dgv.Rows[rowindex].Cells[array[j]];
- DataGridViewImageCellLayout imageLayout = dataGridViewImageCell.ImageLayout;
- if (imageLayout == DataGridViewImageCellLayout.Stretch)
- {
- if (myratio >= 1f)
- {
- instance.ScaleAbsolute(Module1.G_ruler.ConvertToPoint(num11 - 1, isx: true) * myratio, Module1.G_ruler.ConvertToPoint(myheight - 1f, isx: false));
- }
- else
- {
- instance.ScaleAbsoluteWidth(Module1.G_ruler.ConvertToPoint(num11, isx: true) * myratio);
- }
- }
- else
- {
- instance.ScaleToFit(Module1.G_ruler.ConvertToPoint(num11, isx: true) * myratio, Module1.G_ruler.ConvertToPoint(myheight, isx: false));
- }
- val = new PdfPCell(instance);
- val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
- val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
- val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
- val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
- if ((val.PaddingRight == 0f) & (val.PaddingLeft == 0f) & (val.PaddingBottom == 0f) & (val.PaddingTop == 0f))
- {
- val.Padding = 0.5f;
- }
- }
- else
- {
- val = new PdfPCell();
- }
- }
- else
- {
- val = new PdfPCell();
- }
- }
- else if (dgv.Columns[array[j]] is DataGridViewCheckBoxColumn)
- {
- Bitmap dGVCheckBoxCellImage = Module1.getDGVCheckBoxCellImage(dgv.Rows[rowindex].Cells[array[j]], dGVCellBackColor);
- Image instance2 = Image.GetInstance((Image)dGVCheckBoxCellImage, BaseColor.WHITE);
- val = new PdfPCell(instance2);
- val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
- val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
- val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
- val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
- if ((val.PaddingRight == 0f) & (val.PaddingLeft == 0f) & (val.PaddingBottom == 0f) & (val.PaddingTop == 0f))
- {
- val.Padding = 0.5f;
- }
- dGVCheckBoxCellImage.Dispose();
- }
- else
- {
- string text2 = Conversions.ToString(dgv.Rows[rowindex].Cells[array[j]].FormattedValue);
- Phrase val2 = new Phrase(text2, GetPDFFont(dGVCellFont, dGVCellForeColor));
- val = new PdfPCell(val2);
- val.PaddingLeft = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Left, isx: true);
- val.PaddingRight = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Right, isx: true);
- val.PaddingBottom = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Bottom, isx: false);
- val.PaddingTop = Module1.G_ruler.ConvertToPoint(dGVCellPadding.Top, isx: false);
- }
- ((Rectangle)val).BorderColor = GetPDFColor(dgv.GridColor);
- if (myratio >= 1f)
- {
- val.FixedHeight = Module1.G_ruler.ConvertToPoint(myheight, isx: false);
- }
- else
- {
- val.MinimumHeight = Module1.G_ruler.ConvertToPoint(myheight, isx: false);
- }
- ((Rectangle)val).BackgroundColor = GetPDFColor(dGVCellBackColor);
- val.Rowspan = num3;
- val.Colspan = num8;
- DataGridViewContentAlignment alignment = Module1.GetDGVCellStyle(dgv.Rows[rowindex].Cells[array[j]]).Alignment;
- if (alignment != DataGridViewContentAlignment.BottomCenter && alignment != DataGridViewContentAlignment.BottomLeft && alignment != DataGridViewContentAlignment.BottomRight)
- {
- if (alignment != DataGridViewContentAlignment.MiddleCenter && alignment != DataGridViewContentAlignment.MiddleLeft && alignment != DataGridViewContentAlignment.MiddleRight && alignment != 0)
- {
- val.VerticalAlignment = 4;
- }
- else
- {
- val.VerticalAlignment = 5;
- }
- }
- else
- {
- val.VerticalAlignment = 6;
- }
- DataGridViewContentAlignment alignment2 = Module1.GetDGVCellStyle(dgv.Rows[rowindex].Cells[array[j]]).Alignment;
- if (alignment2 != DataGridViewContentAlignment.BottomLeft && alignment2 != DataGridViewContentAlignment.MiddleLeft && alignment2 != DataGridViewContentAlignment.TopLeft && alignment2 != 0)
- {
- if (alignment2 != DataGridViewContentAlignment.BottomCenter && alignment2 != DataGridViewContentAlignment.MiddleCenter && alignment2 != DataGridViewContentAlignment.TopCenter)
- {
- val.HorizontalAlignment = 2;
- }
- else
- {
- val.HorizontalAlignment = 1;
- }
- }
- else
- {
- val.HorizontalAlignment = 0;
- }
- tb.AddCell(val);
- }
- }
- }
- private static BaseColor GetPDFColor(Color cc)
- {
- //IL_0001: Unknown result type (might be due to invalid IL or missing references)
- //IL_0007: Expected O, but got Unknown
- return new BaseColor(cc);
- }
- */
- }
- }
|