| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577 |
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Printing;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using Microsoft.VisualBasic;
- using Microsoft.VisualBasic.CompilerServices;
- using Microsoft.VisualBasic.FileIO;
- using NPOI.XSSF.UserModel;
- using NPOI.XSSF.Util;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using Curtain.Log;
- namespace Dongke.IBOSS.PRD.Framework.Controls
- {
- [StandardModule]
- internal sealed class DGVtoExcel
- {
- public static Font[] w_font = null;
- public static IFont[] w_ifont = null;
- public static Color[] w_color;
- public static short w_gridcolor;
- public static string[] w_Cellstyle = null;
- public static ICellStyle[] w_iCellstyle = null;
- public static short w_ZDYcolors = 0;
- public static bool DGVToExcel(DataGridView mydgv, string title, string FileName, string Tableheader, string TableFooter, string sheetname)
- {
- bool result = myDGVToExcel(mydgv, null, title, FileName, Tableheader, TableFooter, sheetname);
- GC.Collect();
- return result;
- }
- private static int GetPielxs(DataGridView dgv, int width)
- {
- Graphics graphics = dgv.CreateGraphics();
- float width2 = graphics.MeasureString("8", new Font("宋体", 8f)).Width;
- graphics.Dispose();
- int num = 8;
- try
- {
- num = checked((int)Math.Round(Math.Ceiling((float)width / width2)));
- if (num <= 0)
- {
- num = 1;
- }
- else if (num > 255)
- {
- num = 255;
- }
- }
- catch (Exception projectError)
- {
- Logger.Error(projectError, "DGVtoExcel-GetPielxs");
- MessageBox.Show(projectError.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
- ProjectData.SetProjectError(projectError);
- ProjectData.ClearProjectError();
- }
- return num;
- }
- public static bool myNewDGVtoExcel(DataGridView mydgv, string filename, string title, string TableHeader, string TableFooter, bool isopenfile)
- {
- //return false;
- checked
- {
- bool result;
- if (mydgv == null)
- {
- result = false;
- }
- else
- {
- progressexcel progressexcel2 = new progressexcel();
- progressexcel2.TopMost = true;
- progressexcel2.Visible = true;
- Module1.G_CancelExportDGVToExcel = false;
- try
- {
- progressexcel2.Label1.Text = "正在做导出前的准备,请稍侯……";
- 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)
- {
- progressexcel2.Close();
- progressexcel2.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++;
- }
- }
- if (Operators.CompareString(filename, "", TextCompare: false) == 0)
- {
- filename = MyProject.Computer.FileSystem.GetTempFileName();
- if (MyProject.Computer.FileSystem.FileExists(filename))
- {
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(filename);
- }
- catch (Exception projectError)
- {
- Logger.Error(projectError, "DGVtoExcel-myNewDGVtoExcel-projectError");
- ProjectData.SetProjectError(projectError);
- ProjectData.ClearProjectError();
- }
- }
- filename = MyProject.Computer.FileSystem.GetParentPath(filename) + "\\" + Strings.Replace(MyProject.Computer.FileSystem.GetName(filename), ".", "") + ".xls";
- }
- else if (MyProject.Computer.FileSystem.FileExists(filename))
- {
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(filename);
- }
- catch (Exception projectError2)
- {
- Logger.Error(projectError2, "DGVtoExcel-myNewDGVtoExcel-projectError2");
- ProjectData.SetProjectError(projectError2);
- ProjectData.ClearProjectError();
- }
- }
- newDGVtoExcel newDGVtoExcel2 = new newDGVtoExcel();
- if (newDGVtoExcel2.CreateFile(filename) != 0)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- Interaction.MsgBox("直接写Excel文件失败,导出数据到Excel文件失败", MsgBoxStyle.OkOnly, "提示信息");
- result = false;
- }
- else
- {
- int lrow;
- if (Operators.CompareString(title, "", TextCompare: false) == 0)
- {
- lrow = 1;
- }
- else
- {
- newDGVtoExcel newDGVtoExcel3 = newDGVtoExcel2;
- newDGVtoExcel.ValueTypes ValueType = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel.CellFont CellFontUsed = newDGVtoExcel.CellFont.xlsFont3;
- newDGVtoExcel.CellAlignment Alignment = newDGVtoExcel.CellAlignment.xlsLeftAlign;
- newDGVtoExcel.CellHiddenLocked HiddenLocked = newDGVtoExcel.CellHiddenLocked.xlsNormal;
- int lrow2 = 1;
- int lcol = 1;
- int CellFormat = 0;
- newDGVtoExcel3.WriteValue(ref ValueType, ref CellFontUsed, ref Alignment, ref HiddenLocked, ref lrow2, ref lcol, title, ref CellFormat);
- lrow = 2;
- }
- if (Operators.CompareString(TableHeader, "", TextCompare: false) != 0)
- {
- newDGVtoExcel newDGVtoExcel4 = newDGVtoExcel2;
- newDGVtoExcel.ValueTypes ValueType2 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel.CellFont CellFontUsed2 = newDGVtoExcel.CellFont.xlsFont2;
- newDGVtoExcel.CellAlignment Alignment2 = newDGVtoExcel.CellAlignment.xlsLeftAlign;
- newDGVtoExcel.CellHiddenLocked HiddenLocked2 = newDGVtoExcel.CellHiddenLocked.xlsNormal;
- int lrow3 = 2;
- int lcol2 = 1;
- int CellFormat2 = 0;
- newDGVtoExcel4.WriteValue(ref ValueType2, ref CellFontUsed2, ref Alignment2, ref HiddenLocked2, ref lrow3, ref lcol2, TableHeader, ref CellFormat2);
- lrow++;
- }
- if (mydgv.ColumnHeadersVisible)
- {
- int num7 = 0;
- int num8 = num - 1;
- for (int k = num7; k <= num8; k++)
- {
- newDGVtoExcel newDGVtoExcel5 = newDGVtoExcel2;
- newDGVtoExcel.ValueTypes ValueType3 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel.CellAlignment Alignment3 = (newDGVtoExcel.CellAlignment)122;
- int lcol3 = k + 1;
- newDGVtoExcel5.WriteValue(ref ValueType3, ref Alignment3, ref lrow, ref lcol3, mydgv.Columns[array3[k]].HeaderText);
- newDGVtoExcel newDGVtoExcel6 = newDGVtoExcel2;
- byte FirstColumn = (byte)(k + 1);
- byte LastColumn = (byte)(k + 1);
- short WidthValue = (short)GetPielxs(mydgv, mydgv.Columns[array3[k]].Width);
- newDGVtoExcel6.SetColumnWidth(ref FirstColumn, ref LastColumn, ref WidthValue);
- }
- lrow++;
- }
- progressexcel2.ProgressBar1.Maximum = mydgv.RowCount;
- int num9 = 0;
- int num10 = mydgv.RowCount - 1;
- for (int l = num9; l <= num10; l++)
- {
- progressexcel2.Label1.Text = "正在导出第〖" + Conversions.ToString(l + 1) + "〗条记录,共〖" + Conversions.ToString(mydgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(l + 1) / (double)mydgv.RowCount, "###.##%") + "〗";
- progressexcel2.ProgressBar1.Value = l + 1;
- Application.DoEvents();
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- int num11 = 0;
- int num12 = num - 1;
- for (num4 = num11; num4 <= num12; num4++)
- {
- newDGVtoExcel.ValueTypes ValueType4 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel.CellAlignment cellAlignment = (newDGVtoExcel.CellAlignment)120;
- if (mydgv.Columns[array3[num4]].ValueType != null)
- {
- switch (Strings.Left(mydgv.Columns[array3[num4]].ValueType.Name.ToUpper(), 3))
- {
- case "INT":
- ValueType4 = newDGVtoExcel.ValueTypes.xlsInteger;
- break;
- case "DEC":
- ValueType4 = newDGVtoExcel.ValueTypes.xlsNumber;
- break;
- }
- }
- DataGridViewContentAlignment alignment = mydgv.Columns[array3[num4]].DefaultCellStyle.Alignment;
- if (alignment != DataGridViewContentAlignment.BottomCenter && alignment != DataGridViewContentAlignment.MiddleCenter && alignment != DataGridViewContentAlignment.TopCenter)
- {
- switch (alignment)
- {
- case DataGridViewContentAlignment.TopRight:
- case DataGridViewContentAlignment.MiddleRight:
- case DataGridViewContentAlignment.BottomRight:
- cellAlignment |= newDGVtoExcel.CellAlignment.xlsRightAlign;
- break;
- case DataGridViewContentAlignment.TopLeft:
- case DataGridViewContentAlignment.MiddleLeft:
- case DataGridViewContentAlignment.BottomLeft:
- cellAlignment |= newDGVtoExcel.CellAlignment.xlsLeftAlign;
- break;
- }
- }
- else
- {
- cellAlignment |= newDGVtoExcel.CellAlignment.xlsCentreAlign;
- }
- if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewImageColumn", TextCompare: false) == 0)
- {
- newDGVtoExcel newDGVtoExcel7 = newDGVtoExcel2;
- int lrow4 = lrow + l;
- int lcol4 = num4 + 1;
- newDGVtoExcel7.WriteValue(ref ValueType4, ref lrow4, ref lcol4, "暂不支持图片列导出");
- }
- else if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
- {
- if (mydgv.Rows[l].Cells[array3[num4]].Value != null && mydgv.Rows[l].Cells[array3[num4]].Value != DBNull.Value)
- {
- if (Conversions.ToBoolean(mydgv.Rows[l].Cells[array3[num4]].FormattedValue))
- {
- ValueType4 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel newDGVtoExcel8 = newDGVtoExcel2;
- int lrow5 = lrow + l;
- int lcol5 = num4 + 1;
- newDGVtoExcel8.WriteValue(ref ValueType4, ref lrow5, ref lcol5, "√");
- }
- else
- {
- ValueType4 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel newDGVtoExcel9 = newDGVtoExcel2;
- int lrow6 = lrow + l;
- int lcol6 = num4 + 1;
- newDGVtoExcel9.WriteValue(ref ValueType4, ref lrow6, ref lcol6, "");
- }
- }
- }
- else
- {
- string text = Conversions.ToString(mydgv.Rows[l].Cells[array3[num4]].FormattedValue) ?? "";
- if (Versioned.IsNumeric(text) && Conversions.ToDouble(text) > 99999999999.0)
- {
- ValueType4 = newDGVtoExcel.ValueTypes.xlsText;
- }
- if (ValueType4 != newDGVtoExcel.ValueTypes.xlsText && !Versioned.IsNumeric(text))
- {
- ValueType4 = newDGVtoExcel.ValueTypes.xlsText;
- }
- newDGVtoExcel newDGVtoExcel10 = newDGVtoExcel2;
- int lrow7 = lrow + l;
- int lcol7 = num4 + 1;
- newDGVtoExcel10.WriteValue(ref ValueType4, ref lrow7, ref lcol7, text);
- }
- }
- }
- if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
- {
- newDGVtoExcel newDGVtoExcel11 = newDGVtoExcel2;
- newDGVtoExcel.ValueTypes ValueType5 = newDGVtoExcel.ValueTypes.xlsText;
- newDGVtoExcel.CellFont CellFontUsed3 = newDGVtoExcel.CellFont.xlsFont2;
- newDGVtoExcel.CellAlignment Alignment4 = newDGVtoExcel.CellAlignment.xlsLeftAlign;
- newDGVtoExcel.CellHiddenLocked HiddenLocked3 = newDGVtoExcel.CellHiddenLocked.xlsNormal;
- int lrow8 = mydgv.RowCount + lrow;
- int lcol8 = 1;
- int CellFormat3 = 0;
- newDGVtoExcel11.WriteValue(ref ValueType5, ref CellFontUsed3, ref Alignment4, ref HiddenLocked3, ref lrow8, ref lcol8, TableFooter, ref CellFormat3);
- }
- newDGVtoExcel2.CloseFile();
- progressexcel2.Close();
- progressexcel2.Dispose();
- if (isopenfile && Interaction.MsgBox("导出的Excel文件已保存到【" + filename + " 】\r\n您是否要立即打开这个文件以查看其内容?", MsgBoxStyle.YesNoCancel, "提示信息") == MsgBoxResult.Yes)
- {
- try
- {
- Process.Start(filename);
- }
- catch (Exception projectError3)
- {
- Logger.Error(projectError3, "DGVtoExcel-myNewDGVtoExcel-projectError3");
- ProjectData.SetProjectError(projectError3);
- ProjectData.ClearProjectError();
- }
- }
- result = true;
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, "DGVtoExcel-myNewDGVtoExcel-ex");
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- progressexcel2.Close();
- progressexcel2.Dispose();
- Interaction.MsgBox("导出Excel文件时失败,对程序员有用的错误提示是:" + ex2.Message, MsgBoxStyle.OkOnly, "提示信息");
- result = false;
- ProjectData.ClearProjectError();
- }
- }
- return result;
- }
- }
- public static bool DGVToExcel(DataGridView mydgv, TreeView columnheadertree, string title, string FileName, string Tableheader, string TableFooter)
- {
- bool result = myDGVToExcel(mydgv, columnheadertree, title, FileName, Tableheader, TableFooter, "");
- GC.Collect();
- return result;
- }
- private static bool myDGVToExcel(DataGridView mydgv, TreeView columnheadertree, string title, string FileName, string Tableheader, string TableFooter, string sheetname)
- {
- return myDGVToExcel(mydgv, columnheadertree, new Font("黑体", 18f, FontStyle.Bold), title, FileName, new Font("宋体", 12f), Tableheader, new Font("宋体", 12f), TableFooter, sheetname, isopenfile: true, isshowprogress: true, isTopNodeFill: false);
- }
- public static bool myDGVToExcel(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 progressexcel2 = null;
- Module1.G_CancelExportDGVToExcel = false;
- if (isshowprogress)
- {
- progressexcel2 = new progressexcel();
- progressexcel2.TopMost = true;
- progressexcel2.Visible = true;
- }
- Ruler ruler = new Ruler();
- try
- {
- if (isshowprogress)
- {
- progressexcel2.Label1.Text = "正在做导出前的准备,请稍侯……";
- progressexcel2.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)
- {
- progressexcel2.Close();
- progressexcel2.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++;
- }
- }
- w_ZDYcolors = 8;
- int endcol = num - 1;
- if (Operators.CompareString(FileName, "", TextCompare: false) == 0)
- {
- 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 = Conversions.ToString(DateAndTime.Today.Year) + "-" + Conversions.ToString(DateAndTime.Today.Month) + "-" + Conversions.ToString(DateAndTime.Today.Day);
- if (isshowprogress)
- {
- progressexcel2.Visible = false;
- }
- if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
- {
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- saveFileDialog.Dispose();
- return false;
- }
- FileName = saveFileDialog.FileName;
- saveFileDialog.Dispose();
- if (isshowprogress)
- {
- progressexcel2.Visible = true;
- }
- }
- w_font = null;
- w_ifont = null;
- w_Cellstyle = null;
- w_iCellstyle = null;
- w_color = null;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- if (isshowprogress)
- {
- progressexcel2.Visible = false;
- }
- switch (Interaction.MsgBox("选择的文件【" + FileName + "】已经存在,您确定要覆盖原来的文件吗?注意:覆盖原文件后,原文件中的全部数据都将丢失!\r\n单击[是]按钮,将覆盖原文件,单击[否]按钮,导出数据将以追加的形式添加到文件中,单击[取消]按钮将取消导出", MsgBoxStyle.YesNoCancel | MsgBoxStyle.Question, "提示信息"))
- {
- case MsgBoxResult.Cancel:
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- return false;
- case MsgBoxResult.Yes:
- try
- {
- MyProject.Computer.FileSystem.DeleteFile(FileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
- }
- catch (Exception projectError)
- {
- Logger.Error(projectError, "DGVtoExcel-myDGVtoExcel-projectError");
- ProjectData.SetProjectError(projectError);
- Interaction.MsgBox("数据导出失败,原因是文件【" + FileName + "】无法覆盖,可能该文件正在使用中,请关闭该文件后再试", MsgBoxStyle.Exclamation, "提示信息");
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- break;
- }
- if (isshowprogress)
- {
- progressexcel2.Visible = true;
- }
- }
- XSSFWorkbook hSSFWorkbook;
- if (MyProject.Computer.FileSystem.FileExists(FileName))
- {
- FileStream fileStream;
- try
- {
- fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
- }
- catch (Exception ex)
- {
- Logger.Error(ex, "DGVtoExcel-myDGVtoExcel-ex");
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- Interaction.MsgBox("数据导出失败,原因是打开Excel文件〖" + FileName + "〗失败," + ex2.Message, MsgBoxStyle.Exclamation, "提示信息");
- fileStream = null;
- result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- try
- {
- hSSFWorkbook = new XSSFWorkbook(fileStream);
- }
- catch (Exception projectError2)
- {
- Logger.Error(projectError2, "DGVtoExcel-myDGVtoExcel-projectError2");
- ProjectData.SetProjectError(projectError2);
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.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 XSSFWorkbook();
- }
- string excelNewSheetName = GetExcelNewSheetName(hSSFWorkbook, sheetname);
- XSSFSheet hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- int num7;
- if (Operators.CompareString(title, "", TextCompare: false) == 0)
- {
- num7 = 0;
- }
- else
- {
- SetTitle(hSSFWorkbook, hSSFSheet, 0, 0, 0, endcol, title, titlefont);
- num7 = 1;
- }
- if (Operators.CompareString(Tableheader, "", TextCompare: false) != 0)
- {
- SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, Tableheader, tableheaderfont);
- num7++;
- }
- w_gridcolor = 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++)
- {
- 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]]);
- SetCellStyle(hSSFWorkbook, hSSFSheet, num7, k, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont, dGVHeaderBackColor, dGVHeaderForeColor, needwrap: false);
- }
- }
- else
- {
- mytree mytree2 = new mytree(columnheadertree, mydgv);
- int leves = mytree2.GetLeves();
- if (mytree2.GetColumns() != mydgv.ColumnCount)
- {
- mytree2.Dispose();
- IRow row2 = hSSFSheet.CreateRow(num7);
- row2.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.ColumnHeadersHeight, isX: false)) * 72f;
- int num10 = 0;
- int num11 = num - 1;
- for (int l = num10; l <= num11; l++)
- {
- SetCellValue(hSSFSheet, num7, l, mydgv.Columns[array3[l]].HeaderText);
- Font dGVHeaderFont2 = Module1.GetDGVHeaderFont(mydgv.Columns[array3[l]]);
- Color dGVHeaderBackColor2 = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[l]]);
- Color dGVHeaderForeColor2 = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[l]]);
- SetCellStyle(hSSFWorkbook, hSSFSheet, num7, l, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, dGVHeaderFont2, dGVHeaderBackColor2, dGVHeaderForeColor2, needwrap: false);
- }
- }
- else
- {
- int num12 = 1;
- int num13 = leves;
- for (int m = num12; m <= num13; m++)
- {
- IRow row3 = hSSFSheet.CreateRow(num7 + m - 1);
- row3.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM((float)((double)mydgv.ColumnHeadersHeight / (double)leves), isX: false)) * 72f;
- }
- int num14 = 1;
- int num15 = leves;
- for (int n = num14; n <= num15; n++)
- {
- if (n != 1)
- {
- num7++;
- }
- int num16 = 0;
- int num17 = num - 1;
- bool flag = true;
- for (int num18 = num16; num18 <= num17; num18++)
- {
- string nodeText = mytree2.GetNodeText(array3[num18], n);
- int cellHeightLevels = mytree2.GetCellHeightLevels(array3[num18], n, isTopNodeFill, out flag);
- int cellWidthCols = mytree2.GetCellWidthCols(array3[num18], n);
- if (unchecked(cellHeightLevels == 0 || cellWidthCols == 0))
- {
- continue;
- }
- Font font = mytree2.GetNodeFont(array3[num18], n);
- if (font == null)
- {
- font = Module1.GetDGVHeaderFont(mydgv.Columns[array3[num18]]);
- }
- Color forcolor = mytree2.GetNodeForeColor(array3[num18], n);
- if (forcolor.IsEmpty)
- {
- forcolor = Module1.GetDGVHeaderForeColor(mydgv.Columns[array3[num18]]);
- }
- Color bkcolor = mytree2.GetNodeBackColor(array3[num18], n);
- if (bkcolor.IsEmpty)
- {
- bkcolor = Module1.GetDGVHeaderBackColor(mydgv.Columns[array3[num18]]);
- }
- if (unchecked(cellWidthCols == 1 && cellHeightLevels == 1))
- {
- int num19 = num7;
- if (!flag)
- {
- num19++;
- }
- SetCellValue(hSSFSheet, num19, num18, nodeText);
- SetCellStyle(hSSFWorkbook, hSSFSheet, num19, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- continue;
- }
- SetCellValue(hSSFSheet, num7, num18, nodeText);
- SetCellStyle(hSSFWorkbook, hSSFSheet, num7, num18, leftborder: true, rightborder: true, topborder: true, bottomborder: true, StringAlignment.Center, StringAlignment.Center, font, bkcolor, forcolor, needwrap: false);
- int num20 = num7;
- int num21 = num7 + 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 != num7 || num25 != num18))
- {
- SetCellBorder(hSSFWorkbook, hSSFSheet, num22, num25, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- SetMergeCell(hSSFSheet, num7, num18, num7 + cellHeightLevels - 1, num18 + cellWidthCols - 1);
- }
- }
- mytree2.Dispose();
- }
- }
- num7++;
- }
- int num26 = 1;
- int num27 = 0;
- int num28 = 0;
- int num29 = 0;
- int num30 = 0;
- int num31 = 1;
- int num32 = 0;
- int num33 = mydgv.RowCount - 1;
- for (int num34 = num32; num34 <= num33; num34++)
- {
- IRow row4 = hSSFSheet.CreateRow(num7 + num34);
- row4.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num34].Height, isX: false)) * 72f;
- if (!mydgv.Rows[num34].Visible)
- {
- row4.ZeroHeight = true;
- }
- if (num34 + num7 == 1048575)
- {
- break;
- }
- }
- int num35 = 0;
- int num36 = 0;
- if (isshowprogress)
- {
- progressexcel2.ProgressBar1.Maximum = mydgv.RowCount;
- }
- int num37 = 0;
- int num38 = mydgv.RowCount - 1;
- for (int num39 = num37; num39 <= num38; num39++)
- {
- bool flag2 = false;
- if (isshowprogress)
- {
- progressexcel2.Label1.Text = "正在导出第〖" + Conversions.ToString(num39 + 1) + "〗条记录,共〖" + Conversions.ToString(mydgv.RowCount) + "〗条记录,已完成〖" + Strings.Format((double)(num39 + 1) / (double)mydgv.RowCount, "###.##%") + "〗";
- progressexcel2.ProgressBar1.Value = num39 + 1;
- Application.DoEvents();
- }
- if (Module1.G_CancelExportDGVToExcel)
- {
- break;
- }
- int num40 = 0;
- int num41 = num - 1;
- for (num4 = num40; num4 <= num41; num4++)
- {
- int num42;
- if (mydgv.Columns[array3[num4]].ValueType == null)
- {
- num42 = 0;
- }
- else
- {
- switch (Strings.Left(mydgv.Columns[array3[num4]].ValueType.Name.ToUpper(), 3))
- {
- case "STR":
- num42 = 0;
- break;
- case "INT":
- case "DEC":
- num42 = 1;
- flag2 = false;
- break;
- case "DAT":
- num42 = 2;
- break;
- case "BOO":
- num42 = 3;
- flag2 = false;
- break;
- default:
- num42 = 0;
- break;
- }
- }
- num31 = 1;
- num26 = 1;
- num29 = 0;
- num27 = 0;
- num30 = 0;
- num29 = 0;
- num27 = 0;
- num28 = 0;
- string text = Conversions.ToString(mydgv.Rows[num39].Cells[array3[num4]].Tag);
- switch (text)
- {
- default:
- {
- float mywidth = 0f;
- float num43 = 0f;
- int firstcolumn = 0;
- int firstrow = 0;
- int firstc = 0;
- int firstr = 0;
- switch (text)
- {
- default:
- num31 = 1;
- num26 = 1;
- break;
- case "合并":
- case "左上合并":
- case "左下合并":
- case "右上合并":
- case "右下合并":
- case "上边合并":
- case "下边合并":
- case "左边合并":
- case "右边合并":
- firstrow = 0;
- mymerge.GetHVMergeWidthAndHeight(mydgv, array3[num4], 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(mydgv, array3[num4], 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(mydgv, array3[num4], num39, ref num26, ref num28, ref num27, ref firstrow, ref firstr);
- break;
- }
- }
- if (unchecked(num29 > 0 || num27 > 0))
- {
- continue;
- }
- if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewImageColumn", TextCompare: false) == 0)
- {
- if (mydgv.Rows[num39].Cells[array3[num4]].Value != null)
- {
- Image img = (Image)mydgv.Rows[num39].Cells[array3[num4]].FormattedValue;
- byte[] byteByImage = GetByteByImage(img);
- int pictureIndex = hSSFWorkbook.AddPicture(byteByImage, PictureType.JPEG);
- IDrawing drawing = hSSFSheet.CreateDrawingPatriarch();
- XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, num4, num7 + num39, num4 + 1, num7 + num39 + 1);
- drawing.CreatePicture(anchor, pictureIndex);
- }
- }
- else if (Operators.CompareString(Versioned.TypeName(mydgv.Columns[array3[num4]]), "DataGridViewCheckBoxColumn", TextCompare: false) == 0)
- {
- if (mydgv.Rows[num39].Cells[array3[num4]].Value != null && mydgv.Rows[num39].Cells[array3[num4]].Value != DBNull.Value && Conversions.ToBoolean(mydgv.Rows[num39].Cells[array3[num4]].FormattedValue))
- {
- SetCellValue(hSSFSheet, num7 + num39, num4, "√");
- }
- flag2 = false;
- }
- else
- {
- string text2 = Conversions.ToString(mydgv.Rows[num39].Cells[array3[num4]].FormattedValue) ?? "";
- flag2 = Strings.InStr(text2, "\r\n") > 0;
- switch (num42)
- {
- case 1:
- if (Versioned.IsNumeric(text2))
- {
- SetCellValue(hSSFSheet, num7 + num39, num4, Conversions.ToDouble(text2));
- }
- else
- {
- SetCellValue(hSSFSheet, num7 + num39, num4, text2);
- }
- break;
- case 2:
- SetCellValue(hSSFSheet, num7 + num39, num4, text2);
- break;
- default:
- SetCellValue(hSSFSheet, num7 + num39, num4, text2);
- break;
- }
- }
- DataGridViewCellStyle dGVCellStyle = Module1.GetDGVCellStyle(mydgv.Rows[num39].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 (!flag2 && dGVCellStyle.WrapMode == DataGridViewTriState.True)
- {
- flag2 = true;
- }
- Color dGVCellBackColor = Module1.GetDGVCellBackColor(mydgv.Rows[num39].Cells[array3[num4]]);
- Color dGVCellForeColor = Module1.GetDGVCellForeColor(mydgv.Rows[num39].Cells[array3[num4]]);
- SetCellStyle(hSSFWorkbook, hSSFSheet, num7 + num39, num4, leftborder: true, rightborder: true, topborder: true, bottomborder: true, halign, valign, Module1.GetDGVCellFont(mydgv.Rows[num39].Cells[array3[num4]]), dGVCellBackColor, dGVCellForeColor, flag2);
- if (unchecked(num31 == 1 && num26 == 1))
- {
- continue;
- }
- int num44 = num7 + num39;
- int num45 = num7 + num39 + num28;
- for (int num46 = num44; num46 <= num45; num46++)
- {
- int num47 = num4;
- int num48 = num4 + num30;
- for (int num49 = num47; num49 <= num48; num49++)
- {
- unchecked
- {
- if (num46 != checked(num7 + num39) || num49 != num4)
- {
- SetCellBorder(hSSFWorkbook, hSSFSheet, num46, num49, leftborder: true, rightborder: true, topborder: true, bottomborder: true);
- }
- }
- }
- }
- SetMergeCell(hSSFSheet, num7 + num39, num4, num7 + num39 + num28, num4 + num30);
- num4 += num30;
- }
- if (!((num39 + num7 == 1048575) & (num39 != mydgv.RowCount - 1)))
- {
- continue;
- }
- int num50 = 0;
- int num51 = num - 1;
- for (int num52 = num50; num52 <= num51; num52++)
- {
- hSSFSheet.SetColumnWidth(num52, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num52]].Width / 8.0) * 256.0));
- }
- num35++;
- excelNewSheetName = GetExcelNewSheetName(hSSFWorkbook, sheetname);
- hSSFSheet = (XSSFSheet)hSSFWorkbook.CreateSheet(excelNewSheetName);
- num36 = 1;
- int num53 = num39 + 1;
- int num54 = mydgv.RowCount - 1;
- for (num4 = num53; num4 <= num54; num4++)
- {
- IRow row5 = hSSFSheet.CreateRow(num36 - 1);
- if (!mydgv.Rows[num4].Visible)
- {
- row5.ZeroHeight = true;
- }
- row5.HeightInPoints = Module1.ConvertMmToInch(ruler.ConvertToMM(mydgv.Rows[num4].Height, isX: false)) * 72f;
- if (num36 == 1048576)
- {
- break;
- }
- num36++;
- }
- num7 = -num39 - 1;
- }
- num7 = ((num36 != 0) ? (num36 - 1) : (num7 + mydgv.RowCount));
- if (Operators.CompareString(TableFooter, "", TextCompare: false) != 0)
- {
- SetTitle(hSSFWorkbook, hSSFSheet, num7, num7, 0, endcol, TableFooter, tablefooterfont);
- }
- w_font = null;
- w_ifont = null;
- w_Cellstyle = null;
- w_iCellstyle = null;
- w_color = null;
- int num55 = 0;
- int num56 = num - 1;
- for (int num57 = num55; num57 <= num56; num57++)
- {
- hSSFSheet.SetColumnWidth(num57, (int)Math.Round(Math.Ceiling((double)mydgv.Columns[array3[num57]].Width / 8.0) * 256.0));
- }
- ruler?.Dispose();
- ruler = null;
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- hSSFWorkbook.SetActiveSheet(hSSFWorkbook.GetSheetIndex(excelNewSheetName));
- if (!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)
- {
- Logger.Error(projectError3, "DGVtoExcel-myDGVtoExcel-projectError3");
- ProjectData.SetProjectError(projectError3);
- try
- {
- Thread.Sleep(100);
- Process.Start(FileName);
- }
- catch (Exception projectError4)
- {
- Logger.Error(projectError4, "DGVtoExcel-myDGVtoExcel-projectError4");
- ProjectData.SetProjectError(projectError4);
- ProjectData.ClearProjectError();
- }
- ProjectData.ClearProjectError();
- }
- }
- result = true;
- }
- }
- }
- catch (Exception projectError5)
- {
- Logger.Error(projectError5, "DGVtoExcel-myDGVtoExcel-projectError5");
- ProjectData.SetProjectError(projectError5);
- if (isshowprogress)
- {
- progressexcel2.Close();
- progressexcel2.Dispose();
- }
- try
- {
- ruler?.Dispose();
- ruler = null;
- }
- catch (Exception projectError6)
- {
- Logger.Error(projectError6, "DGVtoExcel-myDGVtoExcel-projectError6");
- ProjectData.SetProjectError(projectError6);
- ProjectData.ClearProjectError();
- }
- if (Operators.CompareString(FileName, "", TextCompare: false) == 0)
- {
- result = myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: true);
- ProjectData.ClearProjectError();
- }
- else
- {
- result = myNewDGVtoExcel(mydgv, FileName, title, Tableheader, TableFooter, isopenfile: false);
- ProjectData.ClearProjectError();
- }
- }
- }
- return result;
- }
- }
- public static void SetTitle(XSSFWorkbook mybook, XSSFSheet mysheet, int startrow, int endrow, int startcol, int endcol, string titltext, Font titlefont)
- {
- IRow row = null;
- for (int i = startrow; i <= endrow; i = checked(i + 1))
- {
- row = mysheet.CreateRow(i);
- }
- ICell cell = row.CreateCell(0);
- cell.SetCellValue(titltext);
- SetCellStyle(mybook, mysheet, startrow, startcol, leftborder: false, rightborder: false, topborder: false, bottomborder: false, StringAlignment.Center, StringAlignment.Center, titlefont, Color.Transparent, Color.Black, needwrap: true);
- row.HeightInPoints = TestHeight(titltext, titlefont);
- SetMergeCell(mysheet, startrow, startcol, endrow, endcol);
- }
- public static float TestHeight(string tt, Font f)
- {
- PrintDocument printDocument = new PrintDocument();
- Graphics graphics = printDocument.PrinterSettings.CreateMeasurementGraphics();
- float height = graphics.MeasureString(tt, f).Height;
- graphics.Dispose();
- printDocument.Dispose();
- return height;
- }
- public static void SetMergeCell(XSSFSheet mysheet, int startrow, int startcol, int endrow, int endcol)
- {
- CellRangeAddress region = new CellRangeAddress(startrow, endrow, startcol, endcol);
- mysheet.AddMergedRegion(region);
- }
- public static void SetCellBorder(XSSFWorkbook mybook, XSSFSheet mysheet, int rowindex, int colindex, bool leftborder, bool rightborder, bool topborder, bool bottomborder)
- {
- NPOI.SS.UserModel.BorderStyle borderStyle = NPOI.SS.UserModel.BorderStyle.Thin;
- ICell cell = mysheet.GetRow(rowindex).GetCell(colindex);
- if (cell == null)
- {
- cell = mysheet.GetRow(rowindex).CreateCell(colindex);
- }
- ICellStyle cellStyle = mybook.CreateCellStyle();
- if (leftborder)
- {
- cellStyle.BorderLeft = borderStyle;
- cellStyle.LeftBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (rightborder)
- {
- cellStyle.BorderRight = borderStyle;
- cellStyle.RightBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (topborder)
- {
- cellStyle.BorderTop = borderStyle;
- cellStyle.TopBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (bottomborder)
- {
- cellStyle.BorderBottom = borderStyle;
- cellStyle.BottomBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
- }
- cell.CellStyle = cellStyle;
- }
- public static void SetCellValue(XSSFSheet mysheet, int rowindex, int colindex, string value)
- {
- mysheet.GetRow(rowindex).CreateCell(colindex).SetCellValue(value);
- }
- public static void SetCellValue(XSSFSheet mysheet, int rowindex, int colindex, double value)
- {
- mysheet.GetRow(rowindex).CreateCell(colindex).SetCellValue(value);
- }
- public static void SetCellValue(XSSFSheet mysheet, int rowindex, int colindex, DateTime value)
- {
- mysheet.GetRow(rowindex).CreateCell(colindex).SetCellValue(value);
- }
- public static void SetCellValue(XSSFSheet mysheet, int rowindex, int colindex, bool value)
- {
- mysheet.GetRow(rowindex).CreateCell(colindex).SetCellValue(value);
- }
- public static void SetCellStyle(XSSFWorkbook mybook, XSSFSheet mysheet, int rowindex, int colindex, bool leftborder, bool rightborder, bool topborder, bool bottomborder, StringAlignment halign, StringAlignment valign, Font myfont, Color bkcolor, Color forcolor, bool needwrap)
- {
- NPOI.SS.UserModel.BorderStyle borderStyle = NPOI.SS.UserModel.BorderStyle.Thin;
- ICell cell = mysheet.GetRow(rowindex).GetCell(colindex);
- if (cell == null)
- {
- cell = mysheet.GetRow(rowindex).CreateCell(colindex);
- }
- ICellStyle cellStyle = null;
- bool flag = false;
- int num = 0;
- checked
- {
- string cellStyleString;
- if (w_Cellstyle == null)
- {
- w_Cellstyle = new string[1];
- w_iCellstyle = new ICellStyle[1];
- num = 0;
- cellStyleString = GetCellStyleString(leftborder, rightborder, topborder, bottomborder, halign, valign, myfont, bkcolor, forcolor, needwrap);
- }
- else
- {
- cellStyleString = GetCellStyleString(leftborder, rightborder, topborder, bottomborder, halign, valign, myfont, bkcolor, forcolor, needwrap);
- int num2 = 0;
- int num3 = w_Cellstyle.Length - 1;
- int i;
- for (i = num2; i <= num3; i++)
- {
- if (Operators.CompareString(cellStyleString, w_Cellstyle[i], TextCompare: false) == 0)
- {
- flag = true;
- break;
- }
- }
- if (flag)
- {
- cellStyle = w_iCellstyle[i];
- }
- else
- {
- num = w_Cellstyle.Length;
- w_Cellstyle = (string[])Utils.CopyArray(w_Cellstyle, new string[num + 1]);
- w_iCellstyle = (ICellStyle[])Utils.CopyArray(w_iCellstyle, new ICellStyle[num + 1]);
- }
- }
- if (!flag)
- {
- cellStyle = mybook.CreateCellStyle();
- if (leftborder)
- {
- cellStyle.BorderLeft = borderStyle;
- cellStyle.LeftBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (rightborder)
- {
- cellStyle.BorderRight = borderStyle;
- cellStyle.RightBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (topborder)
- {
- cellStyle.BorderTop = borderStyle;
- cellStyle.TopBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.None;
- }
- if (bottomborder)
- {
- cellStyle.BorderBottom = borderStyle;
- cellStyle.BottomBorderColor = w_gridcolor;
- }
- else
- {
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
- }
- switch (halign)
- {
- case StringAlignment.Center:
- cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- break;
- case StringAlignment.Far:
- cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
- break;
- default:
- cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
- break;
- }
- switch (valign)
- {
- case StringAlignment.Near:
- cellStyle.VerticalAlignment = VerticalAlignment.Top;
- break;
- case StringAlignment.Far:
- cellStyle.VerticalAlignment = VerticalAlignment.Bottom;
- break;
- default:
- cellStyle.VerticalAlignment = VerticalAlignment.Center;
- break;
- }
- if (!bkcolor.IsEmpty && !(bkcolor.Equals(Color.Transparent) | bkcolor.Equals(Color.White)))
- {
- cellStyle.FillPattern = FillPattern.SolidForeground;
- //cellStyle.FillForegroundColor = GetXLColour(mybook, bkcolor);
- //((XSSFColor)cellStyle.FillForegroundColorColor).SetRgb(new byte[] { bkcolor.R, bkcolor.G, bkcolor.B });
- XSSFColor xssfColor = new XSSFColor(bkcolor);
- (cellStyle as XSSFCellStyle).SetFillForegroundColor(xssfColor);
- }
- if (needwrap)
- {
- cellStyle.WrapText = true;
- }
- else
- {
- cellStyle.WrapText = false;
- }
- IFont iFontByFont = GetIFontByFont(mybook, myfont, forcolor);
- if (!forcolor.IsEmpty && !(forcolor.Equals(Color.Transparent) | forcolor.Equals(Color.Black)))
- {
- //iFontByFont.Color = GetXLColour(mybook, forcolor);
- XSSFColor xssfColor = new XSSFColor(forcolor);
- (iFontByFont as XSSFFont).SetColor(xssfColor);
- }
- cellStyle.SetFont(iFontByFont);
- w_Cellstyle[num] = cellStyleString;
- w_iCellstyle[num] = cellStyle;
- }
- cell.CellStyle = cellStyle;
- }
- }
- public static byte[] GetByteByImage(Image img)
- {
- MemoryStream memoryStream = new MemoryStream();
- img.Save(memoryStream, ImageFormat.Jpeg);
- checked
- {
- byte[] array = new byte[(int)memoryStream.Length + 1];
- memoryStream.Position = 0L;
- memoryStream.Read(array, 0, (int)memoryStream.Length);
- memoryStream.Close();
- return array;
- }
- }
- private static IFont GetIFontByFont(XSSFWorkbook mybook, Font myfont, Color forecolor)
- {
- checked
- {
- int num;
- if (w_font == null)
- {
- w_font = new Font[1];
- w_ifont = new IFont[1];
- w_color = new Color[1];
- num = 0;
- }
- else
- {
- bool flag = false;
- int num2 = 0;
- int num3 = w_font.Length - 1;
- int i;
- for (i = num2; i <= num3; i++)
- {
- if (IsEqualFont(myfont, w_font[i]) & forecolor.Equals(w_color[i]))
- {
- flag = true;
- break;
- }
- }
- if (flag)
- {
- return w_ifont[i];
- }
- num = w_font.Length;
- w_font = (Font[])Utils.CopyArray(w_font, new Font[num + 1]);
- w_ifont = (IFont[])Utils.CopyArray(w_ifont, new IFont[num + 1]);
- w_color = (Color[])Utils.CopyArray(w_color, new Color[num + 1]);
- }
- IFont font = mybook.CreateFont();
- font.FontHeightInPoints = (short)Math.Round(myfont.SizeInPoints);
- font.FontName = myfont.Name;
- font.IsItalic = myfont.Italic;
- font.IsStrikeout = myfont.Strikeout;
- if (myfont.Underline)
- {
- font.Underline = FontUnderlineType.Single;
- }
- else
- {
- font.Underline = FontUnderlineType.None;
- }
- if (myfont.Bold)
- {
- font.Boldweight = 700;
- }
- else
- {
- font.Boldweight = 400;
- }
- w_font[num] = myfont;
- w_ifont[num] = font;
- w_color[num] = forecolor;
- return font;
- }
- }
- private static bool IsEqualFont(Font f1, Font f2)
- {
- FontConverter fontConverter = new FontConverter();
- string left = fontConverter.ConvertToString(f1);
- string right = fontConverter.ConvertToString(f2);
- return Operators.CompareString(left, right, TextCompare: false) == 0;
- }
- private static string GetCellStyleString(bool leftborder, bool rightborder, bool topborder, bool bottomborder, StringAlignment halign, StringAlignment valign, Font myfont, Color bkcolor, Color forcolor, bool needwrap)
- {
- string text = ((!leftborder) ? "0" : "1");
- text = ((!rightborder) ? (text + "0") : (text + "1"));
- text = ((!topborder) ? (text + "0") : (text + "1"));
- text = ((!bottomborder) ? (text + "0") : (text + "1"));
- switch (halign)
- {
- case StringAlignment.Near:
- text += "3";
- break;
- case StringAlignment.Center:
- text += "1";
- break;
- case StringAlignment.Far:
- text += "2";
- break;
- }
- switch (valign)
- {
- case StringAlignment.Near:
- text += "3";
- break;
- case StringAlignment.Center:
- text += "1";
- break;
- case StringAlignment.Far:
- text += "2";
- break;
- }
- text = ((!needwrap) ? (text + "2") : (text + "1"));
- text += Module1.ConvertFontToString(myfont);
- text += Module1.ConvertColorToString(bkcolor);
- return text + Module1.ConvertColorToString(forcolor);
- }
- public static short GetXLColour(XSSFWorkbook mybook, Color SystemColour)
- {
- XSSFColor hSSFColor = new XSSFColor(SystemColour);
- return hSSFColor.Indexed;
- /*XSSFPalette customPalette = mybook.GetCustomPalette();
- XSSFColor hSSFColor = customPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B);
- checked
- {
- if (Information.IsNothing(hSSFColor) || ((hSSFColor.Indexed >= w_ZDYcolors) & (hSSFColor.Indexed <= 64)))
- {
- if (w_ZDYcolors <= 64)
- {
- try
- {
- customPalette.SetColorAtIndex(w_ZDYcolors, SystemColour.R, SystemColour.G, SystemColour.B);
- short result = w_ZDYcolors;
- w_ZDYcolors++;
- return result;
- }
- catch (Exception projectError)
- {
- ProjectData.SetProjectError(projectError);
- hSSFColor = customPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B);
- short indexed = hSSFColor.Indexed;
- ProjectData.ClearProjectError();
- return indexed;
- }
- }
- hSSFColor = customPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B);
- return hSSFColor.Indexed;
- }
- return hSSFColor.Indexed;
- }*/
- }
- public static bool WriteToFile(XSSFWorkbook mybook, string filename)
- {
- FileStream fileStream = null;
- try
- {
- fileStream = new FileStream(filename, FileMode.Create);
- }
- catch (Exception ex)
- {
- Logger.Error(ex, "DGVtoExcel-WriteToFile-ex");
- ProjectData.SetProjectError(ex);
- Exception ex2 = ex;
- Interaction.MsgBox(ex2.Message, MsgBoxStyle.Exclamation, "导出Excel失败");
- bool result = false;
- ProjectData.ClearProjectError();
- return result;
- }
- mybook.Write(fileStream);
- fileStream.Close();
- return true;
- }
- public static string GetExcelNewSheetName(XSSFWorkbook mybook, string sheetname)
- {
- sheetname = Strings.Trim(sheetname);
- if (mybook == null)
- {
- return "";
- }
- int numberOfSheets = mybook.NumberOfSheets;
- if (Operators.CompareString(sheetname, "", TextCompare: false) == 0)
- {
- sheetname = "导出结果";
- }
- int num = 1;
- checked
- {
- string[] array = new string[numberOfSheets - 1 + 1];
- int num2 = 0;
- int num3 = numberOfSheets - 1;
- for (int i = num2; i <= num3; i++)
- {
- array[i] = mybook.GetSheetName(i);
- }
- bool flag = false;
- int num4 = 0;
- int num5 = numberOfSheets - 1;
- for (int j = num4; j <= num5; j++)
- {
- if (Operators.CompareString(array[j], sheetname, TextCompare: false) == 0)
- {
- flag = true;
- break;
- }
- }
- string text = sheetname;
- if (flag)
- {
- while (flag)
- {
- flag = false;
- sheetname = text + "-" + Conversions.ToString(num);
- num++;
- int num6 = 0;
- int num7 = numberOfSheets - 1;
- for (int k = num6; k <= num7; k++)
- {
- if (Operators.CompareString(array[k], sheetname, TextCompare: false) == 0)
- {
- flag = true;
- break;
- }
- }
- }
- }
- return sheetname;
- }
- }
- }
- }
|