Add TSV support.
Import Upload
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using MileageTraker.Web.Models;
|
||||
@@ -9,10 +10,11 @@ using MileageTraker.Web.Utility;
|
||||
|
||||
namespace MileageTraker.Web.DAL
|
||||
{
|
||||
public class FuelmanCsvImporter
|
||||
public static class FuelmanCsvImporter
|
||||
{
|
||||
private sealed class FuelmanCsvClassMap : CsvClassMap<FuelLog>
|
||||
{
|
||||
readonly string[] _dateFormats = { @"M/d/yy HH:mm", @"M/d/yyyy HH:mm" };
|
||||
public FuelmanCsvClassMap()
|
||||
{
|
||||
Map(m => m.Date).ConvertUsing(row =>
|
||||
@@ -20,7 +22,12 @@ namespace MileageTraker.Web.DAL
|
||||
var date = row.GetField("CEDATE");
|
||||
var time = row.GetField<int>("CETIME");
|
||||
var dttm = string.Format(@"{0} {1:00:00}", date, time);
|
||||
return DateTime.ParseExact(dttm, @"M/d/yyyy HH:mm", CultureInfo.InvariantCulture);
|
||||
|
||||
DateTime parsed;
|
||||
if (DateTime.TryParseExact(dttm, _dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed))
|
||||
return parsed;
|
||||
|
||||
throw new ArgumentException(string.Format("Date in incorrect format: CEDATE:{0} CETIME:{1}", date, time));
|
||||
});
|
||||
Map(m => m.DriverFullName).ConvertUsing(row =>
|
||||
{
|
||||
@@ -41,16 +48,35 @@ namespace MileageTraker.Web.DAL
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<FuelLog> Import(string filename)
|
||||
public static bool IsProbablyTabSeparated(FileInfo fileInfo)
|
||||
{
|
||||
using (var tr = File.OpenText(fileInfo.FullName))
|
||||
{
|
||||
var line = tr.ReadLine();
|
||||
var tabHeadCount =
|
||||
(from c in line
|
||||
where c == '\t'
|
||||
select c).Count();
|
||||
// if there's more than 10 tabs in the first line, probably TSV
|
||||
return tabHeadCount > 10;
|
||||
}
|
||||
}
|
||||
|
||||
public static ICollection<FuelLog> Import(FileInfo fileInfo)
|
||||
{
|
||||
var fileInfo = new FileInfo(filename);
|
||||
if (!fileInfo.Exists)
|
||||
throw new ImportException("Cannot find file '" + filename + "'");
|
||||
throw new ImportException("Cannot find file '" + fileInfo.Name + "'");
|
||||
|
||||
var tabSeparatedValues = IsProbablyTabSeparated(fileInfo);
|
||||
|
||||
using (var tr = File.OpenText(fileInfo.FullName))
|
||||
{
|
||||
var csv = new CsvReader(tr);
|
||||
csv.Configuration.RegisterClassMap<FuelmanCsvClassMap>();
|
||||
var csvConfiguration = new CsvConfiguration();
|
||||
if (tabSeparatedValues)
|
||||
csvConfiguration.Delimiter = "\t";
|
||||
csvConfiguration.RegisterClassMap<FuelmanCsvClassMap>();
|
||||
|
||||
var csv = new CsvReader(tr, csvConfiguration);
|
||||
|
||||
var fuelLogs = new List<FuelLog>();
|
||||
while (csv.Read())
|
||||
|
||||
Reference in New Issue
Block a user