duheng
2024-03-26 bb006801e4d7fc281e8c1b43ab4b7b83da044ab6
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Curve
{
    public partial class ExportToXLS
    {
        //static string InvalidFileNameChar = "/:*?\"<>|";
 
        //导出单条曲线
        public static void Export(List<Model.CurvePoint> points, string strDefaultFileName = "曲线")
        {
            try
            {
                //选择文件
                SaveFileDialog saveFileDlg = new SaveFileDialog();
                //saveFileDlg.FileName = strDefaultFileName;
                saveFileDlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                if (saveFileDlg.ShowDialog() != DialogResult.OK)
                    return;
                string strXlsFile = saveFileDlg.FileName;
                string fileName = Path.GetFileName(strXlsFile);
 
                int i = 0;
                for (i = 0; i < InvalidFileNameChar.Length; i++)
                {
                    if (fileName.Contains(InvalidFileNameChar[i].ToString()))
                    {
                        throw new Exception(string.Format("{0}含有特殊符号{1},不能作为文件名,请修改",
                            strXlsFile, InvalidFileNameChar[i]));
                    }
                }
 
                #region 初始化表格
                HSSFWorkbook theBook = InitializeBook(strXlsFile);
                if (theBook == null)
                {
                    return;
                }
 
                var theSheet1 = theBook.GetSheet("Sheet1");
                if (theSheet1 == null)
                    return;
                #endregion
 
                NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
 
                rowTile.CreateCell(0).SetCellValue("序号");//          
                rowTile.CreateCell(1).SetCellValue("流量");
                rowTile.CreateCell(2).SetCellValue("扬程");
 
 
                for (i = 0; i < points.Count; i++)
                {
                    var pt = points[i];
                    int col = 0;
                    NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i + 1);
 
                    rowtemp.CreateCell(col).SetCellValue(i + 1);//序号
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(MyTrans(pt.X));
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(MyTrans(pt.Y));
                    col++;
                }
 
                //Force excel to recalculate all the formula while open
                theSheet1.ForceFormulaRecalculation = true;
 
                //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
                using (FileStream fs = File.OpenWrite(strXlsFile))
                {
                    theBook.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
 
                    // XtraMessageBox.Show("导出成功!");
                }
 
                //WinFrmUI.FileMessageBox.Show(strXlsFile, SPump.WinFrmUI.Localization.Current);
 
            }
            catch (TimeoutException ex)
            {
                XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }
 
        /*public static void Export(ViewModel.LxpFeatChartCurveGroup LxpCurveGroup, int FitPointNumQH = 15)
        {
            Export(LxpCurveGroup.FeatCurveQH.CurveExpress, LxpCurveGroup.FeatCurveQE.CurveExpress,
                LxpCurveGroup.FeatCurveQP.CurveExpress,
                FitPointNumQH);
        }*/
 
        static string InvalidFileNameChar = "/:*?\"<>|";
        public static void Export(
             Model.CurveExpress CurveExpressQH,
               Model.CurveExpress CurveExpressQE,
            Model.CurveExpress CurveExpressQP,
            int FitPointNumQH = 15)
        {
            try
            {
                //选择文件
                SaveFileDialog saveFileDlg = new SaveFileDialog();
                //saveFileDlg.FileName = strDefaultFileName;
                saveFileDlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                if (saveFileDlg.ShowDialog() != DialogResult.OK)
                    return;
                string strXlsFile = saveFileDlg.FileName;
                string fileName = Path.GetFileName(strXlsFile);
 
                int i = 0;
                for (i = 0; i < InvalidFileNameChar.Length; i++)
                {
                    if (fileName.Contains(InvalidFileNameChar[i].ToString()))
                    {
                        throw new Exception(string.Format("{0}含有特殊符号{1},不能作为文件名,请修改",
                            strXlsFile, InvalidFileNameChar[i]));
                    }
                }
 
                #region 初始化表格
                HSSFWorkbook theBook = InitializeBook(strXlsFile);
                if (theBook == null)
                {
                    return;
                }
 
                var theSheet1 = theBook.GetSheet("Sheet1");
                if (theSheet1 == null)
                    return;
                #endregion
                NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
                rowTile.CreateCell(0).SetCellValue("序号");//          
                rowTile.CreateCell(1).SetCellValue("流量");
                rowTile.CreateCell(2).SetCellValue("扬程");
                rowTile.CreateCell(3).SetCellValue("效率");
                rowTile.CreateCell(4).SetCellValue("功率");
 
                //均匀插值点,计算点坐标
                double rQmin = CurveExpressQH.Min;
                double rQmax = CurveExpressQH.Max;
                double space = (rQmax - rQmin) / (FitPointNumQH - 1);
                double Q = rQmin;
                double H, E, P;
                for (i = 0; i < FitPointNumQH; i++)
                {
                    H = Model.FitCurveHelper.GetFitPointY(CurveExpressQH, Q);
                    P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
                    var eta = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
 
 
                    int col = 0;
                    NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i + 1);
 
                    rowtemp.CreateCell(col).SetCellValue(i + 1);//序号
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(MyTrans(Q));
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(MyTrans(H));
                    col++;
 
                    if (CurveExpressQE != null
                        && Q <= CurveExpressQE.Max
                        && Q >= CurveExpressQE.Min)
                    {
                        if (Q < 0.1)
                            E = 0;
                        else
                            E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
                        rowtemp.CreateCell(col).SetCellValue(MyTrans(E));
                        col++;
                    }
 
 
                    if (CurveExpressQP != null
                        && Q <= CurveExpressQP.Max
                        && Q >= CurveExpressQP.Min)
                    {
                        P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
                        rowtemp.CreateCell(col).SetCellValue(MyTrans(P));
                        col++;
                    }
 
 
                    Q = Q + space;
                    if (Q > CurveExpressQH.Max * 0.9999)
                        Q = CurveExpressQH.Max * 0.9999;
                }
 
                //Force excel to recalculate all the formula while open
                theSheet1.ForceFormulaRecalculation = true;
 
                //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
                using (FileStream fs = File.OpenWrite(strXlsFile))
                {
                    theBook.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
 
                    // XtraMessageBox.Show("导出成功!");
                }
 
                //WinFrmUI.FileMessageBox.Show(strXlsFile, SPump.WinFrmUI.Localization.Current);
 
            }
            catch (TimeoutException ex)
            {
                XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
 
        }
 
        //初始化表格:复制Blank.xls
        private static HSSFWorkbook InitializeBook(string strFullName, string TemplateFileName = "Blank")
        {
            string strTemplateFile = Application.StartupPath + "\\Template\\ImportPumpFeatCurveStd1.xls";
            if (!File.Exists(strTemplateFile))
            {
                return null;
            }
            File.Copy(strTemplateFile, strFullName);
 
            HSSFWorkbook theBook = null;
            using (FileStream file = new FileStream(strFullName, FileMode.Open, FileAccess.Read))
            {
                theBook = new HSSFWorkbook(file);
            }
 
            //create application entry of DocumentSummaryInformation
            NPOI.HPSF.DocumentSummaryInformation dsi = NPOI.HPSF.PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "EvenTech";
            theBook.DocumentSummaryInformation = dsi;
 
            //create application entry of SummaryInformation
            NPOI.HPSF.SummaryInformation si = NPOI.HPSF.PropertySetFactory.CreateSummaryInformation();
            si.Subject = "yiwei";
            theBook.SummaryInformation = si;
 
            return theBook;
        }
 
        private static string MyTrans(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");
            //return ((int)(doubleValue * 10000) / 10000.0).ToString();
        }
 
 
        public static void Export_Custom(
        List<Model.CurvePoint> Points)
        {
            try
            {
                //选择文件
                SaveFileDialog saveFileDlg = new SaveFileDialog();
                //saveFileDlg.FileName = strDefaultFileName;
                saveFileDlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                if (saveFileDlg.ShowDialog() != DialogResult.OK)
                    return;
                string strXlsFile = saveFileDlg.FileName;
                string fileName = Path.GetFileName(strXlsFile);
 
                int i = 0;
                for (i = 0; i < InvalidFileNameChar.Length; i++)
                {
                    if (fileName.Contains(InvalidFileNameChar[i].ToString()))
                    {
                        throw new Exception(string.Format("{0}含有特殊符号{1},不能作为文件名,请修改",
                            strXlsFile, InvalidFileNameChar[i]));
                    }
                }
 
                if (File.Exists(strXlsFile))
                {
                    File.Delete(strXlsFile);
                }
                #region 初始化表格
                HSSFWorkbook theBook = InitializeBook(strXlsFile);
                if (theBook == null)
                {
                    return;
                }
 
                var theSheet1 = theBook.GetSheet("Sheet1");
                if (theSheet1 == null)
                    return;
                #endregion
 
                //标题
                NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
                rowTile.CreateCell(0).SetCellValue("序号");
                rowTile.CreateCell(1).SetCellValue("流量");
                rowTile.CreateCell(2).SetCellValue("扬程");
                rowTile.CreateCell(3).SetCellValue("效率");
                rowTile.CreateCell(4).SetCellValue("功率");
 
                //均匀插值点,计算点坐标
 
                for (i = 0; i < Points.Count; i++)
                {
 
                    var pt = Points[i];
 
                    int col = 0;
                    NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i + 1);
 
                    rowtemp.CreateCell(col).SetCellValue(i + 1);//序号
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(pt.X);
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(pt.Y);
                    col++;
 
                }
 
                //Force excel to recalculate all the formula while open
                theSheet1.ForceFormulaRecalculation = true;
 
                //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
                using (FileStream fs = File.OpenWrite(strXlsFile))
                {
                    theBook.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。 
                    XtraMessageBox.Show("导出成功!");
                }
 
                //WinFrmUI.FileMessageBox.Show(strXlsFile, SPump.WinFrmUI.Localization.Current);
 
            }
            catch (TimeoutException ex)
            {
                XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }
 
        //初始化表格:复制Blank.xls
        private static HSSFWorkbook InitializeBook1(string strFullName, string TemplateFileName = "Blank")
        {
 
            HSSFWorkbook theBook = null;
            using (FileStream file = new FileStream(strFullName, FileMode.Open, FileAccess.Read))
            {
                theBook = new HSSFWorkbook(file);
            }
 
            //create application entry of DocumentSummaryInformation
            NPOI.HPSF.DocumentSummaryInformation dsi = NPOI.HPSF.PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "EvenTech";
            theBook.DocumentSummaryInformation = dsi;
 
            //create application entry of SummaryInformation
            NPOI.HPSF.SummaryInformation si = NPOI.HPSF.PropertySetFactory.CreateSummaryInformation();
            si.Subject = "yiwei";
            theBook.SummaryInformation = si;
 
            return theBook;
        }
    }
}