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

386 lines
13 KiB
C#
Raw Permalink Normal View History

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;
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[] GetBaseStationPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(BaseStationStatusModel);
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 SaveBaseStationDataToCSVFile(ObservableCollection<BaseStationStatusModel> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetBaseStationPropertyInfoArray();
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].DataTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].In_Vol);
strValue.Append(",");
strValue.Append(BaseStationList[i].In_Cur);
strValue.Append(",");
strValue.Append(BaseStationList[i].JBH_Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].JBH_Hum);
strValue.Append(",");
if (BaseStationList[i].JBH_Leak== (ImageSource)Application.Current.FindResource("CycleRed"))
strValue.Append("漏水");
else
strValue.Append("未漏水");
strValue.Append(",");
strValue.Append(BaseStationList[i].JBH_Attitude_x);
strValue.Append(",");
strValue.Append(BaseStationList[i].JBH_Attitude_y);
strValue.Append(",");
strValue.Append(BaseStationList[i].JBH_Attitude_z);
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 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].DataTime);
strValue.Append(",");
strValue.Append(LogRecordList[i].Remark);
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].DataTime);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaName);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaState);
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("三级紧急");
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
}
}