Parcourir la source

报告程序新增三元图导出数据功能

zhangjiaxin il y a 4 mois
Parent
commit
994e4a25ad

+ 27 - 1
OTSIncAReportApp/1-UI/Control_ECharts/EChart_Trianglediagram.Designer.cs

@@ -30,6 +30,9 @@
         {
             this.components = new System.ComponentModel.Container();
             this.timer1 = new System.Windows.Forms.Timer(this.components);
+            this.panel1 = new System.Windows.Forms.Panel();
+            this.button1 = new System.Windows.Forms.Button();
+            this.panel1.SuspendLayout();
             this.SuspendLayout();
             // 
             // timer1
@@ -37,14 +40,35 @@
             this.timer1.Interval = 2000;
             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
             // 
+            // panel1
+            // 
+            this.panel1.Controls.Add(this.button1);
+            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
+            this.panel1.Location = new System.Drawing.Point(0, 0);
+            this.panel1.Name = "panel1";
+            this.panel1.Size = new System.Drawing.Size(946, 655);
+            this.panel1.TabIndex = 0;
+            // 
+            // button1
+            // 
+            this.button1.Location = new System.Drawing.Point(1150, 800);
+            this.button1.Name = "button1";
+            this.button1.Size = new System.Drawing.Size(75, 23);
+            this.button1.TabIndex = 0;
+            this.button1.Text = "导出数据";
+            this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click_1);
+            // 
             // EChart_Trianglediagram
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.Controls.Add(this.panel1);
             this.Margin = new System.Windows.Forms.Padding(2);
             this.Name = "EChart_Trianglediagram";
-            this.Size = new System.Drawing.Size(773, 477);
+            this.Size = new System.Drawing.Size(946, 655);
             this.Load += new System.EventHandler(this.EChart_Trianglediagram_Load);
+            this.panel1.ResumeLayout(false);
             this.ResumeLayout(false);
 
         }
@@ -52,5 +76,7 @@
         #endregion
 
         private System.Windows.Forms.Timer timer1;
+        private System.Windows.Forms.Panel panel1;
+        private System.Windows.Forms.Button button1;
     }
 }

+ 135 - 15
OTSIncAReportApp/1-UI/Control_ECharts/EChart_Trianglediagram.cs

@@ -1,7 +1,8 @@
 using CefSharp;
 using CefSharp.WinForms;
 using Newtonsoft.Json;
-
+using NPOI.SS.UserModel;
+using NPOI.XSSF.UserModel;
 using OTSIncAReportApp.DataOperation.DataAccess;
 using OTSIncAReportApp.OTSRstMgrFunction;
 using OTSIncAReportApp.OTSSampleReportInfo;
@@ -25,6 +26,8 @@ namespace OTSIncAReportApp.Control_ECharts
         public List<string> m_old_list_string_sizelevel;//图例中,颗粒粒级字符列表,未处理格式的,从底层拿出来的原始格式
         public List<string> m_list_string_samplename;//图例中:样品名字符列表
 
+        private DataTable TernaryData = new DataTable();//当前三元图的数据
+        private List<string> TernaryName = new List<string>();//三元图名称
         public string Template { get; set; }
 
         public int condition = -1;
@@ -85,7 +88,15 @@ namespace OTSIncAReportApp.Control_ECharts
         }
         private void EChart_Trianglediagram_Load(object sender, EventArgs e)
         {
+            TernaryData.Columns.Add("A");
+            TernaryData.Columns.Add("B");
+            TernaryData.Columns.Add("C");
+            TernaryData.Columns.Add("DMAX");
+            TernaryData.Columns.Add("DMIN");
+
+
             string template = Template;
+            TernaryName.Clear();
             if (template == "")
             { //三元相图模板
                 template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
@@ -108,10 +119,17 @@ namespace OTSIncAReportApp.Control_ECharts
                     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]);
                     break;
                 }
             }
-
+            //for (int i=0;i< nameList.Count;i++)
+            //{
+            //    TernaryName.Add(nameList[i]);
+            //}
             //将所有的测量结果名添加到list中
             m_list_string_samplename.Clear();
             string sou = "";
@@ -186,9 +204,6 @@ namespace OTSIncAReportApp.Control_ECharts
                 ReportExporting = false;
                 TriangleJsonClass cl = GettriangleJsonClass(resultFile.FilePath, resultFile.anotherFileName, nameList, TemplateName,sel);
 
-
-          
-
                 m_list_trianglejsonclass.Add(cl);
             }
 
@@ -215,12 +230,12 @@ namespace OTSIncAReportApp.Control_ECharts
             }
             InitBrowser();
 
-
-
         }
 
         private TriangleJsonClass GettriangleJsonClass(string path, string name, List<string> nameList, string templatename,int sel)
         {
+            TernaryData.Rows.Clear();
+
             TriangleJsonClass triangleJsonClass = new TriangleJsonClass();
 
             m_list_string_samplename.Add(name);
@@ -244,6 +259,14 @@ namespace OTSIncAReportApp.Control_ECharts
                 double X = (right + top / 2) * 100;
                 double[] point = { Math.Round(X, 2), Math.Round(Y) };
                 list_point_sort.Add(point);
+
+                DataRow dr = TernaryData.NewRow();
+                dr["A"] = top.ToString();
+                dr["B"] = left.ToString();
+                dr["C"] = right.ToString();
+                dr["DMAX"] = item["DMAX"].ToString();
+                dr["DMIN"] = item["DMIN"].ToString();
+                TernaryData.Rows.Add(dr);
             }
 
             triangleJsonClass = new TriangleJsonClass()
