DataTable
LINQ
CSV
//DataTable
List<traySetRouteTemp> CsvTOecssetroutetmp = new List<traySetRouteTemp>();
using (OleDbConnection connection = new OleDbConnection(
string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source={0}; Extended Properties=""text;
HDR=Yes;IMEX=1;FMT=Delimited"";", strFilePath)))
{
using (OleDbCommand command = new OleDbCommand(
string.Format(@"SELECT
SPECIAL_ROUTE,
SPECIAL_SEQ,
BATCH_NO,
ROUTE,
ROUTE_SEQ,
STEP_ST,
STEP_END FROM " + strFileName), connection))
{
connection.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
//修正因使用者匯入檔案第一列為非 VO 欄位名稱
//則無法在使用 DataTable To List 正確轉換資料
//導致轉出 List 欄位資料皆為空,故在建立 DataTable
//完成時,再將Table 欄位名稱更正為與 VO 相同名稱
//by jako Add 20160107
dt.Columns[0].ColumnName = "SPECIAL_ROUTE";
dt.Columns[1].ColumnName = "SPECIAL_SEQ";
dt.Columns[2].ColumnName = "BATCH_NO";
dt.Columns[3].ColumnName = "ROUTE";
dt.Columns[4].ColumnName = "ROUTE_SEQ";
dt.Columns[5].ColumnName = "STEP_ST";
dt.Columns[6].ColumnName = "STEP_END";
CsvTOecssetroutetmp = DataTableUtility.ToList<traySetRouteTemp>(dt).ToList();
connection.Close();
return CsvTOecssetroutetmp;
}
}
}
//LINQ
List<ecsSetRouteTemp> CsvTOecssetroutetmp = new List<ecsSetRouteTemp>();
var data = File.ReadAllLines(strFilePath + "\\" + strFileName).Skip(1)
.Select(
l =>
{
var split = l.Split(new[] { ',', ' ' }, StringSplitOptions.None);
return new ecsSetRouteTemp
{
SPECIAL_ROUTE = split[0],
SPECIAL_SEQ = split[1],
BATCH_NO = split[2],
ROUTE = split[3],
ROUTE_SEQ = split[4],
STEP_ST = split[5],
STEP_END = split[0]
};
}
);
foreach (var item in data)
{
CsvTOecssetroutetmp.Add(item);
}
return CsvTOecssetroutetmp;