20230201_145_upperpc/InSituLaboratory/Base/CSVDownload.cs

608 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using InSituLaboratory.Entities.Sensor;
using InSituLaboratory.Models.Sendsor;
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 InSituLaboratory.Base
{
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 MEMS色谱仪
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetMEMSSPInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(MEMSSpModels);
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 SaveMEMSSPDataToCSVFile(ObservableCollection<MEMSSpModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetMEMSSPInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].C2);
strValue.Append(",");
strValue.Append(BaseStationList[i].C3);
strValue.Append(",");
strValue.Append(BaseStationList[i].C4);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region MEMS质谱仪
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetMEMSZPInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(MEMSZpModels);
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 SaveMEMSZPDataToCSVFile(ObservableCollection<MEMSZpModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetMEMSZPInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].CH4);
strValue.Append(",");
strValue.Append(BaseStationList[i].H2O);
strValue.Append(",");
strValue.Append(BaseStationList[i].N2);
strValue.Append(",");
strValue.Append(BaseStationList[i].O2);
strValue.Append(",");
strValue.Append(BaseStationList[i].Ar);
strValue.Append(",");
strValue.Append(BaseStationList[i].CO2);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetColorMSInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(ColorMSModels);
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 SaveColorMSDataToCSVFile(ObservableCollection<ColorMSModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetColorMSInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].C2);
strValue.Append(",");
strValue.Append(BaseStationList[i].C3);
strValue.Append(",");
strValue.Append(BaseStationList[i].C4);
strValue.Append(",");
strValue.Append(BaseStationList[i].C5);
strValue.Append(",");
strValue.Append(BaseStationList[i].C6);
strValue.Append(",");
strValue.Append(BaseStationList[i].C7);
strValue.Append(",");
strValue.Append(BaseStationList[i].C8);
strValue.Append(",");
strValue.Append(BaseStationList[i].C9);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetCH4IsotopeInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(CH4IsotopeModels);
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 SaveCh4IsotopeDataToCSVFile(ObservableCollection<CH4IsotopeModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetCH4IsotopeInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].C1);
strValue.Append(",");
strValue.Append(BaseStationList[i].C2);
strValue.Append(",");
strValue.Append(BaseStationList[i].Abundance);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetCO2IsotopeInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(CO2IsotopsModels);
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 SaveCO2IsotopeDataToCSVFile(ObservableCollection<CO2IsotopsModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetCO2IsotopeInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].LightIntensity);
strValue.Append(",");
strValue.Append(BaseStationList[i].LaserTemperature);
strValue.Append(",");
strValue.Append(BaseStationList[i].CO2Concentration);
strValue.Append(",");
strValue.Append(BaseStationList[i].IsotopicAbundance);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetSequencerInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(SequencerModels);
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 SaveSequencerDataToCSVFile(ObservableCollection<SequencerModels> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetSequencerInfoArray();
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].Id);
strValue.Append(",");
strValue.Append(BaseStationList[i].CreateTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].SamplingTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Tem);
strValue.Append(",");
strValue.Append(BaseStationList[i].Hum);
strValue.Append(",");
strValue.Append(BaseStationList[i].Pressure);
strValue.Append(",");
strValue.Append(BaseStationList[i].Insulation);
strValue.Append(",");
strValue.Append(BaseStationList[i].ReagentTemperature);
strValue.Append(",");
strValue.Append(BaseStationList[i].SampleConcentration);
strValue.Append(",");
strValue.Append(BaseStationList[i].CurrentWorkflow);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
}
}