@@ -258,12 +281,31 @@ namespace OTSIncAReportApp.Control_ECharts
             return triangleJsonClass;
         }
 
+        /// <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)
@@ -287,9 +329,6 @@ namespace OTSIncAReportApp.Control_ECharts
             }
             double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
             listName.Add(d.ToString() + "~MAX");
-            //int sel = m_ReportApp.m_conditionChoose.m_SourceGridData.GetComboDownListIndexByItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
-            //int sel = m_condition.SourceGridData.ConditionItemList[m_ReportApp.m_DataMgrFun.GetPropIndexByPropItemName(m_ReportApp.SourceGridData.ConditionItemList, OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE)].comboDownList.IndexOf(m_ReportApp.SourceGridData.ConditionItemList[1].itemVal.ToString());
-            //int sel = m_condition.m_SourceGridData.GetPropIndexByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
             string filedAndParticl = "";
             string po = "";
             string con = "";
@@ -342,6 +381,8 @@ namespace OTSIncAReportApp.Control_ECharts
             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++)
             {
@@ -365,6 +406,10 @@ namespace OTSIncAReportApp.Control_ECharts
                     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);
                 }
 
@@ -486,6 +531,7 @@ namespace OTSIncAReportApp.Control_ECharts
             //pic = str;
 
         }
+
         /// <summary>
         /// 加载浏览器
         /// </summary>
@@ -503,7 +549,10 @@ namespace OTSIncAReportApp.Control_ECharts
                 m_ReportApp.browser_trio.MenuHandler = new MenuHandler();
                 m_ReportApp.browser_trio.Dock = DockStyle.Fill;
                 //添加网页到当前自定义控件
-                this.Controls.Add(m_ReportApp.browser_trio);
+                //this.Controls.Add(m_ReportApp.browser_trio);
+                this.panel1.Controls.Add(m_ReportApp.browser_trio);
+
+
                 m_ReportApp.js.MessageText = "";
                 //供页面调用以传递数据
                 m_ReportApp.browser_trio.RegisterJsObject("jsObj", m_ReportApp.js, false);
@@ -514,6 +563,7 @@ namespace OTSIncAReportApp.Control_ECharts
 
                 //网页加载结束后调用事件
                 m_ReportApp.browser_trio.FrameLoadEnd += WebBrowser_FrameLoadEnd;
+
             }
             else
             {
@@ -522,7 +572,9 @@ namespace OTSIncAReportApp.Control_ECharts
                 m_ReportApp.browser_trio.Refresh();
 
                 //供页面调用以传递数据
-                this.Controls.Add(m_ReportApp.browser_trio);
+                //this.Controls.Add(m_ReportApp.browser_trio);
+                this.panel1.Controls.Add(m_ReportApp.browser_trio);
+
             }
 
             if (m_ReportApp.timerKG)
@@ -545,6 +597,70 @@ namespace OTSIncAReportApp.Control_ECharts
                 m_ReportApp.browser_trio.ExecuteScriptAsync("showchart('" + str + "','','triangle')");
             }
         }
+
+        private void ExportDataTableToExcel(DataTable dataTable, string filePath)
+        {
+            // 创建一个新的工作簿
+            IWorkbook workbook = new XSSFWorkbook(); // 对于.xlsx格式
+                                                     // IWorkbook workbook = new HSSFWorkbook(); // 对于.xls格式
+
+            // 添加一个新的工作表
+            ISheet sheet = workbook.CreateSheet("Sheet1");
+
+
+            // 创建表头行(第一行)
+            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.Columns.Count; i++)
+            //{
+            //    ICell cell = headerRow.CreateCell(i);
+            //    cell.SetCellValue(dataTable.Columns[i].ColumnName);
+            //}
+
+            // 填充数据行
+            for (int i = 0; i < dataTable.Rows.Count; i++)
+            {
+                IRow row = sheet.CreateRow(i + 1);
+                for (int j = 0; j < dataTable.Columns.Count; j++)
+                {
+                    ICell cell = row.CreateCell(j);
+                    if (dataTable.Rows[i][j] != DBNull.Value)
+                    {
+                        cell.SetCellValue(dataTable.Rows[i][j].ToString());
+                    }
+                }
+            }
+
+            // 自动调整列宽(可选)
+            // 注意:NPOI的AutoSizeColumn方法不如EPPlus的AutoFitColumns方法那样精确
+            // 你可能需要手动设置列宽或者接受默认的宽度
+            // 但以下是一个尝试自动调整列宽的示例,它可能不完全符合你的期望
+            for (int i = 0; i < dataTable.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的对象,并且想确保资源被释放
+        }
         //public string GetPic()
         //{
         //    return js.MessageText;
@@ -681,5 +797,9 @@ namespace OTSIncAReportApp.Control_ECharts
         }
         #endregion
 
+        private void button1_Click_1(object sender, EventArgs e)
+        {
+            ExportExcelDocument();
+        }
     }
 }