123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747 |
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration;
- using OTSIncAReportApp.DataOperation.DataAccess;
- using OTSIncAReportApp.OTSRstMgrFunction;
- using OTSIncAReportApp.OTSSampleReportInfo;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- namespace OTSIncAReportApp.Control_ECharts
- {
- /// <summary>
- /// 三元相图EChart版
- /// </summary>
- public partial class DEVChart_Trianglediagram : UserControl
- {
- #region 变量定义
- public List<string> m_list_string_sizelevel; //图例中:颗粒粒级字符列表
- public List<string> m_old_list_string_sizelevel;//图例中,颗粒粒级字符列表,未处理格式的,从底层拿出来的原始格式
- public List<string> m_list_string_samplename;//图例中:样品名字符列表
- private List<DataTable> TernaryData = new List<DataTable>();//当前三元图的数据
- private List<string> TernaryName = new List<string>();//三元图名称
- public string Template { get; set; }
- public int condition = -1;
- frmReportApp m_ReportApp;
- frmReportConditionChoose m_condition;
- ResultDataMgr m_DataMgr;
- private bool ReportExporting = false;
- public bool Report { set; get; }
- /// <summary>
- /// 判断颗粒范围(true全部颗粒,false选择颗粒)
- /// </summary>
- public bool ParticleRange { set; get; }
- #endregion
- #region 构造函数及窗体加载
- /// <summary>
- /// 三角图,构造函数
- /// </summary>
- /// <param name="in_sec">传入,委托类对象</param>
- /// <param name="in_Cotsreportprojfilemgrclr">传入,项目管理类对象</param>
- public DEVChart_Trianglediagram(frmReportApp ReportApp)
- {
- m_list_string_sizelevel = new List<string>();
- m_list_string_samplename = new List<string>();
- m_old_list_string_sizelevel = new List<string>();
- TernaryData = new List<DataTable>();
- m_ReportApp = ReportApp;
- m_condition = ReportApp.m_conditionChoose;
- m_DataMgr = ReportApp.m_rstDataMgr;
- InitializeComponent();
- }
-
- private void EChart_Trianglediagram_Load(object sender, EventArgs e)
- {
- List<DataTable> listDraw = new List<DataTable>();
- string template = Template;
- TernaryName.Clear();
- if (template == "")
- { //三元相图模板
- template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
- }
- //获取粒级表
- string pathtpf = Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
- List<string> nameList = new List<string>();
- DataSet ds = XMLoperate.GetXmlData(pathtpf, "XMLData");
- DataTable dt = ds.Tables["Member"];
- string TemplateName ="";
- //设置标签名称
- List<string> listName = new List<string>();
- //获取粒级表
- string pathe = m_DataMgr.m_RptConfigFile.PartSizeFileFolder + m_DataMgr.m_RptConfigFile.PartSizeFile;
- DataSet ds2 = XMLoperate.GetXml(pathe);
- string sizestr = ds2.Tables[0].Rows[0]["Sizes"].ToString();
- for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
- {
- if (sizestr.Split(',')[i].Length > 0)
- {
- double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
- double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
- listName.Add(d1.ToString() + "~" + d2.ToString());
- }
- }
- double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
- listName.Add(d.ToString() + "~MAX");
- Bitmap map = new Bitmap(950, 115);
- Graphics g = Graphics.FromImage(map);
- SolidBrush sbrush_White = new SolidBrush(Color.White);
- SolidBrush sbrush = new SolidBrush(Color.Black);
- g.FillRectangle(sbrush_White, 0, 0, 950, 115);
- Font myFont = new Font("Arial", 10, FontStyle.Regular);
- Font myFont2 = new Font("Arial", 10, FontStyle.Regular);
- Pen mypen = new Pen(Color.Black, 1);
- List<Color> Color_list = SetColor_list();
- string str_unit = "";
- int PositionJeight = 27;
- TernaryDiagram ternaryDiagram = new TernaryDiagram();
- for (int i = 0; i < listName.Count; i = i + 8)
- {
- if (i < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i]);
- ternaryDiagram.ShapeList(0, new PointF(10, PositionJeight), 10, Color_list[i], g);
- //g.DrawRectangle(mypen, 0, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 1, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i].ToString() + str_unit, myFont2, sbrush, 22, PositionJeight - 2);
- }
- if (i + 1 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 1]);
- ternaryDiagram.ShapeList(1, new PointF(104, PositionJeight), 10, Color_list[i+1], g);
- //g.DrawRectangle(mypen, 94, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 95, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 1].ToString() + str_unit, myFont2, sbrush, 116, PositionJeight - 2);
- }
- if (i + 2 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 2]);
- ternaryDiagram.ShapeList(2, new PointF(194, PositionJeight), 10, Color_list[i + 2], g);
- //g.DrawRectangle(mypen, 188, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 189, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 2].ToString() + str_unit, myFont2, sbrush, 206, PositionJeight - 2);
- }
- if (i + 3 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 3]);
- ternaryDiagram.ShapeList(3, new PointF(292, PositionJeight), 10, Color_list[i + 3], g);
- //g.DrawRectangle(mypen, 282, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 283, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 3].ToString() + str_unit, myFont2, sbrush, 304, PositionJeight - 2);
- }
- if (i + 4 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 4]);
- ternaryDiagram.ShapeList(0, new PointF(386, PositionJeight), 10, Color_list[i + 4], g);
- //g.DrawRectangle(mypen, 376, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 377, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 4].ToString() + str_unit, myFont2, sbrush, 398, PositionJeight - 2);
- }
- if (i + 5 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
- ternaryDiagram.ShapeList(1, new PointF(480, PositionJeight), 10, Color_list[i + 5], g);
- //g.DrawRectangle(mypen, 470, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 471, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 5].ToString() + str_unit, myFont2, sbrush, 492, PositionJeight - 2);
- }
- if (i + 6 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 6]);
- ternaryDiagram.ShapeList(2, new PointF(574, PositionJeight), 10, Color_list[i + 6], g);
- //g.DrawRectangle(mypen, 564, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 565, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 6].ToString() + str_unit, myFont2, sbrush, 586, PositionJeight - 2);
- }
- if (i + 7 < listName.Count)
- {
- //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 7]);
- ternaryDiagram.ShapeList(3, new PointF(668, PositionJeight), 10, Color_list[i + 7], g);
- //g.DrawRectangle(mypen, 658, PositionJeight, 8, 8);
- //g.FillRectangle(sbrush_Color, 659, PositionJeight + 1, 7, 7);
- g.DrawString(listName[i + 7].ToString() + str_unit, myFont2, sbrush, 680, PositionJeight - 2);
- }
- //if (i + 8 < listName.Count)
- //{
- // SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
- // g.DrawRectangle(mypen, 752, PositionJeight, 8, 8);
- // g.FillRectangle(sbrush_Color, 753, PositionJeight + 1, 7, 7);
- // g.DrawString(listName[i + 8].ToString() + str_unit, myFont2, sbrush, 762, PositionJeight - 2);
- //}
- //if (i + 9 < listName.Count)
- //{
- // SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
- // g.DrawRectangle(mypen, 846, PositionJeight, 8, 8);
- // g.FillRectangle(sbrush_Color, 847, PositionJeight + 1, 7, 7);
- // g.DrawString(listName[i + 9].ToString() + str_unit, myFont2, sbrush, 856, PositionJeight - 2);
- //}
- PositionJeight = PositionJeight + 15;
- }
- PictureBox pictureBox = new PictureBox();
- pictureBox.Size = new Size(950, 115);
- pictureBox.Location = new Point(40, 10);
- pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
- pictureBox.Image = map;
- this.panel1.Controls.Add(pictureBox);
- //遍历第一层节点
- foreach (DataRow element in dt.Rows)
- {
- string Name = element["TemplateName"].ToString();
- if (Name == template)
- {
- string Element = element["Element"].ToString();
- nameList.Add(Element.Split('.')[0]);
- nameList.Add(Element.Split('.')[1]);
- nameList.Add(Element.Split('.')[2]);
- TernaryName.Add(template.Split('.')[0]);
- TernaryName.Add(template.Split('.')[1]);
- TernaryName.Add(template.Split('.')[2]);
- TemplateName=Name;
- break;
- }
- }
- //将所有的测量结果名添加到list中
- m_list_string_samplename.Clear();
- TernaryData.Clear();
- string sou = "";
- int sel = 0;
- if (Report)
- {
- sou = m_DataMgr.ResultFilesList[m_DataMgr.GetWorkingResultId()].anotherFileName.ToString();
- if(ParticleRange)
- {
- sel = 0;
- }
- else
- {
- sel = 1;
- }
- }
- else
- {
- sou = m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
- //判断颗粒范围(全部颗粒或者选择颗粒)
- sel = m_ReportApp.m_conditionChoose.m_conditionData.GetComboDownListIndexByItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
- }
- Report = false;
- if (sou.Contains("+"))
- {
- if (m_ReportApp.more)
- {
- m_ReportApp.trans = false;
- }
- else
- {
- m_ReportApp.trans = true;
- }
- for (int i = 0; i < sou.Split('+').Length; i++)
- {
- ResultFile resultFile = m_DataMgr.ResultFilesList.Find(s => s.anotherFileName == sou.Split('+')[i]);
- if (resultFile == null)
- {
- continue;
- }
- DataTable cl = GettriangleJsonClass(resultFile.FilePath, resultFile.anotherFileName, nameList,sel);
- listDraw.Add(cl);
- }
- }
- else
- {
- if (!m_ReportApp.more)
- {
- m_ReportApp.trans = false;
- }
- else
- {
- m_ReportApp.trans = true;
- }
- m_ReportApp.more = false;
- ResultFile resultFile;
- int index = (int)m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemVal;
- if (ReportExporting)
- {
- resultFile = m_DataMgr.ResultFilesList[m_DataMgr.GetWorkingResultId()];
- }
- else
- {
- resultFile = m_DataMgr.ResultFilesList[index];
- }
- ReportExporting = false;
- DataTable cl = GettriangleJsonClass(resultFile.FilePath, resultFile.anotherFileName, nameList, sel);
- listDraw.Add(cl);
- }
- int point = 40;
- for (int i=0;i<listDraw.Count;i++)
- {
- Bitmap maps = Plot(listDraw[i], TemplateName, Color_list);
- PictureBox picture = new PictureBox();
- picture.Size = new Size(530, 530);
- picture.Location = new Point(point, 125);
- picture.SizeMode = PictureBoxSizeMode.StretchImage;
- picture.Image = maps;
- this.panel1.Controls.Add(picture);
- point = point + 570;
- }
- }
- /// <summary>
- /// 颜色列表
- /// </summary>
- /// <returns></returns>
- private List<Color> SetColor_list()
- {
- List<Color> Color_list = new List<Color>();
- Color_list.Add(FromHex("#0f4471"));
- Color_list.Add(FromHex("#FFD700"));
- Color_list.Add(FromHex("#ff5722"));
- Color_list.Add(FromHex("#5628b4"));
- Color_list.Add(FromHex("#20BF55"));
- Color_list.Add(FromHex("#f23557"));
- Color_list.Add(FromHex("#118df0"));
- Color_list.Add(FromHex("#FF00FF"));
- Color_list.Add(FromHex("#d3327b"));
- Color_list.Add(FromHex("#ae318a"));
- Color_list.Add(FromHex("#993090"));
- Color_list.Add(FromHex("#6f3071"));
- Color_list.Add(FromHex("#7711AF"));
- Color_list.Add(FromHex("#CF77FF"));
- Color_list.Add(FromHex("#AE004F"));
- Color_list.Add(FromHex("#F35872"));
- Color_list.Add(FromHex("#FA7729"));
- Color_list.Add(FromHex("#FFC526"));
- Color_list.Add(FromHex("#F8E71C"));
- Color_list.Add(FromHex("#34ADAE"));
- Color_list.Add(FromHex("#3DDFD2"));
- Color_list.Add(FromHex("#A0FFFF"));
- return Color_list;
- }
- /// <summary>
- /// 绘制图形
- /// </summary>
- /// <param name="dt_point_sort">数据</param>
- /// <param name="TemplateName">表名称</param>
- /// <param name="Color_list">颜色列</param>
- /// <returns></returns>
- private Bitmap Plot(DataTable dt_point_sort, string TemplateName, List<Color> Color_list)
- {
- int Line = 500;
- Bitmap map = new Bitmap(530, 530);
- Graphics g = Graphics.FromImage(map);
- SolidBrush sbrush_White = new SolidBrush(Color.White);
- g.FillRectangle(sbrush_White, 0, 0, 530, 530);
- Pen mypen = new Pen(Color.Black, 1);
- g.DrawImage(map, 0, 0, map.Width, map.Height);
- g.DrawLine(mypen, new Point(Line + 10, Line), new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))));//"\"
- g.DrawLine(mypen, new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))), new Point(0 + 10, Line));//"/"
- g.DrawLine(mypen, new Point(0 + 10, Line), new Point(Line + 10, Line));//"_"
- mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
- mypen.DashPattern = new float[] { 2, 2 };
- mypen = new Pen(Color.FromArgb(212, 212, 212), 1);
- int point = Line / 10;
- for (int i = 1; i < 10; i++)
- {
- //"\"
- int x1_a = point / 2 + ((point / 2) * (i - 1));
- int x2_a = Line - (43 * i);
- int y1_a = point * i;
- g.DrawLine(mypen, new Point(x1_a + 10, x2_a), new Point(y1_a + 10, Line));
- //"/"
- int x1_b = Line - (point / 2 + ((point / 2) * (i - 1)));
- int x2_b = Line - (43 * i);
- int y1_b = Line - (point * i);
- g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(y1_b + 10, Line));
- //"_"
- int x1_c = point / 2 + ((point / 2) * (i - 1));
- int x2_c = Line - (43 * i);
- int y1_c = Line - (point / 2 + ((point / 2) * (i - 1)));
- int y2_c = Line - (43 * i);
- g.DrawLine(mypen, new Point(x1_c + 10, x2_c), new Point(y1_c + 10, y2_c));
- g.DrawLine(mypen, new Point(y1_a + 10, Line + 10), new Point(y1_a + 10, Line));
- g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(x1_b + 10 + 10, x2_b));
- g.DrawLine(mypen, new Point(x1_c + 10 - 10, x2_c), new Point(y1_c + 10, y2_c));
- g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_c - 20, x2_c - 5));
- g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(y1_b, Line + 8));
- g.DrawString(((10 - i) * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_b + 20, x2_b - 5));
- }
- mypen = new Pen(Color.Black, 1);
- TernaryDiagram ternaryDiagram = new TernaryDiagram();
- int order = 1;
- HashSet<int> hashSet = new HashSet<int>();
-
- for (int i = 0; i < dt_point_sort.Rows.Count; i++)
- {
- int pos = Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"]);
- if (pos > 3)
- {
- order = pos % 4;
- }
- else
- {
- order = pos;
- }
- PointF pointF = new PointF(Convert.ToInt32(dt_point_sort.Rows[i]["X"])+10, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]));
- ternaryDiagram.ShapeList(order, pointF, 10, Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])], g);
- //SolidBrush mysbrush = new SolidBrush(Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])]);
- //g.FillRectangle(mysbrush, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 8, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 4, 5);
- //g.DrawRectangle(mypen, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 7, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 5, 5);
- }
- Font myFont = new Font("Arial Unicode MS", 13, FontStyle.Bold);
- Font Font_features = new Font("Arial Unicode MS", 10, FontStyle.Bold);
- SolidBrush sbrush = new SolidBrush(Color.Black);
- g.DrawString("features:" + dt_point_sort.Rows.Count.ToString(), Font_features, sbrush, 400, 200);
- g.DrawString(TemplateName, myFont, sbrush, 265 - (TemplateName.Length * 10 / 2), 10);
- g.DrawString(TemplateName.Split('.')[0].ToString(), myFont, sbrush, 265 - (TemplateName.Split('.')[0].ToString().Length * 10 / 2), 45);//顶点名字
- g.DrawString(TemplateName.Split('.')[1].ToString(), myFont, sbrush, 0, 510);//左侧名字
- g.DrawString(TemplateName.Split('.')[2].ToString(), myFont, sbrush, 500 - (TemplateName.Split('.')[0].ToString().Length * 10), 510);//左侧名字
- #endregion
- return map;
- }
- public static Color FromHex(string hex)
- {
- hex = hex.Replace("#", ""); // 移除 # 前缀
- byte a = 255; // 默认Alpha值为255(不透明)
- byte r = Convert.ToByte(hex.Substring(0, 2), 16);
- byte g = Convert.ToByte(hex.Substring(2, 2), 16);
- byte b = Convert.ToByte(hex.Substring(4, 2), 16);
- return Color.FromArgb(a, r, g, b);
- }
-
- private DataTable GettriangleJsonClass(string path, string name, List<string> nameList, int sel)
- {
- DataTable data = new DataTable();
- data.Columns.Add("A");
- data.Columns.Add("B");
- data.Columns.Add("C");
- data.Columns.Add("DMAX");
- data.Columns.Add("DMIN");
- DataTable runData = new DataTable();
- runData.Columns.Add("X");
- runData.Columns.Add("Y");
- runData.Columns.Add("C");
- runData.Columns.Add("Color_position");
- List<string> list_string_sort = new List<string>();
- List<double[]> list_point_sort = new List<double[]>();
- DataTable particles = GetParticles(path, nameList, sel);
- foreach (DataRow item in particles.Rows)
- {
- if (item["particleLocation"].ToString() == "0,0,0")
- {
- continue;
- }
- list_string_sort.Add(item["sizeLevel"].ToString());
- double top = Convert.ToDouble(item["top"]);
- double left = Convert.ToDouble(item["left"]);
- double right = Convert.ToDouble(item["right"]);
- //x=right+top/2,y=(√3/2)*top
- double Y = 0.866 * top * 500;
- double X = (right + top / 2) * 500;
- double[] point = { Math.Round(X, 2), Math.Round(Y) };
- list_point_sort.Add(point);
- DataRow dr = data.NewRow();
- dr["A"] = top.ToString();
- dr["B"] = left.ToString();
- dr["C"] = right.ToString();
- dr["DMAX"] = item["DMAX"].ToString();
- dr["DMIN"] = item["DMIN"].ToString();
- data.Rows.Add(dr);
- DataRow runDR = runData.NewRow();
- runDR["X"] = Math.Round(X);
- runDR["Y"] = Math.Round(Y);
- runDR["Color_position"] = item["Color_position"].ToString();
- runData.Rows.Add(runDR);
- }
- TernaryData.Add(data);
- return runData;
- }
- /// <summary>
- /// 导出Excel数据
- /// </summary>
- private void ExportExcelDocument()
- {
- // 创建一个SaveFileDialog实例
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- // 设置对话框的标题
- saveFileDialog.Title = "选择Excel文件保存位置";
- // 设置默认的文件扩展名为.xlsx
- saveFileDialog.DefaultExt = "xlsx";
- // 设置过滤器,限制用户可以选择的文件类型为Excel文件
- saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx";
- // 显示对话框并检查用户是否点击了“保存”按钮
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- // 获取用户选择的文件路径
- string filePath = saveFileDialog.FileName;
- // 调用导出方法
- ExportDataTableToExcel(TernaryData, filePath);
- }
- }
- //获取颗粒信息
- private DataTable GetParticles(string filepath, List<string> nameList,int sel)
- {
- ParticleData particledata = new ParticleData(filepath);
- //设置标签名称
- List<string> listName = new List<string>();
- //获取粒级表
- string pathe = m_DataMgr.m_RptConfigFile.PartSizeFileFolder + m_DataMgr.m_RptConfigFile.PartSizeFile;
- DataSet ds = XMLoperate.GetXml(pathe);
- string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
- for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
- {
- if (sizestr.Split(',')[i].Length > 0)
- {
- double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
- double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
- listName.Add(d1.ToString() + "~" + d2.ToString());
- }
- }
- double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
- listName.Add(d.ToString() + "~MAX");
- string filedAndParticl = "";
- string po = "";
- string con = "";
- int row = m_condition.m_conditionData.ConditionItemList.Count;
- if (condition != -1)
- {
- List<string> li = new List<string>() { "DMAX", "DMIN", "Area", "FERET" };
- con = li[condition];
- }
- else
- {
- if (row < 4)
- {
- con = "DMAX";
- }
- else
- {
- con = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName( OTS_REPORT_PROP_GRID_ITEMS.SIZE_CAL_METHOD_TYPE).ToString();
- }
- }
- switch (con)
- {
- case "DMAX":
- po = "DMAX";
- break;
- case "DMIN":
- po = "DMIN";
- break;
- case "ECD":
- po = "Area";
- break;
- case "FERET":
- po = "DFERET";
- break;
- }
- //particledata.GetParticleAllHaveXray("");
- //DataTable dtp = particledata.GetParticleHaveXray(filedAndParticl);//旧
- DataTable dtp = particledata.GetParticleAllHaveXray(filedAndParticl);//新
- //创建一个临时表
- DataTable ret_dt = new DataTable();
- //然后额外存放三列,用于存放拆分后三个顶点的值
- ret_dt.Columns.Add("sizeLevel");
- ret_dt.Columns.Add("particleLocation");
- ret_dt.Columns.Add("top");
- ret_dt.Columns.Add("left");
- ret_dt.Columns.Add("right");
- ret_dt.Columns.Add("Color_position");
- ret_dt.Columns.Add("DMAX");
- ret_dt.Columns.Add("DMIN");
- for (int k = 0; k < listName.Count; k++)
- {
- string str = listName[k];
- string d1 = str.Split('~')[0];
- string d2 = str.Split('~')[1];
- if (d2 == "MAX")
- {
- d2 = "999";
- }
- DataRow[] datas = dtp.Select(getWhere(d2, d1, po));
- foreach (var item in datas)
- {
- DataRow dr = ret_dt.NewRow();
- dr["sizeLevel"] = str;
- string element = item["Element"].ToString();
- string strRet = getStrRet(nameList, element);
- dr["particleLocation"] = strRet;
- dr["top"] = strRet.Split(',')[0];
- dr["left"] = strRet.Split(',')[1];
- dr["right"] = strRet.Split(',')[2];
- dr["Color_position"] = k.ToString();
- string strDMAX = item["DMAX"].ToString();
- string strDMIN = item["DMIN"].ToString();
- dr["DMAX"] = strDMAX;
- dr["DMIN"] = strDMIN;
- ret_dt.Rows.Add(dr);
- }
- }
- return ret_dt;
- }
- private string getWhere(string max, string min, string col)
- {
- return col + ">=" + min + " and " + col + "<=" + max;
- }
- private string getStrRet(List<string> template, string element)
- {
- List<string> name = new List<string>()
- { "h","he",
- "li","be","b","c","n","o","f","ne",
- "na","mg","al","si","p","s","cl","ar",
- "k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
- "rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe",
- "cs","ba","la",
- "ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
- "hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
- "fr","ra","ac",
- "th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr"
- };
- List<double> value = new List<double>()
- { 1.008,4.003,
- 6.941,9.012,10.811,12.011,14.007,15.999,18.998,20.180,
- 22.990,24.305,26.982,28.086,30.974,32.066,35.453,39.948,
- 39.098,40.08,44.956,47.88,50.942,51.996,54.938,55.847,58.933,58.70,63.546,65.39,69.72,72.61,74.922,78.96,79.904,83.80,
- 85.468,87.62,88.906,91.22,92.906,95.94,98.00,101.07,102.906,106.42,107.868,112.41,114.82,118.71,121.76,127.60,126.905,131.29,
- 132.905,137.33,138.906,
- 140.12,140.908,144.24,145.0,150.36,151.97,157.25,158.925,162.50,64.93,167.26,168.934,173.04,174.967,
- 178.49,180.948,183.85,186.207,190.20,192.22,195.08,196.967,200.59,204.38,207.2,208.980,209.00,210.00,222.00,
- 223.00,226.025,227.028,
- 232.038,231.036,238.029,237.048,244.00,243.00,247.00,247.00,251.00,252.00,257.00,258.00,259.00,260.00
- };
- double d_ASum = 0;
- double d_BSum = 0;
- double d_CSum = 0;
- string aElements = template[0];
- string bElements = template[1];
- string cElements = template[2];
- foreach (var item in element.Split(';'))
- {
- if (item.Contains(aElements + "-"))
- {
- if (aElements != "")
- {
- d_ASum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(aElements.ToLower())];
- }
- }
- for (int i = 0; i < bElements.Split(',').Length; i++)
- {
- string e = bElements.Split(',')[i];
- if (item.Contains(e + "-"))
- {
- d_BSum = d_BSum + Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(e.ToLower())];
- }
- }
- if (item.Contains(cElements + "-"))
- {
- if (cElements != "")
- d_CSum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(cElements.ToLower())];
- }
- }
- double allNums = d_ASum + d_BSum + d_CSum;
- string strRet = "";
- if ((allNums) == 0)
- {
- strRet = "0,0,0";
- }
- else
- {
- double aPercent = 0, bPercent = 0, cPercent = 0;
- if (d_ASum != 0)
- {
- aPercent = d_ASum / allNums;
- }
- if (d_BSum != 0)
- {
- bPercent = d_BSum / allNums;
- }
- if (d_CSum != 0)
- {
- cPercent = d_CSum / allNums;
- }
- strRet = aPercent.ToString() + "," + bPercent.ToString() + "," + cPercent.ToString();
- }
- return strRet;
- }
- private void ExportDataTableToExcel(List<DataTable> dataTable, string filePath)
- {
- // 创建一个新的工作簿
- IWorkbook workbook = new XSSFWorkbook(); // 对于.xlsx格式
- // IWorkbook workbook = new HSSFWorkbook(); // 对于.xls格式
- for (int a=0;a< dataTable.Count;a++)
- {
- // 添加一个新的工作表
- ISheet sheet = workbook.CreateSheet("Sheet"+(a+1).ToString());
- // 创建表头行(第一行)
- IRow headerRow = sheet.CreateRow(0);
- ICell cell1 = headerRow.CreateCell(0);
- cell1.SetCellValue(TernaryName[0].ToString());
- ICell cell2 = headerRow.CreateCell(1);
- cell2.SetCellValue(TernaryName[1].ToString());
- ICell cell3 = headerRow.CreateCell(2);
- cell3.SetCellValue(TernaryName[2].ToString());
- ICell cell4 = headerRow.CreateCell(3);
- cell4.SetCellValue("DMAX");
- ICell cell5 = headerRow.CreateCell(4);
- cell5.SetCellValue("DMIN");
- // 填充数据行
- for (int i = 0; i < dataTable[a].Rows.Count; i++)
- {
- IRow row = sheet.CreateRow(i + 1);
- for (int j = 0; j < dataTable[a].Columns.Count; j++)
- {
- ICell cell = row.CreateCell(j);
- if (dataTable[a].Rows[i][j] != DBNull.Value)
- {
- cell.SetCellValue(dataTable[a].Rows[i][j].ToString());
- }
- }
- }
- // 自动调整列宽(可选)
- // 注意:NPOI的AutoSizeColumn方法不如EPPlus的AutoFitColumns方法那样精确
- // 你可能需要手动设置列宽或者接受默认的宽度
- // 但以下是一个尝试自动调整列宽的示例,它可能不完全符合你的期望
- for (int i = 0; i < dataTable[a].Columns.Count; i++)
- {
- sheet.AutoSizeColumn(i);
- // 或者,你可以设置一个固定的宽度(以字符为单位)
- // sheet.SetColumnWidth(i, 20 * 256); // 20是字符宽度,256是转换因子
- }
- }
- // 保存Excel文件
- using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
- {
- workbook.Write(fileStream);
- }
- // 释放资源(对于.xlsx格式,通常不需要显式调用Dispose,因为XSSFWorkbook实现了IDisposable但内部已经处理了资源释放)
- // 但为了良好的编程习惯,你可以显式调用(尽管这里不是必需的)
- // workbook.Dispose(); // 如果你使用的是实现了IDisposable的对象,并且想确保资源被释放
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- ExportExcelDocument();
- }
- }
- }
|