ningshuxia
2025-03-31 d321346f204a69b1929cc39098a5d88f44509e7b
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
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Monitor
{
    public class ExportValidatedDataHelper
    {
        //验证数据
        public static void Export(long monitorDataSourcesId, long stationId, DateTime date)
        {
            var packet = new BLL.StationSignalRecordPacket().Get(monitorDataSourcesId, stationId, date.Year, date.Month);
            var list = packet?.StationSignalRecords?.ToList();
            list = list?.Where(x => x.Time.Day == date.Day && x.Time.Minute == 0 && x.Time.Second == 0).ToList();
            if (list == null || !list.Any())
            {
                XtraMessageBox.Show("无数据!");
                return;
            }
 
            try
            {
                var dlg = new SaveFileDialog();
                dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
                dlg.FileName = date.ToString("D");
                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("总功率");
                rowTile.CreateCell(4).SetCellValue("单泵转速字典");
                rowTile.CreateCell(5).SetCellValue("单泵流量字典");
                rowTile.CreateCell(6).SetCellValue("单泵扬程字典");
                rowTile.CreateCell(7).SetCellValue("单泵功率字典");
 
                int rowIndex = 1;
                DateTime current = DateTime.Now;
                double totalHead, totalPower;
                foreach (var record in list)
                {
                    current = record.Time;
                    totalHead = record.PumpSignalRecords.Sum(x => x.Head) / record.PumpRunCount;
                    totalPower = record.PumpSignalRecords.Sum(x => x.InstantaneousPower);
 
                    var flag_rpm_dict = record.PumpSignalRecords.ToDictionary(x => x.Flag, x => x.Rpm);
                    var flag_flow_dict = record.PumpSignalRecords.ToDictionary(x => x.Flag, x => x.FlowRate);
                    var flag_head_dict = record.PumpSignalRecords.ToDictionary(x => x.Flag, x => x.Head);
                    var flag_power_dict = record.PumpSignalRecords.ToDictionary(x => x.Flag, x => x.InstantaneousPower);
 
 
                    var row = theSheet1.CreateRow(rowIndex);
                    rowIndex++;
 
                    int col = 0;
                    row.CreateCell(col).SetCellValue(current.ToString("G"));
                    col++;
 
 
                    row.CreateCell(col).SetCellValue(Trans(totalHead));
                    col++;
 
                    row.CreateCell(col).SetCellValue(Trans(totalPower));
                    col++;
 
                    row.CreateCell(col).SetCellValue(JsonHelper.Object2Json(flag_rpm_dict));
                    col++;
 
                    row.CreateCell(col).SetCellValue(JsonHelper.Object2Json(flag_flow_dict));
                    col++;
 
                    row.CreateCell(col).SetCellValue(JsonHelper.Object2Json(flag_head_dict));
                    col++;
 
                    row.CreateCell(col).SetCellValue(JsonHelper.Object2Json(flag_power_dict));
                    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);
        }
 
 
    }
}