ParticlesGridDevidePage.cs 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.SS.UserModel.Charts;
  4. using NPOI.SS.Util;
  5. using NPOI.XSSF.UserModel;
  6. using OTSIncAReportApp;
  7. using OTSIncAReportApp.DataOperation.DataAccess;
  8. using OTSIncAReportApp.DataOperation.Model;
  9. using OTSIncAReportApp.SysMgrTools;
  10. using OTSIncAReportGraph.Class;
  11. using OTSIncAReportGraph.OTSIncAReportGraphFuncation;
  12. using OTSIncAReportGrids.OTSIncAReportGridsFuncation;
  13. using OTSIncAReportMailInterface;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Data;
  18. using System.Drawing;
  19. using System.Drawing.Imaging;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Reflection;
  23. using System.Threading;
  24. using System.Windows.Forms;
  25. namespace OTSIncAReportGrids
  26. {
  27. public partial class ParticlesGridDevidePage : UserControl
  28. {
  29. #region 枚举定义
  30. /// <summary>
  31. /// 样品台X轴方向
  32. /// </summary>
  33. enum OTS_X_AXIS_DIRECTION
  34. {
  35. LEFT_TOWARD = 0,
  36. RIGHT_TOWARD = 1
  37. }
  38. /// <summary>
  39. /// 样品台Y轴方向
  40. /// </summary>
  41. enum OTS_Y_AXIS_DIRECTION
  42. {
  43. UP_TOWARD = 0,
  44. DOWN_TOWARD = 1
  45. }
  46. #endregion
  47. //设置模拟数据表
  48. public DataTable m_dt = new DataTable();
  49. //底层操作类
  50. OTSReportGridsFun m_OTSIncAReportGridsFun = null;
  51. //进度条窗体
  52. public Frm_UserProgress m_frm_userprogress;
  53. //移动SEM到指定位置发生线程
  54. private Thread m_mythread;
  55. //记录线程是否已经运行完成的状态
  56. private bool m_mythread_state = false;
  57. //导出图像文件的路径
  58. private string m_imagefilepath = "";
  59. //显示元素列表
  60. public List<string> m_list_elementscolname = null;
  61. //单个数据源所拥有的列数,这里保存下来,供导出模块使用
  62. public int m_oneresult_columncount = 0;
  63. OTSImageDisHelp m_ReportFun = null;
  64. private OTSImageDisHelp ReportFun { get => m_ReportFun; set => m_ReportFun = value; }
  65. //多个数据源,每个数据源拥有的列数,这里保存下来,供分组使用
  66. public List<int> m_list_oneresult_columncount;
  67. //国际化
  68. Language lan;
  69. Hashtable table;
  70. //测量结果
  71. ResultFile result = null;
  72. string str_SysType = "";//当前启动模式(夹杂物、清洁度)
  73. frmReportConditionChoose m_condition;
  74. FieldData fieldData;
  75. DataTable particlesAll;
  76. DataTable particles;
  77. ParticleData Particledata;
  78. UserLibraryData userLibrary;
  79. DataTable userLibraryData;
  80. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  81. #region 分页器相关
  82. #region 分页字段和属性
  83. string condition = "";
  84. public string FileName = null;
  85. private int pageIndex = 1;
  86. /// <summary>
  87. /// 当前页面
  88. /// </summary>
  89. public virtual int PageIndex
  90. {
  91. get { return pageIndex; }
  92. set { pageIndex = value; }
  93. }
  94. private int pageSize = 20;
  95. /// <summary>
  96. /// 每页记录数
  97. /// </summary>
  98. public virtual int PageSize
  99. {
  100. get { return pageSize; }
  101. set { pageSize = value; }
  102. }
  103. //string OrderFunction = "fieldid,particleid";
  104. private int recordCount = 0;
  105. /// <summary>
  106. /// 总记录数
  107. /// </summary>
  108. public virtual int RecordCount
  109. {
  110. get { return recordCount; }
  111. set { recordCount = value; }
  112. }
  113. private int pageCount = 0;
  114. /// <summary>
  115. /// 总页数
  116. /// </summary>
  117. public int PageCount
  118. {
  119. get
  120. {
  121. pageCount = GetPageCount();
  122. return pageCount;
  123. }
  124. }
  125. /// <summary>
  126. /// 计算总页数
  127. /// </summary>
  128. /// <returns></returns>
  129. private int GetPageCount()
  130. {
  131. if (PageSize == 0)
  132. {
  133. return 0;
  134. }
  135. int pageCount = RecordCount / PageSize;
  136. if (RecordCount % PageSize == 0)
  137. {
  138. pageCount = RecordCount / PageSize;
  139. }
  140. else
  141. {
  142. pageCount = RecordCount / PageSize + 1;
  143. }
  144. return pageCount;
  145. }
  146. #endregion
  147. frmReportApp m_ReportApp;
  148. ResultFile resultFile = null;
  149. #endregion
  150. public ParticlesGridDevidePage(frmReportApp ReportApp)
  151. {
  152. m_ReportApp = ReportApp;
  153. m_condition = ReportApp.m_conditionChoose;
  154. resultFile = ReportApp.m_rstDataMgr.CurResultFile;
  155. ReportFun = new OTSImageDisHelp(resultFile);
  156. string sou = m_condition.m_CurrentConditions[OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
  157. if (sou.Contains("+"))
  158. {
  159. result = m_ReportApp.m_rstDataMgr.ResultFilesList[0];
  160. } else
  161. {
  162. for (int i = 0; i < m_ReportApp.m_rstDataMgr.ResultFilesList.Count; i++)
  163. {
  164. if (sou == m_ReportApp.m_rstDataMgr.ResultFilesList[i].FileName.ToString())
  165. {
  166. result = m_ReportApp.m_rstDataMgr.ResultFilesList[i];
  167. }
  168. }
  169. }
  170. //result = m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()];
  171. //设置窗体的双缓冲,以保证大数据时拖动不卡
  172. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
  173. this.UpdateStyles();
  174. InitializeComponent();
  175. //利用反射设置DataGridView的双缓冲
  176. Type dgvType = this.dgV_ParticlesDevidePage.GetType();
  177. PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
  178. pi.SetValue(this.dgV_ParticlesDevidePage, true, null);
  179. }
  180. bool Init()
  181. {
  182. try
  183. {
  184. lan = new Language(this);
  185. table = lan.GetNameTable(this.Name);
  186. m_mythread_state = false;
  187. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  188. //m_frm_userprogress = new Frm_UserProgress();
  189. fieldData = new FieldData(result.FilePath);
  190. Particledata = new ParticleData(result.FilePath);
  191. str_SysType = ((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["Sample"])["Members"])["MsrParams"])["SysType"].ToString();
  192. if (str_SysType == "0:IncA")
  193. {
  194. userLibraryData = null;
  195. }
  196. else
  197. {
  198. string str_libraryName = ((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["Sample"])["Members"])["MsrParams"])["STDName"].ToString();
  199. userLibrary = new UserLibraryData(str_libraryName);
  200. if (userLibrary.GetSqlHelper() != null)
  201. {
  202. userLibraryData = userLibrary.GetSubAttributeFromDatabase();
  203. }
  204. else
  205. {
  206. userLibraryData = null;
  207. }
  208. }
  209. //初始化底层操作类
  210. m_OTSIncAReportGridsFun = new OTSReportGridsFun(m_ReportApp, this);
  211. return true;
  212. }
  213. catch (Exception ex)
  214. {
  215. log.Error(ex.ToString());
  216. return false;
  217. }
  218. }
  219. private void ParticlesGridDevidePage_Load(object sender, EventArgs e)
  220. {
  221. UpdateLoad();
  222. }
  223. private void UpdateLoad()
  224. {
  225. if (!Init())
  226. {
  227. return;
  228. }
  229. ProgressStart();//进度条关闭通过cbB_PageSize.SelectedIndex事件
  230. if (!UpdateTable())
  231. {
  232. return;
  233. }
  234. cbB_PageSize.Items.Add("20");
  235. cbB_PageSize.Items.Add("100");
  236. cbB_PageSize.Items.Add("500");
  237. cbB_PageSize.Items.Add("1000");
  238. cbB_PageSize.Items.Add("5000");
  239. cbB_PageSize.Items.Add("All");
  240. cbB_PageSize.SelectedIndex = 0;
  241. }
  242. #region 自定义方法
  243. bool UpdateTable()
  244. {
  245. int sel = m_ReportApp.m_conditionChoose.m_SourceGridData.GetComboDownListIndexByItemName(OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
  246. if (sel == 1)
  247. {
  248. List<OTSIncAReportApp.DataOperation.Model.Particle> selectParticles = m_ReportApp.GetSelectedParticles();
  249. DataTable dtUelect = new DataTable();
  250. dtUelect.Columns.Add("fieldid");
  251. dtUelect.Columns.Add("particleid");
  252. dtUelect.Columns.Add("AveGray");
  253. dtUelect.Columns.Add("RectLeft");
  254. dtUelect.Columns.Add("RectTop");
  255. dtUelect.Columns.Add("RectWidth");
  256. dtUelect.Columns.Add("RectHeight");
  257. dtUelect.Columns.Add("Area");
  258. dtUelect.Columns.Add("PosX");
  259. dtUelect.Columns.Add("PosY");
  260. dtUelect.Columns.Add("TypeId");
  261. //dtUelect.Columns.Add("ElementNum");
  262. dtUelect.Columns.Add("SegmentNum");
  263. dtUelect.Columns.Add("SEMPosX");
  264. dtUelect.Columns.Add("SEMPosY");
  265. dtUelect.Columns.Add("XrayId");
  266. dtUelect.Columns.Add("DMAX");
  267. dtUelect.Columns.Add("DMIN");
  268. dtUelect.Columns.Add("DPERP");
  269. dtUelect.Columns.Add("PERIMETER");
  270. dtUelect.Columns.Add("ORIENTATION");
  271. dtUelect.Columns.Add("DINSCR");
  272. dtUelect.Columns.Add("DMEAN");
  273. dtUelect.Columns.Add("DELONG");
  274. dtUelect.Columns.Add("DFERET");
  275. dtUelect.Columns.Add("TypeName");
  276. dtUelect.Columns.Add("TypeColor");
  277. dtUelect.Columns.Add("SubParticles");
  278. dtUelect.Columns.Add("Element");
  279. dtUelect.Columns.Add("Hardness");
  280. dtUelect.Columns.Add("Density");
  281. dtUelect.Columns.Add("Electrical_conductivity");
  282. for (int i = 0; i < selectParticles.Count; i++)
  283. {
  284. dtUelect.Rows.Add(selectParticles[i].FieldId, selectParticles[i].ParticleId, selectParticles[i].AveGray, selectParticles[i].RectLeft, selectParticles[i].RectTop, selectParticles[i].RectWidth, selectParticles[i].RectHeight, selectParticles[i].Area, selectParticles[i].PosX, selectParticles[i].PosX, selectParticles[i].TypeId, /*selectParticles[i].ElementNum,*/ selectParticles[i].SegmentNum, selectParticles[i].SEMPosX, selectParticles[i].SEMPosY, selectParticles[i].ParticleId, selectParticles[i].DMAX, selectParticles[i].DMIN, selectParticles[i].DPERP, selectParticles[i].PERIMETER, selectParticles[i].ORIENTATION, selectParticles[i].DINSCR, selectParticles[i].DMEAN, selectParticles[i].DELONG, selectParticles[i].DFERET, selectParticles[i].TypeName, selectParticles[i].TypeColor, "", "", "", "", "");
  285. }
  286. if (userLibraryData != null)
  287. {
  288. for (int i = 0; i < dtUelect.Rows.Count; i++)
  289. {
  290. DataRow[] dr = userLibraryData.Select("STDId=" + dtUelect.Rows[i]["TypeId"].ToString());
  291. if (dr.Length > 0)
  292. {
  293. dtUelect.Rows[i]["Hardness"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Hardness"].ToString(), @"[^\d.\d]", "");
  294. dtUelect.Rows[i]["Density"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Density"].ToString(), @"[^\d.\d]", "");
  295. dtUelect.Rows[i]["Electrical_conductivity"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Electrical_conductivity"].ToString(), @"[^\d.\d]", "");
  296. }
  297. else
  298. {
  299. dtUelect.Rows[i]["Hardness"] = "";
  300. dtUelect.Rows[i]["Density"] = "";
  301. dtUelect.Rows[i]["Electrical_conductivity"] = "";
  302. }
  303. }
  304. }
  305. else
  306. {
  307. for (int i = 0; i < dtUelect.Rows.Count; i++)
  308. {
  309. dtUelect.Rows[i]["Hardness"] = "";
  310. dtUelect.Rows[i]["Density"] = "";
  311. dtUelect.Rows[i]["Electrical_conductivity"] = "";
  312. }
  313. }
  314. particlesAll = new DataTable();
  315. particlesAll = dtUelect.Copy();
  316. btn_Sel.Enabled = false;
  317. }
  318. else
  319. {
  320. //多次测试发现 读取数据库时间耗时长,但读取条数目对读取数据库时长影响不大,故此处直接读取所有数据库信息,以取代多次连接数据库方式,优化分页速度
  321. particlesAll = new DataTable();
  322. particlesAll = Particledata.GetInfoForPartucleDevidePage2(condition);
  323. particlesAll.Columns.Add("Hardness");
  324. particlesAll.Columns.Add("Density");
  325. particlesAll.Columns.Add("Electrical_conductivity");
  326. if (userLibraryData != null)
  327. {
  328. for (int i = 0; i < particlesAll.Rows.Count; i++)
  329. {
  330. DataRow[] dr = userLibraryData.Select("STDId=" + particlesAll.Rows[i]["TypeId"].ToString());
  331. if (dr.Length > 0)
  332. {
  333. particlesAll.Rows[i]["Hardness"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Hardness"].ToString(), @"[^\d.\d]", "");
  334. particlesAll.Rows[i]["Density"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Density"].ToString(), @"[^\d.\d]", "");
  335. particlesAll.Rows[i]["Electrical_conductivity"] = System.Text.RegularExpressions.Regex.Replace(dr[0]["Electrical_conductivity"].ToString(), @"[^\d.\d]", "");
  336. }
  337. else
  338. {
  339. particlesAll.Rows[i]["Hardness"] = "";
  340. particlesAll.Rows[i]["Density"] = "";
  341. particlesAll.Rows[i]["Electrical_conductivity"] = "";
  342. }
  343. }
  344. }
  345. else
  346. {
  347. for (int i = 0; i < particlesAll.Rows.Count; i++)
  348. {
  349. particlesAll.Rows[i]["Hardness"] = "";
  350. particlesAll.Rows[i]["Density"] = "";
  351. particlesAll.Rows[i]["Electrical_conductivity"] = "";
  352. }
  353. }
  354. if (sel == 0)
  355. {
  356. string str = "TypeId <> -1 and TypeId <> 9 and TypeId <> 0";
  357. particlesAll = SreeenDataTable(particlesAll, str);
  358. }
  359. if (sel == 2)
  360. {
  361. string str = "TypeId = " + "-1 or TypeId =" + "9 or TypeId =" + "0";
  362. particlesAll = SreeenDataTable(particlesAll, str);
  363. }
  364. btn_Sel.Enabled = true;
  365. }
  366. if (particlesAll == null)
  367. {
  368. log.Error("There is an exception in the data of the database!");
  369. #region 加载进度条进度部份结束
  370. //加载完成设置鼠标为默认
  371. this.Cursor = Cursors.Default;
  372. //加载完成,关闭进度条
  373. m_frm_userprogress.Close();
  374. #endregion
  375. return false;
  376. }
  377. DataTable elementchemistry = Particledata.GetElementChemistry();
  378. for (int i = 0; i < particlesAll.Rows.Count; i++)
  379. {
  380. string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
  381. DataRow[] drs = elementchemistry.Select(str);
  382. string ConcatenatedString = "";
  383. for (int j = 0; j < drs.Length; j++)
  384. {
  385. ConcatenatedString += drs[j]["name"] + "-" + drs[j]["Percentage"] + ';';
  386. }
  387. particlesAll.Rows[i]["Element"] = ConcatenatedString;
  388. }
  389. return true;
  390. }
  391. /// <summary>
  392. /// DataRow转换为DataTable
  393. /// </summary>
  394. /// <param name="dt"></param>
  395. /// <param name="strWhere">筛选的条件</param>
  396. /// <returns></returns>
  397. public DataTable SreeenDataTable(DataTable dt, string strWhere)
  398. {
  399. if (dt.Rows.Count <= 0) return dt; //当数据为空时返回
  400. DataTable dtNew = dt.Clone(); //复制数据源的表结构
  401. DataRow[] dr = dt.Select(strWhere); //strWhere条件筛选出需要的数据!
  402. for (int i = 0; i < dr.Length; i++)
  403. {
  404. dtNew.Rows.Add(dr[i].ItemArray); // 将DataRow添加到DataTable中
  405. }
  406. return dtNew;
  407. }
  408. /// <summary>
  409. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  410. /// </summary>
  411. /// <param name="in_cotsparticleclr"></param>
  412. /// <returns></returns>
  413. public Bitmap GetBitmapByParticle(string fieldImage, Rectangle offset_rect)
  414. {
  415. //然后将取出的数据,转换成Bitmap对象
  416. Bitmap ls_bt = DrawFunction.ReadImageFile(fieldImage);
  417. //为了能把整个颗粒显示完整
  418. offset_rect.X = offset_rect.X - 5;
  419. offset_rect.Y = offset_rect.Y - 5;
  420. offset_rect.Width = offset_rect.Width + 10;
  421. offset_rect.Height = offset_rect.Height + 10;
  422. //防止计算偏差后,有坐标溢出现象
  423. if (offset_rect.X < 0)
  424. offset_rect.X = 0;
  425. if (offset_rect.Y < 0)
  426. offset_rect.Y = 0;
  427. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  428. {
  429. offset_rect.Width = ls_bt.Width - offset_rect.X;
  430. }
  431. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  432. {
  433. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  434. }
  435. //
  436. Bitmap new_ret_bp;
  437. //防止为0后面计算出错
  438. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  439. {
  440. //最后通过list_showsegment组建成新的图片,进行返回
  441. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  442. }
  443. else
  444. {
  445. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  446. }
  447. return new_ret_bp;
  448. }
  449. public Bitmap GetBitmapForBig(string sub, ParticleData fielddata, double xs)
  450. {
  451. string vs = "," + sub.Replace(':', '-') + ",";
  452. DataTable dataTable = fielddata.GetParticleAll(vs);
  453. if (dataTable.Rows.Count == 0)
  454. {
  455. return null;
  456. }
  457. string path = result.FilePath;
  458. //内接矩形
  459. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  460. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  461. double min_Y = max_Y;
  462. double min_X = max_X;
  463. //拼接field矩形
  464. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  465. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  466. double MIN_Y = MAX_Y;
  467. double MIN_X = MAX_X;
  468. foreach (DataRow item in dataTable.Rows)
  469. {
  470. //颗粒外接矩形
  471. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  472. if (lefttopX < min_X)
  473. {
  474. min_X = lefttopX;
  475. }
  476. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  477. {
  478. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  479. }
  480. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  481. if (max_Y < lrfttopY)
  482. {
  483. max_Y = lrfttopY;
  484. }
  485. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  486. {
  487. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  488. }
  489. //画布
  490. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  491. if (lefttopXH > MAX_X)
  492. {
  493. MAX_X = lefttopXH;
  494. }
  495. if (lefttopXH < MIN_X)
  496. {
  497. MIN_X = lefttopXH;
  498. }
  499. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  500. if (MAX_Y < lrfttopYH)
  501. {
  502. MAX_Y = lrfttopYH;
  503. }
  504. if (MIN_Y > lrfttopYH)
  505. {
  506. MIN_Y = lrfttopYH;
  507. }
  508. }
  509. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + 1024;
  510. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + 768;
  511. //构造最终的图片白板
  512. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  513. Graphics graph = Graphics.FromImage(tableChartImage);
  514. //初始化这个大图
  515. graph.DrawImage(tableChartImage, 0, 0);
  516. int width = Convert.ToInt32(max_X - min_X);
  517. int height = Convert.ToInt32(max_Y - min_Y);
  518. int X = Convert.ToInt32(min_X - MIN_X);
  519. int Y = Convert.ToInt32(MAX_Y - max_Y);
  520. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  521. foreach (DataRow item in dataTable.Rows)
  522. {
  523. string filePath = path + "\\FIELD_FILES\\";
  524. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  525. //然后将取出的数据,转换成Bitmap对象
  526. Bitmap ls_bt = DrawFunction.ReadImageFile(imagePath);
  527. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  528. int y = System.Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MAX_Y));
  529. try
  530. {
  531. graph.DrawImage(ls_bt, x, y);
  532. }
  533. catch /*(Exception e)*/
  534. {
  535. throw;
  536. }
  537. }
  538. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  539. return bmap;
  540. }
  541. private void BindDataGridView()
  542. {
  543. dgV_ParticlesDevidePage.Visible = false;
  544. if (m_frm_userprogress.IsDisposed)
  545. {
  546. ProgressStart();
  547. }
  548. dgV_ParticlesDevidePage.Rows.Clear();
  549. dgV_ParticlesDevidePage.Columns.Clear();
  550. //从报告xml文件中加载,显示计算列,显示元素信息
  551. string str_DefaultComputedColName = "", str_ElementsColName = "";
  552. DataSet ds = OTSIncAReportApp.DataOperation.DataAccess.XMLoperate.GetXmlData(Application.StartupPath + m_ReportApp.m_OTSReportMgrParamFile, "XMLData");
  553. DataTable dt = ds.Tables["Member"];
  554. foreach (DataRow element in dt.Rows)
  555. {
  556. string RegName = element["RegName"].ToString();
  557. if (RegName == "DefaultComputedColName")
  558. {
  559. str_DefaultComputedColName = element["strValue"].ToString();
  560. }
  561. if (RegName == "ElementsColName")
  562. {
  563. str_ElementsColName = element["strValue"].ToString();
  564. }
  565. }
  566. //获取需要显示的计算列
  567. string[] strs = str_DefaultComputedColName.Split(',');
  568. //列名
  569. Dictionary<string, string> keyValues = new Dictionary<string, string>() { };
  570. keyValues.Add("rowid", table["str4"].ToString());
  571. keyValues.Add("TypeName", table["str6"].ToString());
  572. keyValues.Add("ParticleImage", table["str5"].ToString());
  573. keyValues.Add("FieldID", "FieldID");
  574. keyValues.Add("SEMPosX", "SEMPosX");
  575. keyValues.Add("SEMPosY", "SEMPosY");
  576. for (int i = 0; i < strs.Count(); i++)
  577. {
  578. if (strs[i] == "Area")
  579. {
  580. keyValues.Add("Area", table["str21"].ToString());
  581. }
  582. if (strs[i] == "EquivalentCircleDiameter")
  583. {
  584. keyValues.Add("Equivalent", table["str22"].ToString());
  585. }
  586. if (strs[i] == "MaxDiameter")
  587. {
  588. keyValues.Add("DMAX", table["str23"].ToString());
  589. }
  590. if (strs[i] == "MinDiameter")
  591. {
  592. keyValues.Add("DMIN", table["str24"].ToString());
  593. }
  594. if (strs[i] == "DiameterRatio")
  595. {
  596. keyValues.Add("DiameterRatio", table["str25"].ToString());
  597. }
  598. if (strs[i] == "FerretDiameter")
  599. {
  600. keyValues.Add("DFERET", table["str26"].ToString());
  601. }
  602. if (strs[i] == "PERP")
  603. {
  604. keyValues.Add("DPERP", table["str27"].ToString());
  605. }
  606. if (strs[i] == "PERI")
  607. {
  608. keyValues.Add("PERIMETER", table["str28"].ToString());
  609. }
  610. if (strs[i] == "INSCR")
  611. {
  612. keyValues.Add("DINSCR", table["str29"].ToString());
  613. }
  614. if (strs[i] == "MEAN")
  615. {
  616. keyValues.Add("DMEAN", table["str30"].ToString());
  617. }
  618. if (strs[i] == "ELONG")
  619. {
  620. keyValues.Add("DELONG", table["str31"].ToString());
  621. }
  622. if (strs[i] == "ASPECT_ELONG")
  623. {
  624. keyValues.Add("ASPECT_ELONG", table["str32"].ToString());
  625. }
  626. if (strs[i] == "Orientation")
  627. {
  628. keyValues.Add("ORIENTATION", table["str33"].ToString());
  629. }
  630. if (str_SysType == "1:CleannessA")
  631. {
  632. if (strs[i] == "Hardness")
  633. {
  634. keyValues.Add("Hardness", table["str34"].ToString());
  635. }
  636. if (strs[i] == "Density")
  637. {
  638. keyValues.Add("Density", table["str35"].ToString());
  639. }
  640. if (strs[i] == "Electrical_conductivity")
  641. {
  642. keyValues.Add("Electrical_conductivity", table["str36"].ToString());
  643. }
  644. }
  645. }
  646. keyValues.Add("Element", "Element");
  647. Dictionary<string, string>.Enumerator en = keyValues.GetEnumerator();
  648. for (int irow = 0; irow < keyValues.Count; irow++)
  649. {
  650. if (en.MoveNext())
  651. {
  652. if (en.Current.Key == "ParticleImage")
  653. {
  654. DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
  655. iconColumn.Name = en.Current.Key;
  656. iconColumn.HeaderText = en.Current.Value;
  657. dgV_ParticlesDevidePage.Columns.Add(iconColumn);
  658. }
  659. else if (en.Current.Key == "Element")
  660. {
  661. }
  662. else
  663. {
  664. int columndid = dgV_ParticlesDevidePage.Columns.Add(en.Current.Key, en.Current.Value);
  665. }
  666. }
  667. }
  668. if (PageSize == -1)
  669. {
  670. PageSize = particlesAll.Rows.Count;
  671. }
  672. if (particlesAll == null)
  673. {
  674. return;
  675. }
  676. RecordCount = particlesAll.Rows.Count;
  677. particles = particlesAll.Clone();
  678. for (int fi = (PageIndex - 1) * pageSize; fi < PageIndex * pageSize; fi++)
  679. {
  680. if (fi > RecordCount - 1)
  681. {
  682. break;
  683. }
  684. particles.ImportRow(particlesAll.Rows[fi]);
  685. }
  686. string particleM = "";
  687. //获取需要显示的元素名
  688. List<string> ElementTypeSort = new List<string>(str_ElementsColName.Split(',').ToList());//去重
  689. for (int i = 0; i < ElementTypeSort.Count; i++)
  690. {
  691. dgV_ParticlesDevidePage.Columns.Add(ElementTypeSort[i], ElementTypeSort[i]);
  692. }
  693. double jd = 95f / (double)particles.Rows.Count;//计算进度刻度
  694. string filePath = result.FilePath + "\\FIELD_FILES\\";
  695. KeyValuePair<string, Bitmap> FieldImage = new KeyValuePair<string, Bitmap>();
  696. for (int i = 0; i < particles.Rows.Count; i++)
  697. {
  698. //更新进度,每100条记录加载完,更新一次进度
  699. if (i % 10 == 0)
  700. m_frm_userprogress.SetProgressValueAndText((int)(jd * i), "loading..");
  701. Dictionary<string, string>.Enumerator enl = keyValues.GetEnumerator();
  702. int add_rowindex = dgV_ParticlesDevidePage.Rows.Add();
  703. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[0].Value = (pageSize * (PageIndex - 1) + add_rowindex + 1).ToString();
  704. for (int k = 0; k < keyValues.Count; k++)
  705. {
  706. if (enl.MoveNext())
  707. {
  708. if (enl.Current.Key == "rowid")
  709. {
  710. //dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = i + 1;
  711. }
  712. if (enl.Current.Key == "ParticleImage")
  713. {
  714. string subt = particles.Rows[i]["SubParticles"].ToString();
  715. if (subt != null && subt != "")
  716. {
  717. double ScanFieldSize = Convert.ToDouble(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["Sample"])["Members"])["SEMDataMsr"])["ScanFieldSize"]);
  718. string filePatht = result.FilePath + "\\FIELD_FILES\\";
  719. string imagePatht = filePatht + "Field" + subt.Split(',')[0].Split(':')[0].ToString() + ".bmp";
  720. //然后将取出的数据,转换成Bitmap对象
  721. Bitmap tempbit = Particledata.ReadImageFile(imagePatht);
  722. int pixw = tempbit.Width;
  723. double xs = pixw / ScanFieldSize;
  724. particleM = particleM + "," + subt;
  725. Bitmap bmap = Particledata.GetBitmapForBig(subt, xs, result.FilePath, tempbit.Height, tempbit.Width);
  726. if (bmap != null)
  727. {
  728. string[] str = subt.Split(',');
  729. bmap.Tag = new List<string>() { str[0].Split(':')[0], str[0].Split(':')[1], particles.Rows[i]["TypeId"].ToString() };
  730. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  731. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  732. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  733. dgV_ParticlesDevidePage.Rows[add_rowindex].DefaultCellStyle.ForeColor = Color.Chocolate;
  734. }
  735. else
  736. {
  737. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  738. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  739. }
  740. }
  741. else
  742. {
  743. if (FieldImage.Key != particles.Rows[i]["fieldid"].ToString() || FieldImage.Value == null)
  744. {
  745. string imagePath = filePath + "Field" + particles.Rows[i]["fieldid"].ToString() + ".bmp";
  746. FieldImage = new KeyValuePair<string, Bitmap>(particles.Rows[i]["fieldid"].ToString(), Particledata.ReadImageFile(imagePath));
  747. }
  748. Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(particles.Rows[i]["RectLeft"]), Y = Convert.ToInt32(particles.Rows[i]["RectTop"]), Width = Convert.ToInt32(particles.Rows[i]["RectWidth"]), Height = Convert.ToInt32(particles.Rows[i]["RectHeight"]) };
  749. Bitmap bmap = Particledata.GetBitmapByParticle(FieldImage.Value, rectangle);
  750. bmap.Tag = new List<string>() { particles.Rows[i]["FieldId"].ToString(), particles.Rows[i]["ParticleId"].ToString(), particles.Rows[i]["TypeId"].ToString() };
  751. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  752. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20;
  753. }
  754. }
  755. if (enl.Current.Key == "DiameterRatio")
  756. {
  757. double d = Convert.ToDouble(particles.Rows[i]["DMAX"]) / Convert.ToDouble(particles.Rows[i]["DMIN"]);
  758. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  759. }
  760. if (enl.Current.Key == "ASPECT_ELONG")
  761. {
  762. double d = Convert.ToDouble(particles.Rows[i]["DELONG"]) / Convert.ToDouble(particles.Rows[i]["DMEAN"]);
  763. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  764. }
  765. if (particles.Columns.Contains(enl.Current.Key))
  766. {
  767. double num = 0;
  768. if (double.TryParse(particles.Rows[i][enl.Current.Key].ToString(), out num))
  769. {
  770. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(num, 2);
  771. }
  772. else if (enl.Current.Key == "Element")
  773. {
  774. List<string> elementtemp = new List<string>(ElementTypeSort);
  775. string[] strcbo = particles.Rows[i][enl.Current.Key].ToString().Split(';');
  776. for (int j = 0; j < strcbo.Length; j++)
  777. {
  778. string[] str = strcbo[j].Split('-');
  779. if (ElementTypeSort.Contains(str[0]))
  780. { dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = Math.Round(double.Parse(str[1]), 2).ToString(); }
  781. elementtemp.Remove(str[0].ToString());
  782. }
  783. foreach (var ele in elementtemp)
  784. {
  785. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[ele].Value = "0";
  786. }
  787. }
  788. else
  789. {
  790. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = particles.Rows[i][enl.Current.Key];
  791. }
  792. }
  793. if (enl.Current.Key == "TypeName")
  794. {
  795. if (particles.Rows[i]["TypeId"].ToString() == "9")
  796. {
  797. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = "Not Identified";
  798. }
  799. }
  800. if (enl.Current.Key == "Equivalent")
  801. {
  802. double dSize = Convert.ToDouble(particles.Rows[i]["Area"]);
  803. double Diameter = Math.Sqrt(dSize / Math.PI) * 2;
  804. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(Diameter, 2);
  805. }
  806. }
  807. }
  808. }
  809. #region 加载进度条进度部份结束
  810. //加载完成设置鼠标为默认
  811. this.Cursor = Cursors.Default;
  812. string str8 = table["str8"].ToString();
  813. m_frm_userprogress.SetProgressValueAndText(100, str8);
  814. //加载完成,关闭进度条
  815. m_frm_userprogress.Close();
  816. #endregion
  817. dgV_ParticlesDevidePage.Visible = true;
  818. }
  819. /// <summary>
  820. /// 设置DataGridView样式
  821. /// </summary>
  822. private void SetDataGridViewStyle()
  823. {
  824. //用户不能调整标题的高度
  825. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  826. //用户不能调整 行高
  827. dgV_ParticlesDevidePage.AllowUserToResizeRows = false;
  828. //点击选择整行
  829. dgV_ParticlesDevidePage.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  830. //居中显示
  831. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  832. dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  833. dgV_ParticlesDevidePage.DefaultCellStyle = dataGridViewCellStyle1;
  834. dgV_ParticlesDevidePage.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  835. //再次重覆禁用拖动表头高度,居然有效果了
  836. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  837. //设置grid可以复制
  838. dgV_ParticlesDevidePage.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  839. //设置每列的宽度
  840. dgV_ParticlesDevidePage.Columns[0].Width = 40;//第一列序号的宽度设置一下吧,要不太丑
  841. dgV_ParticlesDevidePage.Columns[1].Width = 150;
  842. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].Width = 450;
  843. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  844. //设置序号列不排序
  845. dgV_ParticlesDevidePage.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  846. //设置序号列不可以设置宽度
  847. dgV_ParticlesDevidePage.Columns[0].Resizable = DataGridViewTriState.False;
  848. dgV_ParticlesDevidePage.RowsDefaultCellStyle.BackColor = Color.Azure;
  849. }
  850. #endregion
  851. #region 相关事件
  852. private void ToolStripMenuItem1_Click(object sender, EventArgs e)
  853. {
  854. //复制整个表
  855. CopyAll();
  856. }
  857. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  858. {
  859. //复制选择区域
  860. CopySelected();
  861. }
  862. /// <summary>
  863. /// 复制选择区域
  864. /// </summary>
  865. public void CopySelected()
  866. {
  867. //复制选择区域
  868. object oo = dgV_ParticlesDevidePage.GetClipboardContent();
  869. if (oo != null)
  870. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  871. }
  872. /// <summary>
  873. /// 复制所有
  874. /// </summary>
  875. public void CopyAll()
  876. {
  877. dgV_ParticlesDevidePage.SelectAll();
  878. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  879. }
  880. /// <summary>
  881. /// 以图像的方式将GridView进行截图
  882. /// </summary>
  883. public void CopyImage()
  884. {
  885. int height, width;
  886. width = dgV_ParticlesDevidePage.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 2;
  887. height = this.Height;
  888. Bitmap image = new Bitmap(width, height);
  889. dgV_ParticlesDevidePage.DrawToBitmap(image, new Rectangle(0, 0, width, height));
  890. Clipboard.SetImage(image);
  891. }
  892. private void toolStripMenuItem4_Click(object sender, EventArgs e)
  893. {
  894. //对gridview进行截图
  895. CopyImage();
  896. }
  897. private void dgV_ParticlesDevidePage_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  898. {
  899. //排序中
  900. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag.ToString() == "统计行")
  901. {
  902. //ROW1>ROW2
  903. e.SortResult = 1;
  904. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  905. e.SortResult = -1;
  906. e.Handled = true;
  907. return;
  908. }
  909. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag.ToString() == "统计行")
  910. {
  911. //ROW1<ROW2
  912. e.SortResult = -1;
  913. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  914. e.SortResult = 1;
  915. e.Handled = true;
  916. return;
  917. }
  918. }
  919. /// <summary>
  920. /// 将OTS坐标转换为Sem 坐标
  921. /// </summary>
  922. /// <param name="POTSCoord"></param>
  923. /// <returns></returns>
  924. public Point ChangeOTSToSemCoord(Point POTSCoord)
  925. {
  926. //first if m_semstagedata is null to get stage inforation
  927. Convert.ToDouble(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["scanFieldSize"]);
  928. //after obtaining stage info,calc stage point data
  929. Point ret_SEM_point = new Point();
  930. // get center point, um
  931. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  932. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  933. long xCenter = (xStart + xEnd) / 2;
  934. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  935. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  936. long yCenter = (yStart + yEnd) / 2;
  937. // delte = SEM - OTSa
  938. long deltex = xCenter - 0;
  939. long deltey = yCenter - 0;
  940. int xdir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["xAxisDir"]);
  941. int ydir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["yAxisDir"]);
  942. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  943. {
  944. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  945. }
  946. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  947. {
  948. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  949. }
  950. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  951. {
  952. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  953. }
  954. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  955. {
  956. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  957. }
  958. return ret_SEM_point;
  959. }
  960. #endregion
  961. #region 获取向导出模块提供的DataTable和GridView对象
  962. /// <summary>
  963. /// 获取到该模块输出后形成的DataTable,和GridView
  964. /// </summary>
  965. /// <param name="out_dt"></param>
  966. /// <param name="out_dg"></param>
  967. public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
  968. {
  969. out_dt = m_dt;
  970. out_dg = this.dgV_ParticlesDevidePage;//同样通过 dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[2].Value 来获取bitmap对象
  971. }
  972. #endregion
  973. #region 连接电镜相关
  974. /// <summary>
  975. /// 移动SEM到指定位置线程函数
  976. /// </summary>
  977. private void Thread_GO(object in_obj)
  978. {
  979. if (m_mythread_state == false)
  980. {
  981. m_mythread_state = true;
  982. Point sem_point = (Point)in_obj;
  983. //第一步,连接电镜
  984. m_OTSIncAReportGridsFun.Connection_ForParticlesGrid();
  985. Thread.Sleep(500);
  986. //第二步,移动到指定位置,先读取再设置
  987. if (m_OTSIncAReportGridsFun.m_SEMConnectionState == true)
  988. {
  989. m_OTSIncAReportGridsFun.MoveSemToPointXY_ForParticlesGrid(sem_point.X, sem_point.Y);
  990. }
  991. Thread.Sleep(1500);
  992. //第三步,断开电镜连接
  993. m_OTSIncAReportGridsFun.DisConnectSEM_ForParticlesGrid();
  994. //Thread.Sleep(500);
  995. m_mythread_state = false;
  996. }
  997. }
  998. #endregion
  999. private void ToolStripMenuItem_exportzoomimagefile_Click(object sender, EventArgs e)
  1000. {
  1001. //导出放大图像
  1002. FolderBrowserDialog fbd = new FolderBrowserDialog();
  1003. if (fbd.ShowDialog() == DialogResult.OK)
  1004. {
  1005. m_imagefilepath = fbd.SelectedPath;
  1006. for (int i = 0; i < m_dt.Rows.Count; i++)
  1007. {
  1008. Bitmap ls_bp = new Bitmap(1, 1);
  1009. ls_bp = (Bitmap)m_dt.Rows[i][1];
  1010. string strPath = m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg";
  1011. string strPath2 = m_imagefilepath + "\\" + (i + 1).ToString() + "s.jpg";
  1012. ls_bp.Save(strPath);
  1013. //m_COTSReportProjFileMgrClr.ReZoom(strPath, strPath2);
  1014. System.IO.File.Delete(strPath);//再将文件删除
  1015. }
  1016. string str1 = table["str9"].ToString();
  1017. MessageBox.Show(str1 + m_dt.Rows.Count.ToString());
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// 导出二次放大图像
  1022. /// </summary>
  1023. /// <param name="str_srcPathName"></param>
  1024. /// <param name="str_outPathName"></param>
  1025. public void ParticleReZoom(string str_srcPathName, string str_outPathName)
  1026. {
  1027. //m_COTSReportProjFileMgrClr.ReZoom(str_srcPathName, str_outPathName);
  1028. }
  1029. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  1030. {
  1031. //恢复至初始状态
  1032. dgV_ParticlesDevidePage.Rows.Clear();
  1033. dgV_ParticlesDevidePage.Columns.Clear();
  1034. BindDataGridView();
  1035. SetDataGridViewStyle();
  1036. }
  1037. private void ToolStripMenuItem5_Click(object sender, EventArgs e)
  1038. {
  1039. /*注意:多数据源情况下,只获取第一个数据源的选择了多行的情况下,只获取最后一个选择行的,没有办法,这是为了符合李阳博士提出的要求*/
  1040. //先获取鼠标所选择的行里的颗粒的FieldID和ParticleID
  1041. string i_ls_fieldid = "";
  1042. string i_ls_particleid = "";
  1043. int cou = dgV_ParticlesDevidePage.SelectedRows.Count;
  1044. if (cou > 0)
  1045. {
  1046. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[cou - 1];
  1047. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  1048. List<string> list_str = (List<string>)ls_bp.Tag;
  1049. if (list_str != null)
  1050. {
  1051. i_ls_fieldid = list_str[0];
  1052. i_ls_particleid = list_str[1];
  1053. }
  1054. }
  1055. ParticleData particleData = new ParticleData(result.FilePath);
  1056. Particle particle = particleData.GetParticleByFidAndPid(i_ls_fieldid, i_ls_particleid);
  1057. ////然后遍历所有的Field
  1058. //Point ls_ots_point = new Point() { X = particle.PosX, Y = particle.PosY };
  1059. ////将该坐标转成SEM坐标
  1060. //Point ls_sem_point = ChangeOTSToSemCoord(ls_ots_point);
  1061. //取得Field的Position,然后执行线程移动SEM到当前Field的位置
  1062. if (m_mythread == null)
  1063. {
  1064. return;
  1065. }
  1066. if (m_mythread.ThreadState == ThreadState.Running || m_mythread.ThreadState == ThreadState.WaitSleepJoin)
  1067. {
  1068. return;
  1069. }
  1070. if (m_mythread.ThreadState == ThreadState.Stopped)
  1071. {
  1072. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  1073. }
  1074. //改为线程调用,先判断线程状态
  1075. m_mythread.Start(new Point(particle.SEMPosX, particle.SEMPosY));
  1076. }
  1077. private void ToolStripMenuItem_selectparticle_Click(object sender, EventArgs e)
  1078. {
  1079. string path = result.FilePath;
  1080. ParticleData fielddata = new ParticleData(result.FilePath);
  1081. List<Particle> particles = fielddata.GetParticleAllList();
  1082. List<Particle> ls_list_cotsparticleclr = new List<Particle>();
  1083. for (int l = 0; l < dgV_ParticlesDevidePage.SelectedRows.Count; l++)
  1084. {
  1085. //从事先加载的Bitmap对象的Tag中取出List<string>
  1086. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[l];
  1087. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  1088. List<string> list_str = (List<string>)ls_bp.Tag;
  1089. if (list_str != null)
  1090. {
  1091. int i_ls_fieldid = Convert.ToInt32(list_str[0]);
  1092. int i_ls_particleid = Convert.ToInt32(list_str[1]);
  1093. foreach (Particle item in particles)
  1094. {
  1095. if (i_ls_fieldid == item.FieldId && i_ls_particleid == item.ParticleId)
  1096. {
  1097. ls_list_cotsparticleclr.Add(item);
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. }
  1103. m_ReportApp.SetSelectedParticles(ls_list_cotsparticleclr);
  1104. string str1 = table["str1"].ToString();
  1105. //然后循环得出,list cotsparticleclr列表,返回给框架
  1106. //m_sec.SendCall(str1, "", "", ls_list_cotsparticleclr);
  1107. string str2 = table["str2"].ToString();
  1108. string str3 = table["str3"].ToString();
  1109. MessageBox.Show(str2 + ls_list_cotsparticleclr.Count.ToString() + str3);
  1110. }
  1111. private void ToolStripMenuItem_exportimagefile_Click(object sender, EventArgs e)
  1112. {
  1113. FolderBrowserDialog fbd = new FolderBrowserDialog();
  1114. if (fbd.ShowDialog() == DialogResult.OK)
  1115. {
  1116. m_imagefilepath = fbd.SelectedPath;
  1117. for (int i = 0; i < dgV_ParticlesDevidePage.Rows.Count; i++)
  1118. {
  1119. Bitmap ls_bp = new Bitmap(1, 1);
  1120. ls_bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i].Cells[2].Value;
  1121. ls_bp.Save(m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg");
  1122. }
  1123. string str9 = table["str9"].ToString();
  1124. MessageBox.Show(str9 + dgV_ParticlesDevidePage.Rows.Count.ToString());
  1125. }
  1126. }
  1127. private void testToolStripMenuItem_Click(object sender, EventArgs e)
  1128. {
  1129. ////测试导出指定颗粒的能谱图,测试相关代码
  1130. //OTSIncAReportGraph.DParticle ls_dparticle = new OTSIncAReportGraph.DParticle();
  1131. //for (int i = 0; i < dgV_ParticlesDevidePage.SelectedRows.Count; i++)
  1132. //{
  1133. // //获取选择行颗粒的fieldid,和particleid
  1134. // string str_fieldid = "";
  1135. // string str_particleid = "";
  1136. // string str_stdtypeid = "";
  1137. // DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[i];
  1138. // Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  1139. // List<string> list_str = (List<string>)ls_bp.Tag;
  1140. // if (list_str != null)
  1141. // {
  1142. // str_fieldid = list_str[0];
  1143. // str_particleid = list_str[1];
  1144. // str_stdtypeid = list_str[2];
  1145. // }
  1146. // ls_dparticle.CLRFieldID = Convert.ToInt32(str_fieldid);
  1147. // ls_dparticle.CLRTagID = Convert.ToInt32(str_particleid);
  1148. // ls_dparticle.STDTypeID = Convert.ToInt32(str_stdtypeid);
  1149. //}
  1150. ////显示xray能谱
  1151. //ShowXRay(ls_dparticle);
  1152. //Bitmap ls_bpex = new Bitmap(control_XRayTable1.Width, control_XRayTable1.Height);
  1153. //control_XRayTable1.DrawToBitmap(ls_bpex, new Rectangle(0, 0, control_XRayTable1.Width, control_XRayTable1.Height));
  1154. ////Bitmap ls_bpex = control_XRayTable1.ExportXRayImage();
  1155. //ls_bpex.Save("e:\\test222.jpg");
  1156. }
  1157. /// <summary>
  1158. /// 判断条件输入 前<后
  1159. /// </summary>
  1160. /// <param name="min"></param>
  1161. /// <param name="max"></param>
  1162. /// <returns></returns>
  1163. bool CompareInput(string min, string max)
  1164. {
  1165. int imax = 0;
  1166. int imin = 0;
  1167. double dmax = 0;
  1168. double dmin = 0;
  1169. if (int.TryParse(max, out imax))
  1170. {
  1171. dmax = (double)imax;
  1172. }
  1173. else
  1174. {
  1175. dmax = Convert.ToDouble(max);
  1176. }
  1177. if (int.TryParse(min, out imin))
  1178. {
  1179. dmin = (double)imin;
  1180. }
  1181. else
  1182. {
  1183. dmin = Convert.ToDouble(min);
  1184. }
  1185. if (dmin <= dmax)
  1186. {
  1187. return true;
  1188. }
  1189. else
  1190. {
  1191. return false;
  1192. }
  1193. }
  1194. private void btn_Sel_Click(object sender, EventArgs e)
  1195. {
  1196. condition = "";
  1197. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text))
  1198. {
  1199. double dnum = 0;
  1200. int inum = 0;
  1201. if (double.TryParse(tBx_AreaMin.Text, out dnum) && dnum >= 0)
  1202. {
  1203. condition += " and Area" + " > " + tBx_AreaMin.Text;
  1204. }
  1205. else if (int.TryParse(tBx_AreaMin.Text, out inum) && inum >= 0)
  1206. {
  1207. condition += " and Area" + " > " + tBx_AreaMin.Text;
  1208. }
  1209. else
  1210. {
  1211. MessageBox.Show(table["str10"].ToString());
  1212. return;
  1213. }
  1214. }
  1215. if (!string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  1216. {
  1217. double dnum = 0;
  1218. int inum = 0;
  1219. if (double.TryParse(Tbx_AreaMax.Text, out dnum) && dnum >= 0)
  1220. {
  1221. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  1222. }
  1223. else if (int.TryParse(Tbx_AreaMax.Text, out inum) && inum >= 0)
  1224. {
  1225. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  1226. }
  1227. else
  1228. {
  1229. MessageBox.Show(table["str10"].ToString());
  1230. return;
  1231. }
  1232. }
  1233. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text) && !string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  1234. {
  1235. if (!CompareInput(tBx_AreaMin.Text, Tbx_AreaMax.Text))
  1236. {
  1237. MessageBox.Show(table["str11"].ToString());
  1238. return;
  1239. }
  1240. }
  1241. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text))
  1242. {
  1243. double dnum = 0;
  1244. int inum = 0;
  1245. if (double.TryParse(tbx_DmaxMin.Text, out dnum) && dnum >= 0)
  1246. {
  1247. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1248. }
  1249. else if (int.TryParse(tbx_DmaxMin.Text, out inum) && inum >= 0)
  1250. {
  1251. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1252. }
  1253. else
  1254. {
  1255. MessageBox.Show(table["str10"].ToString());
  1256. return;
  1257. }
  1258. }
  1259. if (!string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1260. {
  1261. double dnum = 0;
  1262. int inum = 0;
  1263. if (double.TryParse(tbx_DmaxMax.Text, out dnum) && dnum > 0)
  1264. {
  1265. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1266. }
  1267. else if (int.TryParse(tbx_DmaxMax.Text, out inum) && inum >= 0)
  1268. {
  1269. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1270. }
  1271. else
  1272. {
  1273. MessageBox.Show(table["str10"].ToString());
  1274. return;
  1275. }
  1276. }
  1277. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text) && !string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1278. {
  1279. if (!CompareInput(tbx_DmaxMin.Text, tbx_DmaxMax.Text))
  1280. {
  1281. MessageBox.Show(table["str11"].ToString());
  1282. return;
  1283. }
  1284. }
  1285. if (!string.IsNullOrWhiteSpace(tbx_Type.Text))
  1286. {
  1287. condition += " and TypeName Like \"%" + tbx_Type.Text + "%\" ";
  1288. }
  1289. if (m_frm_userprogress.IsDisposed)
  1290. {
  1291. ProgressStart();
  1292. }
  1293. UpdateTable();
  1294. lnkFirst_Click(null, null);
  1295. SetDataGridViewStyle();
  1296. }
  1297. void ProgressStart()
  1298. {
  1299. #region 加载显示进度条部份
  1300. this.Cursor = Cursors.WaitCursor;
  1301. m_frm_userprogress = new Frm_UserProgress();
  1302. //显示进度条,计算进度条应该显示的位置和宽度
  1303. Form ls_main_form = this.ParentForm.ParentForm;//取出父窗体
  1304. if (ls_main_form == null)
  1305. {
  1306. m_frm_userprogress.Visible = false;
  1307. }
  1308. else
  1309. {
  1310. m_frm_userprogress.Width = (int)(MyPrimaryScreen.DESKTOP.Width / MyPrimaryScreen.ScaleX * 0.9);
  1311. m_frm_userprogress.Location = new Point(ls_main_form.Location.X + 200, ls_main_form.Location.Y + 200 + (int)(MyPrimaryScreen.DESKTOP.Height / MyPrimaryScreen.ScaleX) / 2);
  1312. m_frm_userprogress.Show();
  1313. m_frm_userprogress.SetProgressValueAndText(0, "Loading data of particles...");
  1314. }
  1315. #endregion
  1316. }
  1317. private void cbB_order_SelectedIndexChanged(object sender, EventArgs e)
  1318. {
  1319. //int ordernum = cbB_order.SelectedIndex;
  1320. //if (ordernum == 0)
  1321. //{
  1322. // OrderFunction = "fieldid,particleid";
  1323. //}
  1324. //else if (ordernum == 1)
  1325. //{
  1326. // OrderFunction = "TypeName";
  1327. //}
  1328. //else if (ordernum == 2)
  1329. //{
  1330. // OrderFunction = "DMAX";
  1331. //}
  1332. //else if (ordernum == 3)
  1333. //{
  1334. // OrderFunction = "DMAX desc";
  1335. //}
  1336. //else if (ordernum == 4)
  1337. //{
  1338. // OrderFunction = "Area";
  1339. //}
  1340. //else if (ordernum == 5)
  1341. //{
  1342. // OrderFunction = "Area desc";
  1343. //}
  1344. lnkFirst_Click(null, null);
  1345. SetDataGridViewStyle();
  1346. }
  1347. /// <summary>
  1348. /// 页面控件呈现
  1349. /// </summary>
  1350. private void DrawControl(bool callEvent)
  1351. {
  1352. BindDataGridView();
  1353. lblTotalCount.Text = RecordCount.ToString();
  1354. lblPageCount.Text = PageCount.ToString();
  1355. txtPageNum.Text = PageIndex.ToString();
  1356. SetFormCtrEnabled();
  1357. if (PageCount == 0 || PageCount == 1)//有且仅有一页
  1358. {
  1359. lnkFirst.Enabled = false;
  1360. lnkPrev.Enabled = false;
  1361. lnkNext.Enabled = false;
  1362. lnkLast.Enabled = false;
  1363. btnGo.Enabled = false;
  1364. }
  1365. else if (PageIndex == 1)//第一页
  1366. {
  1367. lnkFirst.Enabled = false;
  1368. lnkPrev.Enabled = false;
  1369. }
  1370. else if (PageIndex == PageCount)//最后一页
  1371. {
  1372. lnkNext.Enabled = false;
  1373. lnkLast.Enabled = false;
  1374. }
  1375. }
  1376. private void SetFormCtrEnabled()
  1377. {
  1378. lnkFirst.Enabled = true;
  1379. lnkPrev.Enabled = true;
  1380. lnkNext.Enabled = true;
  1381. lnkLast.Enabled = true;
  1382. btnGo.Enabled = true;
  1383. }
  1384. /// <summary>
  1385. /// 分页属性改变了。
  1386. /// </summary>
  1387. private void txtPageSize_TextChanged(object sender, EventArgs e)
  1388. {
  1389. int num = 0;
  1390. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1391. {
  1392. }
  1393. else { txtPageNum.Text = "1"; }
  1394. }
  1395. private void lnkFirst_Click(object sender, EventArgs e)
  1396. {
  1397. PageIndex = 1;
  1398. DrawControl(true);
  1399. }
  1400. private void lnkPrev_Click(object sender, EventArgs e)
  1401. {
  1402. PageIndex = Math.Max(1, PageIndex - 1);
  1403. DrawControl(true);
  1404. }
  1405. private void lnkNext_Click(object sender, EventArgs e)
  1406. {
  1407. PageIndex = Math.Min(PageCount, PageIndex + 1);
  1408. DrawControl(true);
  1409. }
  1410. private void lnkLast_Click(object sender, EventArgs e)
  1411. {
  1412. PageIndex = PageCount;
  1413. DrawControl(true);
  1414. }
  1415. /// <summary>
  1416. /// 跳转
  1417. /// </summary>
  1418. /// <param name="sender"></param>
  1419. /// <param name="e"></param>
  1420. private void btnGo_Click(object sender, EventArgs e)
  1421. {
  1422. int num = 0;
  1423. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1424. {
  1425. if (num > PageCount)
  1426. {
  1427. num = PageCount;
  1428. txtPageNum.Text = PageCount.ToString();
  1429. }
  1430. PageIndex = num;
  1431. DrawControl(true);
  1432. }
  1433. }
  1434. private void DisplayData()
  1435. {
  1436. if (cbB_PageSize.SelectedItem.ToString() == "All")
  1437. {
  1438. pageSize = -1;
  1439. }
  1440. else
  1441. {
  1442. pageSize = int.Parse(cbB_PageSize.SelectedItem.ToString());
  1443. }
  1444. lnkFirst_Click(null, null);
  1445. SetDataGridViewStyle();
  1446. }
  1447. private void cbB_PageSize_SelectedIndexChanged(object sender, EventArgs e)
  1448. {
  1449. DisplayData();
  1450. }
  1451. /// <summary>
  1452. /// 在帧图上标记颗粒的位置矩形,并保存到FIELD_FILES_MARK文件夹中
  1453. /// </summary>
  1454. public void SaveMarkParticleRectangleOnFieldFile(string str_resultPath, DataTable dataTable, out List<string> vs, out DataTable dt_FIeld)
  1455. {
  1456. Init();
  1457. List<string> maxlength = new List<string>();
  1458. for (int i = 0; i < dataTable.Rows.Count; i++)
  1459. {
  1460. if (dataTable.Rows[i]["TypeName"].ToString() != "Not Identified")
  1461. {
  1462. if (maxlength.Count < 10)
  1463. {
  1464. maxlength.Add(dataTable.Rows[i]["Fieldid"].ToString());
  1465. }
  1466. else
  1467. {
  1468. break;
  1469. }
  1470. }
  1471. }
  1472. List<string> max_list = maxlength.Distinct().ToList();
  1473. string str_path_FIELD_FILES = str_resultPath + "\\FIELD_FILES\\";
  1474. string str_path_FIELD_FILES_MARK = str_resultPath + "\\FIELD_FILES_MARK\\";
  1475. int intQuantity = 0;
  1476. //List<string> vs = new List<string>();
  1477. dt_FIeld = new DataTable();
  1478. vs = new List<string>();
  1479. ////判断是否已经有导出过的标记帧图文件夹,如果已经存在,则直接返回不输出,先不加
  1480. DirectoryInfo theFolder_FieldMark = new DirectoryInfo(str_path_FIELD_FILES_MARK);
  1481. if (!theFolder_FieldMark.Exists)
  1482. {
  1483. //创建
  1484. theFolder_FieldMark.Create();
  1485. }
  1486. else
  1487. {
  1488. theFolder_FieldMark.Delete(true);
  1489. //创建
  1490. theFolder_FieldMark.Create();
  1491. //已经存在的话,直接返回
  1492. //return;
  1493. }
  1494. //查找该field下对应所有的颗粒
  1495. DataTable dt_AllParticle = fieldData.GetAllParticle_DataTable();
  1496. DirectoryInfo theFolder = new DirectoryInfo(str_path_FIELD_FILES);
  1497. if (theFolder.Exists)
  1498. {
  1499. DataTable dt = new DataTable();
  1500. dt.Columns.Add("fieldid");
  1501. dt.Columns.Add("id");
  1502. dt.Columns.Add("fieldName");
  1503. dt.Columns.Add("cunt", typeof(int));
  1504. dt.Columns.Add("fieldFullName");
  1505. for (int i = 0; i < max_list.Count; i++)
  1506. {
  1507. foreach (FileInfo nextifile in theFolder.GetFiles())
  1508. {
  1509. if (nextifile.Name.Contains(".bmp") == true || nextifile.Name.Contains(".BMP") == true)
  1510. {
  1511. //确认对应的帧图名
  1512. string str_fieldid = Path.GetFileNameWithoutExtension(nextifile.Name);
  1513. str_fieldid = str_fieldid.Substring(5, str_fieldid.Length - 5);//减去field字符长度
  1514. bool bl = false;
  1515. if (str_fieldid == max_list[i].ToString())
  1516. {
  1517. bl = true;
  1518. }
  1519. if (bl)
  1520. {
  1521. DataRow dr = dt.NewRow();
  1522. dr["fieldid"] = " fieldid = " + str_fieldid;
  1523. dr["cunt"] = dataTable.Select(" fieldid = " + str_fieldid).Count();
  1524. dr["fieldName"] = nextifile.Name;
  1525. dr["fieldFullName"] = nextifile.FullName;
  1526. dr["id"] = str_fieldid;
  1527. dt.Rows.Add(dr);
  1528. }
  1529. }
  1530. }
  1531. }
  1532. //DataView dv = dt.DefaultView;
  1533. //dv.Sort = "cunt DESC";
  1534. dt_FIeld = dt.Copy();
  1535. for (int i = 0; i < dt_FIeld.Rows.Count; i++)
  1536. {
  1537. if (dataTable.Select(dt_FIeld.Rows[i]["fieldid"].ToString()).Count() > 0)
  1538. {
  1539. vs.Add(dt_FIeld.Rows[i]["id"].ToString());
  1540. Image img = Image.FromFile(dt_FIeld.Rows[i]["fieldFullName"].ToString());
  1541. //img.Save(@"D:\1.bmp");
  1542. Bitmap ls_fieldbp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  1543. using (Graphics g = Graphics.FromImage(ls_fieldbp))
  1544. {
  1545. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  1546. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  1547. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  1548. g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
  1549. }
  1550. //ls_fieldbp.Save(@"D:\1.bmp");
  1551. img.Dispose();
  1552. img = null;
  1553. //循环将颗粒,进行标记到bitmap图上
  1554. DataRow[] drlist = dt_AllParticle.Select(dt_FIeld.Rows[i]["fieldid"].ToString());
  1555. if (drlist.Count() > 0)
  1556. intQuantity = intQuantity + 1;
  1557. for (int i_row = 0; i_row < drlist.Count(); i_row++)
  1558. {
  1559. //string str_fieldid = drlist[i_row]["fieldid"].ToString();
  1560. string str_particleid = drlist[i_row]["particleid"].ToString();
  1561. string str_stdtypeid = drlist[i_row]["typeid"].ToString();
  1562. string str_offset_rect_X = drlist[i_row]["RectLeft"].ToString();
  1563. string str_offset_rect_Y = drlist[i_row]["RectTop"].ToString();
  1564. string str_offset_rect_Width = drlist[i_row]["RectWidth"].ToString();
  1565. string str_offset_rect_Height = drlist[i_row]["RectHeight"].ToString();
  1566. //确定需要标记颗粒的定位
  1567. Rectangle offset_rect = new Rectangle(Convert.ToInt32(str_offset_rect_X), Convert.ToInt32(str_offset_rect_Y),
  1568. Convert.ToInt32(str_offset_rect_Width), Convert.ToInt32(str_offset_rect_Height));
  1569. //向帧图进行标记颗粒位置
  1570. Graphics g = Graphics.FromImage(ls_fieldbp);
  1571. g.DrawRectangle(new Pen(Color.Aquamarine), offset_rect);
  1572. //判断是否超出了图像外面,是的话,让文字在左侧显示,默认在右侧显示
  1573. int ls_offsetx = offset_rect.X + offset_rect.Width;
  1574. if (ls_offsetx > ls_fieldbp.Width - 10)
  1575. {
  1576. ls_offsetx = offset_rect.X - 10;
  1577. }
  1578. g.DrawString(dt_FIeld.Rows[i]["id"].ToString() + "" + str_particleid.ToString(),
  1579. new Font("黑体", 8), new SolidBrush(Color.Aqua), new PointF(ls_offsetx, offset_rect.Y));
  1580. //保存带有标记的帧图
  1581. ls_fieldbp.Save(str_path_FIELD_FILES_MARK + dt_FIeld.Rows[i]["id"].ToString() + ".bmp");
  1582. //ls_fieldbp.Dispose();
  1583. }
  1584. }
  1585. }
  1586. }
  1587. //
  1588. GC.Collect();
  1589. GC.WaitForPendingFinalizers();
  1590. }
  1591. private void testToolStripMenuItem1_Click(object sender, EventArgs e)
  1592. {
  1593. //SaveMarkParticleRectangleOnFieldFile();
  1594. MessageBox.Show("Export completed!");
  1595. }
  1596. private void EXCELToolStripMenuItem_Click(object sender, EventArgs e)
  1597. {
  1598. //将所有的数据导出到EXCEL中
  1599. SaveFileDialog sfd = new SaveFileDialog();
  1600. sfd.Filter = "Excel File(*.xlsx)|*.xlsx";
  1601. //设置默认文件类型显示顺序
  1602. sfd.FilterIndex = 1;
  1603. //保存对话框是否记忆上次打开的目录
  1604. sfd.RestoreDirectory = true;
  1605. if (sfd.ShowDialog() == DialogResult.OK)
  1606. {
  1607. //IWorkbook workbook = new HSSFWorkbook(); //用于创建.xls office2003开始以前的
  1608. IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(); //用于创建.xlsx office2007开始以后的
  1609. ISheet sheet;
  1610. //创建Excel文件
  1611. FileStream fs = File.Create(sfd.FileName);
  1612. fs.Close();
  1613. sheet = workbook.CreateSheet("Particles");//创建工作表
  1614. ISheet sheet3;
  1615. sheet3 = workbook.CreateSheet("Peak spectrum");
  1616. //创建表格边框样式风格
  1617. ICellStyle cellStyle = workbook.CreateCellStyle();
  1618. cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  1619. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1620. //设置上4边
  1621. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1622. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1623. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1624. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1625. //设置字符大小和颜色
  1626. IFont font = workbook.CreateFont();
  1627. font.FontName = "黑体";
  1628. font.FontHeightInPoints = 10;
  1629. cellStyle.SetFont(font);
  1630. //设置颗粒名列宽
  1631. sheet.SetColumnWidth(1, 30 * 256);//夹杂物名列宽
  1632. sheet.SetColumnWidth(2, 7 * 256);//图像列宽
  1633. IRow row;
  1634. ICell cell;
  1635. //插入表头
  1636. row = sheet.CreateRow(1);//从第15行添加一行
  1637. row.Height = 30 * 20;
  1638. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1639. {
  1640. cell = row.CreateCell(i_cell);
  1641. cell.CellStyle = cellStyle;
  1642. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i_cell].Name);
  1643. }
  1644. //插入表内容
  1645. for (int i_row = 0; i_row < dgV_ParticlesDevidePage.Rows.Count; i_row++)
  1646. {
  1647. row = sheet.CreateRow(2 + i_row);
  1648. row.Height = 45 * 20;
  1649. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1650. {
  1651. cell = row.CreateCell(i_cell);
  1652. cell.CellStyle = cellStyle;
  1653. if (dgV_ParticlesDevidePage[i_cell, i_row].Value == null)
  1654. continue;
  1655. if (i_cell == 2)
  1656. {
  1657. //颗粒图像列
  1658. Bitmap bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i_row].Cells[2].Value;
  1659. byte[] bytes = ImageConvertToBytes(bp);
  1660. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  1661. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  1662. //第三步,在sheet中创建画布
  1663. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  1664. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  1665. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2, i_cell, i_row + 2, i_cell + 1, i_row + 3);//终止比开始位置大1,会自动缩放到一个单元格内的
  1666. //第五步,创建图片
  1667. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  1668. //图像效果不好,自己另外导出吧,这里对该列宽进行了设置0
  1669. }
  1670. //else if(i_cell==0)
  1671. //{
  1672. // ICreationHelper createHelper = workbook.GetCreationHelper();
  1673. // XSSFHyperlink link = (XSSFHyperlink)createHelper.CreateHyperlink(HyperlinkType.Url);
  1674. // link.Address=;
  1675. // cell.Hyperlink.Address(link);
  1676. //}
  1677. else
  1678. {
  1679. //非图像列
  1680. double dbl = 0;
  1681. if (double.TryParse(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString(), out dbl))
  1682. {
  1683. cell.SetCellValue(dbl);
  1684. }
  1685. else
  1686. {
  1687. cell.SetCellValue(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString());
  1688. }
  1689. }
  1690. }
  1691. }
  1692. ISheet sheet2;
  1693. sheet2 = workbook.CreateSheet("Peak spectrum Data");//创建工作表
  1694. //插入表头
  1695. row = sheet2.CreateRow(1);
  1696. row.Height = 30 * 20;
  1697. cell = row.CreateCell(0);
  1698. cell.CellStyle = cellStyle;
  1699. cell.SetCellValue("rowid");
  1700. for (int i_cell = 1; i_cell < 2001; i_cell++)
  1701. {
  1702. cell = row.CreateCell(i_cell);
  1703. cell.CellStyle = cellStyle;
  1704. if (i_cell == 1)
  1705. {
  1706. cell.SetCellValue("PeakSpectrumData");
  1707. }
  1708. }
  1709. sheet2.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 1, 2000));
  1710. for (int j = 0; j < particles.Rows.Count; j++)
  1711. {
  1712. row = sheet2.CreateRow(2 + j);
  1713. cell = row.CreateCell(0);
  1714. cell.CellStyle = cellStyle;
  1715. cell.SetCellValue(j + 1);
  1716. var tmpPart = Particledata.GetParticleXrayDataByFidAndPid(particles.Rows[j]["fieldid"].ToString(), particles.Rows[j]["particleid"].ToString());
  1717. if (tmpPart != null)
  1718. {
  1719. if (tmpPart.XrayId > -1)
  1720. {
  1721. uint[] Analysis_xray = new uint[2000];
  1722. for (int i = 0; i < 2000; i++)
  1723. {
  1724. Analysis_xray[i] = BitConverter.ToUInt32(tmpPart.XRayData, i * 4);
  1725. cell = row.CreateCell(i + 1);
  1726. cell.CellStyle = cellStyle;
  1727. cell.SetCellValue(Analysis_xray[i]);
  1728. }
  1729. }
  1730. }
  1731. }
  1732. sheet3.SetColumnWidth(1, 200 * 256);//图像列宽
  1733. //插入表头
  1734. row = sheet3.CreateRow(1);
  1735. row.Height = 30 * 20;
  1736. cell = row.CreateCell(0);
  1737. cell.CellStyle = cellStyle;
  1738. cell.SetCellValue("rowid");
  1739. cell = row.CreateCell(1);
  1740. cell.CellStyle = cellStyle;
  1741. cell.SetCellValue("PeakSpectrum");
  1742. IDrawing drawing;
  1743. IClientAnchor anchor3;
  1744. for (int j = 0; j < particles.Rows.Count; j++)
  1745. {
  1746. row = sheet3.CreateRow(j + 2);
  1747. row.Height = 120 * 20;
  1748. cell = row.CreateCell(0);
  1749. cell.CellStyle = cellStyle;
  1750. cell.SetCellValue(j + 1);
  1751. cell = row.CreateCell(1);
  1752. cell = row.CreateCell(2);
  1753. cell = row.CreateCell(3);
  1754. drawing = sheet3.CreateDrawingPatriarch();
  1755. //锚点
  1756. anchor3 = drawing.CreateAnchor(0, 0, 0, 0, 1, j + 2, 2, j + 3);//XSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2);参数说明:dx1 dy1 起始单元格中的x,y坐标.dx2 dy2 结束单元格中的x,y坐标col1, row1 指定起始的单元格,下标从0开始col2, row2 指定结束的单元格,下标从0开始
  1757. CreateChart(drawing, sheet2, anchor3, j + 2);
  1758. }
  1759. //row = sheet3.CreateRow(2);
  1760. //row.Height = 120 * 20;
  1761. //cell = row.CreateCell(0);
  1762. //cell.CellStyle = cellStyle;
  1763. //cell.SetCellValue(1);
  1764. //cell = row.CreateCell(1);
  1765. //cell = row.CreateCell(2);
  1766. //cell = row.CreateCell(3);
  1767. //drawing = sheet3.CreateDrawingPatriarch();
  1768. ////锚点
  1769. //anchor3 = drawing.CreateAnchor(0, 0, 0, 0, 1, 2, 2, 3);//XSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2);参数说明:dx1 dy1 起始单元格中的x,y坐标.dx2 dy2 结束单元格中的x,y坐标col1, row1 指定起始的单元格,下标从0开始col2, row2 指定结束的单元格,下标从0开始
  1770. //CreateChart(drawing, sheet2, anchor3, 2);
  1771. //row = sheet3.CreateRow(3);
  1772. //row.Height = 120 * 20;
  1773. //cell = row.CreateCell(0);
  1774. //cell.CellStyle = cellStyle;
  1775. //cell.SetCellValue(1);
  1776. //cell = row.CreateCell(1);
  1777. //cell = row.CreateCell(2);
  1778. //cell = row.CreateCell(3);
  1779. //drawing = sheet3.CreateDrawingPatriarch();
  1780. ////锚点
  1781. //anchor3 = drawing.CreateAnchor(0, 0, 0, 0, 1, 3, 2, 4);//XSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2);参数说明:dx1 dy1 起始单元格中的x,y坐标.dx2 dy2 结束单元格中的x,y坐标col1, row1 指定起始的单元格,下标从0开始col2, row2 指定结束的单元格,下标从0开始
  1782. //CreateChart(drawing, sheet2, anchor3, 3);
  1783. //完成后,对Excel进行保存
  1784. FileStream file = new FileStream(sfd.FileName, FileMode.Create);
  1785. workbook.Write(file);
  1786. file.Close();
  1787. MessageBox.Show("Export complete!");
  1788. //导出完成后,打开Excel文件
  1789. if (File.Exists(sfd.FileName))
  1790. {
  1791. //打开刚才导出的文件
  1792. System.Diagnostics.Process.Start(sfd.FileName);
  1793. }
  1794. // 生成一个 单元格 对象 HSSFCell cellTableName = CreateCell(sheetTableIndex, t + 1, 0, strTableName, true);
  1795. //创建一个超链接对象 HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
  1796. // strTableName 这个参数为 sheet名字 A1 为单元格 其他是固定格式 link.Address = "#" + strTableName + "!A1";
  1797. //设置 cellTableName 单元格 的连接对象 cellTableName.Hyperlink = link;
  1798. }
  1799. }
  1800. void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, int rowid)
  1801. {
  1802. var chart = drawing.CreateChart(anchor) as XSSFChart;
  1803. // 创建图形注释的位置
  1804. //var legend = chart.GetOrCreateLegend();
  1805. //legend.Position = LegendPosition.Top;
  1806. //图表
  1807. var data = chart.ChartDataFactory.CreateLineChartData<double, double>(); //折线图
  1808. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
  1809. IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
  1810. leftAxis.Crosses=AxisCrosses.AutoZero;
  1811. leftAxis.IsVisible = true;
  1812. bottomAxis.IsVisible = true;
  1813. //数据源
  1814. IChartDataSource<double> ys = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(rowid, rowid, 1, 2001));
  1815. Double[] doubles = new Double[2000];
  1816. for (int i = 0; i < 2000; i++)
  1817. {
  1818. doubles[i] = i;
  1819. }
  1820. IChartDataSource<double> xs = DataSources.FromArray(doubles);
  1821. //IChartDataSource<double> ys = DataSources.FromArray(doubles);
  1822. //数据系列
  1823. var s1 = data.AddSeries(xs, ys);
  1824. // 开始绘制折线图
  1825. chart.Plot(data, bottomAxis, leftAxis);
  1826. }
  1827. /// <summary>
  1828. /// 将image转成bytes
  1829. /// </summary>
  1830. /// <param name="in_img"></param>
  1831. /// <returns></returns>
  1832. public byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  1833. {
  1834. MemoryStream ms = new MemoryStream();
  1835. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  1836. return ms.ToArray();
  1837. }
  1838. public bool CheckParamExist<T>(T Param)
  1839. {
  1840. if (Param == null)
  1841. {
  1842. return false;
  1843. }
  1844. else
  1845. {
  1846. return true;
  1847. }
  1848. }
  1849. private void ToolStripMenuItem_Delete_Particle_Click(object sender, EventArgs e)
  1850. {
  1851. string path = result.FilePath;
  1852. ParticleData fielddata = new ParticleData(result.FilePath);
  1853. List<Particle> particles = fielddata.GetParticleAllList();
  1854. List<Particle> ls_list_cotsparticleclr = new List<Particle>();
  1855. for (int l = 0; l < dgV_ParticlesDevidePage.SelectedRows.Count; l++)
  1856. {
  1857. //从事先加载的Bitmap对象的Tag中取出List<string>
  1858. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[l];
  1859. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  1860. List<string> list_str = (List<string>)ls_bp.Tag;
  1861. if (list_str != null)
  1862. {
  1863. int i_ls_fieldid = Convert.ToInt32(list_str[0]);
  1864. int i_ls_particleid = Convert.ToInt32(list_str[1]);
  1865. foreach (Particle item in particles)
  1866. {
  1867. if (i_ls_fieldid == item.FieldId && i_ls_particleid == item.ParticleId)
  1868. {
  1869. if (ReportFun.DeleteParticlesFromLibrary(item.ParticleId, item.FieldId))
  1870. {
  1871. MessageBox.Show("Deleted successfully");
  1872. for (int i = 0; i < particlesAll.Rows.Count; i++)
  1873. {
  1874. if (particlesAll.Rows[i]["Fieldid"].ToString() == item.FieldId.ToString() && particlesAll.Rows[i]["Particleid"].ToString() == item.ParticleId.ToString())
  1875. {
  1876. particlesAll.Rows.RemoveAt(i);
  1877. }
  1878. }
  1879. DisplayData();
  1880. }
  1881. else
  1882. {
  1883. MessageBox.Show("Delete failed");
  1884. }
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. }
  1890. }
  1891. #region 将DataTable中的数据写到Excel的指定Sheet中
  1892. /// <summary>
  1893. /// 将DataTable中的数据写到Excel的指定Sheet中
  1894. /// </summary>
  1895. /// <param name="dt"></param>
  1896. /// <param name="m_Sheet"></param>
  1897. public void DataTableToSheet(string title, DataTable dt, Microsoft.Office.Interop.Excel._Worksheet m_Sheet,
  1898. Microsoft.Office.Interop.Excel._Workbook m_Book, int startrow)
  1899. {
  1900. //以下是填写EXCEL中数据
  1901. Microsoft.Office.Interop.Excel.Range range = m_Sheet.get_Range(m_Sheet.Cells[1, 1], m_Sheet.Cells[1, 2]);
  1902. range.MergeCells = true; //合并单元格
  1903. range.Font.Bold = true; //加粗单元格内字符
  1904. //写入题目
  1905. m_Sheet.Cells[startrow, startrow] = title;
  1906. int rownum = dt.Rows.Count;//行数
  1907. int columnnum = dt.Columns.Count;//列数
  1908. int num = rownum + 2; //得到数据中的最大行数
  1909. //写入列标题
  1910. for (int j = 0; j < columnnum; j++)
  1911. {
  1912. int bt_startrow = startrow + 1;
  1913. //将字段名写入文档
  1914. m_Sheet.Cells[bt_startrow, 1 + j] = dt.Columns[j].ColumnName;
  1915. //单元格内背景色
  1916. m_Sheet.get_Range(m_Sheet.Cells[bt_startrow, 1 + j], m_Sheet.Cells[bt_startrow, 1 + j]).Interior.ColorIndex = 15;
  1917. }
  1918. //逐行写入数据
  1919. for (int i = 0; i < rownum; i++)
  1920. {
  1921. for (int j = 0; j < columnnum; j++)
  1922. {
  1923. m_Sheet.Cells[startrow + 2 + i, 1 + j] = dt.Rows[i][j].ToString();
  1924. }
  1925. }
  1926. m_Sheet.Columns.AutoFit();
  1927. //在当前工作表中根据数据生成图表
  1928. CreateChart(m_Book, m_Sheet, num);
  1929. }
  1930. private void CreateChart(Microsoft.Office.Interop.Excel._Workbook m_Book, Microsoft.Office.Interop.Excel._Worksheet m_Sheet, int num)
  1931. {
  1932. Microsoft.Office.Interop.Excel.Range oResizeRange;
  1933. Microsoft.Office.Interop.Excel.Series oSeries;
  1934. m_Book.Charts.Add(Missing.Value, Missing.Value, 1, Missing.Value);
  1935. m_Book.ActiveChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;//设置图形
  1936. //设置数据取值范围
  1937. m_Book.ActiveChart.SetSourceData(m_Sheet.get_Range("A2", "C" + num.ToString()), Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
  1938. //m_Book.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAutomatic, title);
  1939. //以下是给图表放在指定位置
  1940. m_Book.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, m_Sheet.Name);
  1941. oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Rows.get_Item(10, Missing.Value);
  1942. m_Sheet.Shapes.Item("Chart 1").Top = (float)(double)oResizeRange.Top; //调图表的位置上边距
  1943. oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Columns.get_Item(6, Missing.Value); //调图表的位置左边距
  1944. // m_Sheet.Shapes.Item("Chart 1").Left = (float)(double)oResizeRange.Left;
  1945. m_Sheet.Shapes.Item("Chart 1").Width = 400; //调图表的宽度
  1946. m_Sheet.Shapes.Item("Chart 1").Height = 250; //调图表的高度
  1947. m_Book.ActiveChart.PlotArea.Interior.ColorIndex = 19; //设置绘图区的背景色
  1948. m_Book.ActiveChart.PlotArea.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置绘图区边框线条
  1949. m_Book.ActiveChart.PlotArea.Width = 400; //设置绘图区宽度
  1950. //m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; //设置整个图表的背影颜色
  1951. //m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;// 设置整个图表的边框颜色
  1952. m_Book.ActiveChart.ChartArea.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置边框线条
  1953. m_Book.ActiveChart.HasDataTable = false;
  1954. //设置Legend图例的位置和格式
  1955. m_Book.ActiveChart.Legend.Top = 20.00; //具体设置图例的上边距
  1956. m_Book.ActiveChart.Legend.Left = 60.00;//具体设置图例的左边距
  1957. m_Book.ActiveChart.Legend.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexNone;
  1958. m_Book.ActiveChart.Legend.Width = 150;
  1959. m_Book.ActiveChart.Legend.Font.Size = 9.5;
  1960. //m_Book.ActiveChart.Legend.Font.Bold = true;
  1961. m_Book.ActiveChart.Legend.Font.Name = "宋体";
  1962. //m_Book.ActiveChart.Legend.Position = Microsoft.Office.Interop.Excel.XlLegendPosition.xlLegendPositionTop;//设置图例的位置
  1963. m_Book.ActiveChart.Legend.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置图例边框线条
  1964. //设置X轴的显示
  1965. Microsoft.Office.Interop.Excel.Axis xAxis = (Microsoft.Office.Interop.Excel.Axis)m_Book.ActiveChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
  1966. xAxis.MajorGridlines.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlDot;
  1967. xAxis.MajorGridlines.Border.ColorIndex = 1;//gridLine横向线条的颜色
  1968. xAxis.HasTitle = false;
  1969. xAxis.MinimumScale = 1500;
  1970. xAxis.MaximumScale = 6000;
  1971. xAxis.TickLabels.Font.Name = "宋体";
  1972. xAxis.TickLabels.Font.Size = 9;
  1973. //设置Y轴的显示
  1974. Microsoft.Office.Interop.Excel.Axis yAxis = (Microsoft.Office.Interop.Excel.Axis)m_Book.ActiveChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
  1975. yAxis.TickLabelSpacing = 30;
  1976. yAxis.TickLabels.NumberFormat = "M月D日";
  1977. yAxis.TickLabels.Orientation = Microsoft.Office.Interop.Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平还是垂直等
  1978. yAxis.TickLabels.Font.Size = 8;
  1979. yAxis.TickLabels.Font.Name = "宋体";
  1980. //m_Book.ActiveChart.Floor.Interior.ColorIndex = 8;
  1981. /***以下是设置标题*****
  1982. m_Book.ActiveChart.HasTitle=true;
  1983. m_Book.ActiveChart.ChartTitle.Text = "净值指数";
  1984. m_Book.ActiveChart.ChartTitle.Shadow = true;
  1985. m_Book.ActiveChart.ChartTitle.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
  1986. */
  1987. oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(1);
  1988. oSeries.Border.ColorIndex = 45;
  1989. oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
  1990. oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(2);
  1991. oSeries.Border.ColorIndex = 9;
  1992. oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
  1993. }
  1994. #endregion
  1995. }
  1996. }