for Push.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using KssSmaPlaLib.Commons;
|
||||
using KssSmaPlaLib.Exchange.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace KssSmaPlaLib.IO.File
|
||||
{
|
||||
public class CsvUxer
|
||||
{
|
||||
public static bool ExportToCsv<T>(List<T> list, string filePath, out string resultMessage, EncodingUxer.EncodingType encodingType = EncodingUxer.EncodingType.Utf8WithoutBom) where T : ICsvExportableUxer
|
||||
{
|
||||
resultMessage = string.Empty;
|
||||
try
|
||||
{
|
||||
var lines = new List<string> { list.FirstOrDefault()?.CsvHeader ?? "" };
|
||||
lines.AddRange(list.Select(item => item.ToCsvLine()));
|
||||
System.IO.File.WriteAllLines(filePath, lines, EncodingUxer.GetEncoding(encodingType));
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
resultMessage = $"CSV出力に失敗しました。[ファイルパス:{filePath},エラー内容:{ex.Message}]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CSVインポート
|
||||
/// </summary>
|
||||
/// <param name="csvFilePath">CSVファイルパス</param>
|
||||
/// <param name="recordSet">[出力]レコードセット</param>
|
||||
/// <param name="resultMessage">[出力]メッセージ</param>
|
||||
/// <param name="encodingType">[オプション]エンコード</param>
|
||||
/// <returns>成否</returns>
|
||||
public static bool ImportFromCsv(string csvFilePath, out List<Dictionary<string, string>> recordSet, out string resultMessage, EncodingUxer.EncodingType encodingType = EncodingUxer.EncodingType.Utf8WithoutBom)
|
||||
{
|
||||
recordSet = new List<Dictionary<string, string>>();
|
||||
resultMessage = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
// 全行読み込み
|
||||
var lines = System.IO.File.ReadAllLines(csvFilePath, EncodingUxer.GetEncoding(encodingType));
|
||||
|
||||
// データ行0件
|
||||
if (lines.Length < 2) return true;
|
||||
|
||||
// ヘッダ行項目分割
|
||||
var header = Regex.Split(lines[0], ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
|
||||
for (var cnt = 0; cnt < header.Length; cnt++) header[cnt] = header[cnt].Trim('\"');
|
||||
|
||||
// データ行件数分
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
// 項目分割
|
||||
var values = Regex.Split(lines[i], ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
|
||||
for (var cnt = 0; cnt < values.Length; cnt++) values[cnt] = values[cnt].Trim('\"');
|
||||
|
||||
if (values.Length != header.Length)
|
||||
{
|
||||
resultMessage = $"CSV読み込みでヘッダの項目数とデータの項目数が異なります。[CSVファイルパス:{csvFilePath},行番号:{i + 1},ヘッダ行項目数:{header.Length},データ行項目数:{values.Length}]";
|
||||
return false;
|
||||
}
|
||||
|
||||
var record = new Dictionary<string, string>();
|
||||
|
||||
// 全項目
|
||||
for (int j = 0; j < header.Length && j < values.Length; j++)
|
||||
{
|
||||
var key = header[j].Trim('\"');
|
||||
var value = values[j].Trim('\"');
|
||||
record[key] = value;
|
||||
}
|
||||
recordSet.Add(record);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
resultMessage = $"CSV読み込み処理でエラーが発生しました。[CSVファイルパス:{csvFilePath},エラー内容:{ex.Message}]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string Escape(string value) => $"\"{value.Replace("\"", "\"\"")}\"";
|
||||
// public static string Join(params object[] values) => string.Join(",", values.Select(m => Escape(m == null ? "" : m.ToString())));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user