Shuxia Ning
2024-12-19 b0c978129ba55cf81e8470b6c9326745a5dbc7d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using Yw.Geometry;
 
namespace Yw.WinFrmUI.Phart
{
    public class PumpChartExportHelper
    {
 
        /// <summary>
        /// 导出
        /// </summary> 
        public static void ExportXLS(Yw.Geometry.CubicSpline2d qh, Yw.Geometry.CubicSpline2d qe, Yw.Geometry.CubicSpline2d qp, int fitPointNum = 15)
        {
            try
            {
                if (qh == null)
                    return;
 
                var dlg = new SaveFileDialog();
                dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                if (dlg.ShowDialog() != DialogResult.OK)
                    return;
 
                HSSFWorkbook theBook = new HSSFWorkbook();
                var theSheet1 = theBook.CreateSheet("Sheet1");
 
                IRow rowTile = theSheet1.CreateRow(0);
                rowTile.CreateCell(0).SetCellValue("流量");
                rowTile.CreateCell(1).SetCellValue("扬程");
                rowTile.CreateCell(2).SetCellValue("效率");
                rowTile.CreateCell(3).SetCellValue("功率");
 
 
                //均匀插值点,计算点坐标
                double min_flow = qh.MinX;
                double max_flow = qh.MaxX;
                double space = (max_flow - min_flow) / (fitPointNum - 1);
                double Q = min_flow;
                double H, E, P;
                for (int rowIndex = 1; rowIndex <= fitPointNum; rowIndex++)
                {
                    int col = 0;
                    IRow row = theSheet1.CreateRow(rowIndex);
 
                    row.CreateCell(col).SetCellValue(Trans(Q));
                    col++;
 
                    H = qh.GetPointY(Q);
                    row.CreateCell(col).SetCellValue(Trans(H));
                    col++;
 
                    if (qe != null && Q <= qe.MaxX && Q >= qe.MinX)
                    {
                        if (Q < 0.1)
                            E = 0;
                        else
                            E = qe.GetPointY(Q);
                        row.CreateCell(col).SetCellValue(Trans(E));
                        col++;
                    }
 
 
                    if (qp != null && Q <= qp.MaxX && Q >= qp.MinX)
                    {
                        P = qp.GetPointY(Q);
                        row.CreateCell(col).SetCellValue(Trans(P));
                        col++;
                    }
 
 
                    Q = Q + space;
                    if (Q > qh.MaxX * 0.9999)
                        Q = qh.MaxX * 0.9999;
                }
 
                //强制Excel在打开时重新计算所有公式
                theSheet1.ForceFormulaRecalculation = true;
                using (FileStream fs = File.OpenWrite(dlg.FileName))
                {
                    theBook.Write(fs);
                }
 
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
            XtraMessageBox.Show("导出成功");
        }
 
        /// <summary>
        /// 导出定义点
        /// </summary> 
        public static void ExportDefinePoints(List<Yw.Geometry.Point2d> qh, List<Yw.Geometry.Point2d> qe, List<Yw.Geometry.Point2d> qp, int fitPointNum = 15)
        {
            try
            {
                if (qh == null)
                    return;
 
                var dlg = new SaveFileDialog();
                dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                if (dlg.ShowDialog() != DialogResult.OK)
                    return;
 
 
                HSSFWorkbook theBook = new HSSFWorkbook();
                var theSheet1 = theBook.CreateSheet("Sheet1");
 
                IRow rowTile = theSheet1.CreateRow(0);
                rowTile.CreateCell(0).SetCellValue("流量");
                rowTile.CreateCell(1).SetCellValue("扬程");
                rowTile.CreateCell(2).SetCellValue("效率");
                rowTile.CreateCell(3).SetCellValue("功率");
 
                List<Yw.Geometry.Point2d> qh_pt_list = null, qe_pt_list = null, qp_pt_list = null;
 
 
                qh_pt_list = qh;
 
                if (qh_pt_list == null || qh_pt_list.Count < 4)
                    return;
 
                qe_pt_list = qe;
                qp_pt_list = qp;
 
                //均匀插值点,计算点坐标
                double min_flow = qh.Min()?.X ?? 0;
                double max_flow = qh.Max()?.X ?? 0;
                double space = (max_flow - min_flow) / (fitPointNum - 1);
                double Q = 0;
                double H, E = 0, P = 0;
 
                int rowIndex = 1;
                foreach (var pointQH in qh_pt_list)
                {
                    Q = pointQH.X;
                    H = pointQH.Y;
 
                    if (qe_pt_list != null && qe_pt_list.Count > 0)
                    {
                        var pointQE = qe_pt_list.Find(x => x.X == Q);
                        if (pointQE != null)
                        {
                            E = pointQE.Y;
                        }
                        else
                        {
                            E = qe_pt_list.GetPointIndexByX(Q);
                        }
                    }
 
                    if (qp_pt_list != null && qp_pt_list.Count > 0)
                    {
                        var pointQP = qp_pt_list.Find(x => x.X == Q);
                        if (pointQP != null)
                        {
                            P = pointQP.Y;
                        }
                        else
                        {
                            P = qp.GetPointIndexByX(Q);
                        }
                    }
 
 
                    var row = theSheet1.CreateRow(rowIndex);
                    rowIndex++;
 
                    int col = 0;
                    row.CreateCell(col).SetCellValue(Trans(Q));
                    col++;
 
                    row.CreateCell(col).SetCellValue(Trans(H));
                    col++;
 
                    row.CreateCell(col).SetCellValue(Trans(E));
                    col++;
 
                    row.CreateCell(col).SetCellValue(Trans(P));
                    col++;
                }
 
 
                //强制Excel在打开时重新计算所有公式
                theSheet1.ForceFormulaRecalculation = true;
                using (FileStream fs = File.OpenWrite(dlg.FileName))
                {
                    theBook.Write(fs);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
                return;
            }
            XtraMessageBox.Show("导出成功");
        }
 
        /// <summary>
        /// 转换
        /// </summary> 
        private static string DoubleTrans(double doubleValue)
        {
            if (doubleValue < 1)
                return doubleValue.ToString("0.0000");
            else if (doubleValue < 10)
                return doubleValue.ToString("0.000");
            else if (doubleValue < 100)
                return doubleValue.ToString("0.00");
            else
                return doubleValue.ToString("0.0");
        }
 
        /// <summary>
        /// 转换
        /// </summary> 
        private static double Trans(double doubleValue)
        {
            if (doubleValue < 1)
                return Math.Round(doubleValue, 4);
            else if (doubleValue < 10)
                return Math.Round(doubleValue, 3);
            else if (doubleValue < 100)
                return Math.Round(doubleValue, 2);
            else
                return Math.Round(doubleValue, 1);
        }
 
    }
}