ParticlesGridDevidePage.cs 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  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. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd = new OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation();
  1599. ////将所有的数据导出到EXCEL中
  1600. //SaveFileDialog sfd = new SaveFileDialog();
  1601. //sfd.Filter = "Excel File(*.xlsx)|*.xlsx";
  1602. ////设置默认文件类型显示顺序
  1603. //sfd.FilterIndex = 1;
  1604. ////保存对话框是否记忆上次打开的目录
  1605. //sfd.RestoreDirectory = true;
  1606. if (sfd.ShowDialog() == DialogResult.OK)
  1607. {
  1608. //IWorkbook workbook = new HSSFWorkbook(); //用于创建.xls office2003开始以前的
  1609. IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(); //用于创建.xlsx office2007开始以后的
  1610. ISheet sheet;
  1611. //创建Excel文件
  1612. FileStream fs = File.Create(sfd.FileName);
  1613. fs.Close();
  1614. sheet = workbook.CreateSheet("Particles");//创建工作表
  1615. //创建表格边框样式风格
  1616. ICellStyle cellStyle = workbook.CreateCellStyle();
  1617. cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  1618. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1619. //设置上4边
  1620. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1621. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1622. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1623. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1624. //设置字符大小和颜色
  1625. IFont font = workbook.CreateFont();
  1626. font.FontName = "黑体";
  1627. font.FontHeightInPoints = 10;
  1628. cellStyle.SetFont(font);
  1629. //设置颗粒名列宽
  1630. sheet.SetColumnWidth(1, 30 * 256);//夹杂物名列宽
  1631. sheet.SetColumnWidth(2, 7 * 256);//图像列宽
  1632. IRow row;
  1633. ICell cell;
  1634. //插入表头
  1635. row = sheet.CreateRow(1);//从第15行添加一行
  1636. row.Height = 30 * 20;
  1637. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1638. {
  1639. cell = row.CreateCell(i_cell);
  1640. cell.CellStyle = cellStyle;
  1641. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i_cell].Name);
  1642. }
  1643. //插入表内容
  1644. for (int i_row = 0; i_row < dgV_ParticlesDevidePage.Rows.Count; i_row++)
  1645. {
  1646. row = sheet.CreateRow(2 + i_row);
  1647. row.Height = 45 * 20;
  1648. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1649. {
  1650. cell = row.CreateCell(i_cell);
  1651. cell.CellStyle = cellStyle;
  1652. if (dgV_ParticlesDevidePage[i_cell, i_row].Value == null)
  1653. continue;
  1654. if (i_cell == 2)
  1655. {
  1656. //颗粒图像列
  1657. Bitmap bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i_row].Cells[2].Value;
  1658. byte[] bytes = ImageConvertToBytes(bp);
  1659. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  1660. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  1661. //第三步,在sheet中创建画布
  1662. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  1663. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  1664. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2, i_cell, i_row + 2, i_cell + 1, i_row + 3);//终止比开始位置大1,会自动缩放到一个单元格内的
  1665. //第五步,创建图片
  1666. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  1667. //图像效果不好,自己另外导出吧,这里对该列宽进行了设置0
  1668. }
  1669. //else if(i_cell==0)
  1670. //{
  1671. // ICreationHelper createHelper = workbook.GetCreationHelper();
  1672. // XSSFHyperlink link = (XSSFHyperlink)createHelper.CreateHyperlink(HyperlinkType.Url);
  1673. // link.Address=;
  1674. // cell.Hyperlink.Address(link);
  1675. //}
  1676. else
  1677. {
  1678. //非图像列
  1679. double dbl = 0;
  1680. if (double.TryParse(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString(), out dbl))
  1681. {
  1682. cell.SetCellValue(dbl);
  1683. }
  1684. else
  1685. {
  1686. cell.SetCellValue(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString());
  1687. }
  1688. }
  1689. }
  1690. }
  1691. ISheet sheet2;
  1692. sheet2 = workbook.CreateSheet("Peak spectrum Data");//创建工作表
  1693. //插入表头
  1694. row = sheet2.CreateRow(1);
  1695. row.Height = 30 * 20;
  1696. cell = row.CreateCell(0);
  1697. cell.CellStyle = cellStyle;
  1698. cell.SetCellValue("rowid");
  1699. for (int i_cell = 1; i_cell < 2001; i_cell++)
  1700. {
  1701. cell = row.CreateCell(i_cell);
  1702. cell.CellStyle = cellStyle;
  1703. if (i_cell == 1)
  1704. {
  1705. cell.SetCellValue("PeakSpectrumData");
  1706. }
  1707. }
  1708. sheet2.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 1, 2000));
  1709. for (int j = 0; j < particles.Rows.Count; j++)
  1710. {
  1711. row = sheet2.CreateRow(2 + j);
  1712. cell = row.CreateCell(0);
  1713. cell.CellStyle = cellStyle;
  1714. cell.SetCellValue(j + 1);
  1715. var tmpPart = Particledata.GetParticleXrayDataByFidAndPid(particles.Rows[j]["fieldid"].ToString(), particles.Rows[j]["particleid"].ToString());
  1716. if (tmpPart != null)
  1717. {
  1718. if (tmpPart.XrayId > -1)
  1719. {
  1720. uint[] Analysis_xray = new uint[2000];
  1721. for (int i = 0; i < 2000; i++)
  1722. {
  1723. Analysis_xray[i] = BitConverter.ToUInt32(tmpPart.XRayData, i * 4);
  1724. cell = row.CreateCell(i + 1);
  1725. cell.CellStyle = cellStyle;
  1726. cell.SetCellValue(Analysis_xray[i]);
  1727. }
  1728. }
  1729. }
  1730. }
  1731. if (sfd.SpectrumConfirmation)
  1732. {
  1733. ISheet sheet3;
  1734. sheet3 = workbook.CreateSheet("Peak spectrum");
  1735. sheet3.SetColumnWidth(1, 200 * 256);//图像列宽
  1736. //插入表头
  1737. row = sheet3.CreateRow(1);
  1738. row.Height = 30 * 20;
  1739. cell = row.CreateCell(0);
  1740. cell.CellStyle = cellStyle;
  1741. cell.SetCellValue("rowid");
  1742. cell = row.CreateCell(1);
  1743. cell.CellStyle = cellStyle;
  1744. cell.SetCellValue("PeakSpectrum");
  1745. IDrawing drawing;
  1746. IClientAnchor anchor3;
  1747. for (int j = 0; j < particles.Rows.Count; j++)
  1748. {
  1749. row = sheet3.CreateRow(j + 2);
  1750. row.Height = 120 * 20;
  1751. cell = row.CreateCell(0);
  1752. cell.CellStyle = cellStyle;
  1753. cell.SetCellValue(j + 1);
  1754. cell = row.CreateCell(1);
  1755. cell = row.CreateCell(2);
  1756. cell = row.CreateCell(3);
  1757. drawing = sheet3.CreateDrawingPatriarch();
  1758. //锚点
  1759. 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开始
  1760. CreateChart(drawing, sheet2, anchor3, j + 2);
  1761. }
  1762. //row = sheet3.CreateRow(2);
  1763. //row.Height = 120 * 20;
  1764. //cell = row.CreateCell(0);
  1765. //cell.CellStyle = cellStyle;
  1766. //cell.SetCellValue(1);
  1767. //cell = row.CreateCell(1);
  1768. //cell = row.CreateCell(2);
  1769. //cell = row.CreateCell(3);
  1770. //drawing = sheet3.CreateDrawingPatriarch();
  1771. ////锚点
  1772. //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开始
  1773. //CreateChart(drawing, sheet2, anchor3, 2);
  1774. //row = sheet3.CreateRow(3);
  1775. //row.Height = 120 * 20;
  1776. //cell = row.CreateCell(0);
  1777. //cell.CellStyle = cellStyle;
  1778. //cell.SetCellValue(1);
  1779. //cell = row.CreateCell(1);
  1780. //cell = row.CreateCell(2);
  1781. //cell = row.CreateCell(3);
  1782. //drawing = sheet3.CreateDrawingPatriarch();
  1783. ////锚点
  1784. //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开始
  1785. //CreateChart(drawing, sheet2, anchor3, 3);
  1786. }
  1787. //完成后,对Excel进行保存
  1788. FileStream file = new FileStream(sfd.FileName, FileMode.Create);
  1789. workbook.Write(file);
  1790. file.Close();
  1791. MessageBox.Show("Export complete!");
  1792. //导出完成后,打开Excel文件
  1793. if (File.Exists(sfd.FileName))
  1794. {
  1795. //打开刚才导出的文件
  1796. System.Diagnostics.Process.Start(sfd.FileName);
  1797. }
  1798. // 生成一个 单元格 对象 HSSFCell cellTableName = CreateCell(sheetTableIndex, t + 1, 0, strTableName, true);
  1799. //创建一个超链接对象 HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
  1800. // strTableName 这个参数为 sheet名字 A1 为单元格 其他是固定格式 link.Address = "#" + strTableName + "!A1";
  1801. //设置 cellTableName 单元格 的连接对象 cellTableName.Hyperlink = link;
  1802. }
  1803. }
  1804. void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, int rowid)
  1805. {
  1806. var chart = drawing.CreateChart(anchor) as XSSFChart;
  1807. // 创建图形注释的位置
  1808. //var legend = chart.GetOrCreateLegend();
  1809. //legend.Position = LegendPosition.Top;
  1810. //图表
  1811. var data = chart.ChartDataFactory.CreateLineChartData<double, double>(); //折线图
  1812. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
  1813. IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
  1814. leftAxis.Crosses=AxisCrosses.AutoZero;
  1815. leftAxis.IsVisible = true;
  1816. bottomAxis.IsVisible = true;
  1817. //数据源
  1818. IChartDataSource<double> ys = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(rowid, rowid, 1, 2001));
  1819. Double[] doubles = new Double[2000];
  1820. for (int i = 0; i < 2000; i++)
  1821. {
  1822. doubles[i] = i;
  1823. }
  1824. IChartDataSource<double> xs = DataSources.FromArray(doubles);
  1825. //IChartDataSource<double> ys = DataSources.FromArray(doubles);
  1826. //数据系列
  1827. var s1 = data.AddSeries(xs, ys);
  1828. // 开始绘制折线图
  1829. chart.Plot(data, bottomAxis, leftAxis);
  1830. }
  1831. /// <summary>
  1832. /// 将image转成bytes
  1833. /// </summary>
  1834. /// <param name="in_img"></param>
  1835. /// <returns></returns>
  1836. public byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  1837. {
  1838. MemoryStream ms = new MemoryStream();
  1839. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  1840. return ms.ToArray();
  1841. }
  1842. public bool CheckParamExist<T>(T Param)
  1843. {
  1844. if (Param == null)
  1845. {
  1846. return false;
  1847. }
  1848. else
  1849. {
  1850. return true;
  1851. }
  1852. }
  1853. private void ToolStripMenuItem_Delete_Particle_Click(object sender, EventArgs e)
  1854. {
  1855. string path = result.FilePath;
  1856. ParticleData fielddata = new ParticleData(result.FilePath);
  1857. List<Particle> particles = fielddata.GetParticleAllList();
  1858. List<Particle> ls_list_cotsparticleclr = new List<Particle>();
  1859. for (int l = 0; l < dgV_ParticlesDevidePage.SelectedRows.Count; l++)
  1860. {
  1861. //从事先加载的Bitmap对象的Tag中取出List<string>
  1862. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[l];
  1863. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  1864. List<string> list_str = (List<string>)ls_bp.Tag;
  1865. if (list_str != null)
  1866. {
  1867. int i_ls_fieldid = Convert.ToInt32(list_str[0]);
  1868. int i_ls_particleid = Convert.ToInt32(list_str[1]);
  1869. foreach (Particle item in particles)
  1870. {
  1871. if (i_ls_fieldid == item.FieldId && i_ls_particleid == item.ParticleId)
  1872. {
  1873. if (ReportFun.DeleteParticlesFromLibrary(item.ParticleId, item.FieldId))
  1874. {
  1875. MessageBox.Show("Deleted successfully");
  1876. for (int i = 0; i < particlesAll.Rows.Count; i++)
  1877. {
  1878. if (particlesAll.Rows[i]["Fieldid"].ToString() == item.FieldId.ToString() && particlesAll.Rows[i]["Particleid"].ToString() == item.ParticleId.ToString())
  1879. {
  1880. particlesAll.Rows.RemoveAt(i);
  1881. }
  1882. }
  1883. DisplayData();
  1884. }
  1885. else
  1886. {
  1887. MessageBox.Show("Delete failed");
  1888. }
  1889. break;
  1890. }
  1891. }
  1892. }
  1893. }
  1894. }
  1895. #region 将DataTable中的数据写到Excel的指定Sheet中
  1896. /// <summary>
  1897. /// 将DataTable中的数据写到Excel的指定Sheet中
  1898. /// </summary>
  1899. /// <param name="dt"></param>
  1900. /// <param name="m_Sheet"></param>
  1901. public void DataTableToSheet(string title, DataTable dt, Microsoft.Office.Interop.Excel._Worksheet m_Sheet,
  1902. Microsoft.Office.Interop.Excel._Workbook m_Book, int startrow)
  1903. {
  1904. //以下是填写EXCEL中数据
  1905. Microsoft.Office.Interop.Excel.Range range = m_Sheet.get_Range(m_Sheet.Cells[1, 1], m_Sheet.Cells[1, 2]);
  1906. range.MergeCells = true; //合并单元格
  1907. range.Font.Bold = true; //加粗单元格内字符
  1908. //写入题目
  1909. m_Sheet.Cells[startrow, startrow] = title;
  1910. int rownum = dt.Rows.Count;//行数
  1911. int columnnum = dt.Columns.Count;//列数
  1912. int num = rownum + 2; //得到数据中的最大行数
  1913. //写入列标题
  1914. for (int j = 0; j < columnnum; j++)
  1915. {
  1916. int bt_startrow = startrow + 1;
  1917. //将字段名写入文档
  1918. m_Sheet.Cells[bt_startrow, 1 + j] = dt.Columns[j].ColumnName;
  1919. //单元格内背景色
  1920. m_Sheet.get_Range(m_Sheet.Cells[bt_startrow, 1 + j], m_Sheet.Cells[bt_startrow, 1 + j]).Interior.ColorIndex = 15;
  1921. }
  1922. //逐行写入数据
  1923. for (int i = 0; i < rownum; i++)
  1924. {
  1925. for (int j = 0; j < columnnum; j++)
  1926. {
  1927. m_Sheet.Cells[startrow + 2 + i, 1 + j] = dt.Rows[i][j].ToString();
  1928. }
  1929. }
  1930. m_Sheet.Columns.AutoFit();
  1931. //在当前工作表中根据数据生成图表
  1932. CreateChart(m_Book, m_Sheet, num);
  1933. }
  1934. private void CreateChart(Microsoft.Office.Interop.Excel._Workbook m_Book, Microsoft.Office.Interop.Excel._Worksheet m_Sheet, int num)
  1935. {
  1936. Microsoft.Office.Interop.Excel.Range oResizeRange;
  1937. Microsoft.Office.Interop.Excel.Series oSeries;
  1938. m_Book.Charts.Add(Missing.Value, Missing.Value, 1, Missing.Value);
  1939. m_Book.ActiveChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;//设置图形
  1940. //设置数据取值范围
  1941. m_Book.ActiveChart.SetSourceData(m_Sheet.get_Range("A2", "C" + num.ToString()), Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
  1942. //m_Book.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAutomatic, title);
  1943. //以下是给图表放在指定位置
  1944. m_Book.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, m_Sheet.Name);
  1945. oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Rows.get_Item(10, Missing.Value);
  1946. m_Sheet.Shapes.Item("Chart 1").Top = (float)(double)oResizeRange.Top; //调图表的位置上边距
  1947. oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Columns.get_Item(6, Missing.Value); //调图表的位置左边距
  1948. // m_Sheet.Shapes.Item("Chart 1").Left = (float)(double)oResizeRange.Left;
  1949. m_Sheet.Shapes.Item("Chart 1").Width = 400; //调图表的宽度
  1950. m_Sheet.Shapes.Item("Chart 1").Height = 250; //调图表的高度
  1951. m_Book.ActiveChart.PlotArea.Interior.ColorIndex = 19; //设置绘图区的背景色
  1952. m_Book.ActiveChart.PlotArea.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置绘图区边框线条
  1953. m_Book.ActiveChart.PlotArea.Width = 400; //设置绘图区宽度
  1954. //m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; //设置整个图表的背影颜色
  1955. //m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;// 设置整个图表的边框颜色
  1956. m_Book.ActiveChart.ChartArea.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置边框线条
  1957. m_Book.ActiveChart.HasDataTable = false;
  1958. //设置Legend图例的位置和格式
  1959. m_Book.ActiveChart.Legend.Top = 20.00; //具体设置图例的上边距
  1960. m_Book.ActiveChart.Legend.Left = 60.00;//具体设置图例的左边距
  1961. m_Book.ActiveChart.Legend.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexNone;
  1962. m_Book.ActiveChart.Legend.Width = 150;
  1963. m_Book.ActiveChart.Legend.Font.Size = 9.5;
  1964. //m_Book.ActiveChart.Legend.Font.Bold = true;
  1965. m_Book.ActiveChart.Legend.Font.Name = "宋体";
  1966. //m_Book.ActiveChart.Legend.Position = Microsoft.Office.Interop.Excel.XlLegendPosition.xlLegendPositionTop;//设置图例的位置
  1967. m_Book.ActiveChart.Legend.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置图例边框线条
  1968. //设置X轴的显示
  1969. 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);
  1970. xAxis.MajorGridlines.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlDot;
  1971. xAxis.MajorGridlines.Border.ColorIndex = 1;//gridLine横向线条的颜色
  1972. xAxis.HasTitle = false;
  1973. xAxis.MinimumScale = 1500;
  1974. xAxis.MaximumScale = 6000;
  1975. xAxis.TickLabels.Font.Name = "宋体";
  1976. xAxis.TickLabels.Font.Size = 9;
  1977. //设置Y轴的显示
  1978. 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);
  1979. yAxis.TickLabelSpacing = 30;
  1980. yAxis.TickLabels.NumberFormat = "M月D日";
  1981. yAxis.TickLabels.Orientation = Microsoft.Office.Interop.Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平还是垂直等
  1982. yAxis.TickLabels.Font.Size = 8;
  1983. yAxis.TickLabels.Font.Name = "宋体";
  1984. //m_Book.ActiveChart.Floor.Interior.ColorIndex = 8;
  1985. /***以下是设置标题*****
  1986. m_Book.ActiveChart.HasTitle=true;
  1987. m_Book.ActiveChart.ChartTitle.Text = "净值指数";
  1988. m_Book.ActiveChart.ChartTitle.Shadow = true;
  1989. m_Book.ActiveChart.ChartTitle.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
  1990. */
  1991. oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(1);
  1992. oSeries.Border.ColorIndex = 45;
  1993. oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
  1994. oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(2);
  1995. oSeries.Border.ColorIndex = 9;
  1996. oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
  1997. }
  1998. #endregion
  1999. }
  2000. }