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
{
///
/// Create target file
///
/// folder
/// folder name
/// file extension
/// file path
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
///
/// 获取类的属性集合(以便生成CSV文件的所有Column标题)
///
///
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;
}
///
/// Save the List data to CSV file
///
/// data source
/// file path
/// success flag
public static bool SaveMEMSSPDataToCSVFile(ObservableCollection 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 MEMSZpModel
///
/// 获取类的属性集合(以便生成CSV文件的所有Column标题)
///
///
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;
}
///
/// Save the List data to CSV file
///
/// data source
/// file path
/// success flag
public static bool SaveMEMSZPDataToCSVFile(ObservableCollection 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
}
}