20230201_145_upperpc/InSituLaboratory/Base/CSVDownload.cs

237 lines
8.0 KiB
C#
Raw Normal View History

using InSituLaboratory.Entities.Sensor;
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 MEMSSpModel
/// <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
2024-05-10 10:13:30 +00:00
#region MEMSZpModel
/// <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
}
}