20240301_JSEQ_upperpc/JiangsuEarthquakeNowUI/JiangsuEarthquake/Common/CSVDownload.cs

697 lines
26 KiB
C#
Raw Normal View History

2024-11-01 07:54:08 +00:00
using JiangsuEarthquake.Models;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Application = System.Windows.Application;
namespace JiangsuEarthquake.Common
{
public class CSVDownload
{
/// <summary>
/// Create target file
/// </summary>
/// <param name="folder">folder</param>
/// <param name="fileName">folder name</param>
/// <param name="fileExtension">file extension</param>
/// <returns>file path</returns>
public static string CreateFile(string folder, string fileName, string fileExtension)
{
FileStream fs = null;
string filePath = folder + fileName + "." + fileExtension;
try
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
fs = File.Create(filePath);
}
catch (Exception ex)
{ }
finally
{
if (fs != null)
{
fs.Dispose();
}
}
return filePath;
}
#region BaseStation
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetBaseStationEnvironPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(BaseStationEnvirModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveBaseStationEnvironDataToCSVFile(ObservableCollection<BaseStationEnvirModel> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetBaseStationEnvironPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < BaseStationList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(BaseStationList[i].Index);
strValue.Append(",");
strValue.Append(BaseStationList[i].RecordTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Temperature);
strValue.Append(",");
strValue.Append(BaseStationList[i].Humidity);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeX);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeY);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeZ);
strValue.Append(",");
//if (BaseStationList[i].Hatch_State == (ImageSource)Application.Current.FindResource("CycleRed"))
// strValue.Append("舱门异常");
//else if (BaseStationList[i].Hatch_State == (ImageSource)Application.Current.FindResource("CycleGreen"))
// strValue.Append("舱门正常");
//else
// strValue.Append("无数据");
//strValue.Append(",");
//if (BaseStationList[i].Leakage == (ImageSource)Application.Current.FindResource("CycleRed"))
// strValue.Append("漏水");
//else if (BaseStationList[i].Leakage == (ImageSource)Application.Current.FindResource("CycleGreen"))
// strValue.Append("未漏水");
//else
// strValue.Append("无数据");
strValue.Append(BaseStationList[i].Leakage);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region BaseStationMonitor
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetBaseStationMonitorPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(BaseStationEnvirModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveBaseStationMonitorDataToCSVFile(ObservableCollection<BaseStationMonitorModel> BaseStationMonitorList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetBaseStationMonitorPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < BaseStationMonitorList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(BaseStationMonitorList[i].Index);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].RecordTime);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Seis1_Voltage);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Seis1_Current);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Seis2_Voltage);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Seis2_Current);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Elect_Current);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Out_Voltage12_Reserved1);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Out_Voltage12_Reserved2);
strValue.Append(",");
strValue.Append(BaseStationMonitorList[i].Reserved);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region BoosterStation
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetBoosterStationPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(BoosterStationStateDataModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveBoosterStationDataToCSVFile(ObservableCollection<BoosterStationStateDataModel> BoosterStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetBoosterStationPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < BoosterStationList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(BoosterStationList[i].Index);
strValue.Append(",");
strValue.Append(BoosterStationList[i].DataTime);
strValue.Append(",");
strValue.Append(BoosterStationList[i].In_Vol);
strValue.Append(",");
strValue.Append(BoosterStationList[i].In_Cur);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region SeismometerState
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetSeismometerStatePropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(EarthquakeSensorModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveSeismometerStateDataToCSVFile(ObservableCollection<EarthquakeSensorModel> EarthquakeSensorList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetSeismometerStatePropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < EarthquakeSensorList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(EarthquakeSensorList[i].Index);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].DataTime);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Out_Vol);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Backup_Vol);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Pre);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Tem);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Sei_Tilt_Angle);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].OBS_Tilt_Angle);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Species_Dif);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Frequency_Dif);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].CF_Total_Cap);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].CF_Usable_Cap);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].SD_Total_Cap1);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].SD_Usable_Cap1);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].SD_Total_Cap2);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].SD_Usable_Cap2);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Sei_U_Point);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Sei_V_Point);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].Sei_W_Point);
strValue.Append(",");
strValue.Append(EarthquakeSensorList[i].North_Angle);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region SeismometerParameter
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetSeismometerParameterPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(EarthquakeSensorModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveSeismometerParameterToCSVFile(ObservableCollection<EarthQuakeParaSetModel> EarthquakeParameterList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetSeismometerParameterPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < EarthquakeParameterList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(EarthquakeParameterList[i].Index);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].DataTime);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].SeisID);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].StationNo);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].StationName);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].StationShortName);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].EarthQuakeCount);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].ChannelsNo);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].WD);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].JD);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].GaoCheng);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].StartTime);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].Station_Id);
strValue.Append(",");
strValue.Append(EarthquakeParameterList[i].Software_Version);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region LogRecord
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetLogRecordPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(LogRecordModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveLogRecordDataToCSVFile(ObservableCollection<LogRecordModel> LogRecordList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetLogRecordPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < LogRecordList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(LogRecordList[i].Index);
strValue.Append(",");
strValue.Append(LogRecordList[i].RecordTime);
strValue.Append(",");
strValue.Append(LogRecordList[i].Device_Name);
strValue.Append(",");
strValue.Append(LogRecordList[i].Operation_Type);
strValue.Append(",");
strValue.Append(LogRecordList[i].Record);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region AlarmRecord
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetAlarmRecordPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(AlarmRecordModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveAlarmRecordDataToCSVFile(ObservableCollection<AlarmRecordModel> AlarmRecordList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetAlarmRecordPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < AlarmRecordList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(AlarmRecordList[i].Index);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].RecordTime);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaName);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaNum);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaContent);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ProcessingMethod);
strValue.Append(",");
if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleRed"))
strValue.Append("一级紧急");
else if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleOrange"))
strValue.Append("二级紧急");
else if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleYellow"))
strValue.Append("三级紧急");
strValue.Append(",");
strValue.Append(AlarmRecordList[i].IsHandled);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
}
}