Admin import functional

This commit is contained in:
2014-02-02 10:19:02 -05:00
parent 13c5c6cde5
commit 3ab80e283a
41 changed files with 2809 additions and 101 deletions
+50
View File
@@ -3,9 +3,11 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using MileageTraker.Web.Models;
namespace MileageTraker.Web.Utility
{
@@ -61,6 +63,47 @@ namespace MileageTraker.Web.Utility
});
}
public static Enum ParseWithDisplayNames(this Type enumType, string item)
{
Func<string, bool> f = s => string.Equals(s, item, StringComparison.CurrentCultureIgnoreCase);
return Enum.GetValues(enumType)
.OfType<Enum>().First(e => e.GetAllNameVariants().Any(f));
}
public static bool IsValidValue(this Type enumType, string item)
{
Func<string, bool> f = s => string.Equals(s, item, StringComparison.CurrentCultureIgnoreCase);
return Enum.GetValues(enumType)
.OfType<Enum>().Any(e => e.GetAllNameVariants().Any(f));
}
public static IEnumerable<string> GetAllNameVariants(this Enum enumeration)
{
yield return enumeration.ToString();
yield return enumeration.GetDisplayName();
yield return enumeration.GetDisplayShortName();
}
public static Expression<Func<Log, bool>> GetEqualsPredicate(this MileageLogType mileLogType)
{
Expression<Func<Log, bool>> predicate;
switch (mileLogType)
{
case MileageLogType.NonCommuting:
predicate = l => l.LogType.Value == 1;
break;
case MileageLogType.Commuting:
predicate = l => l.LogType.Value == 2;
break;
case MileageLogType.GasPurchase:
predicate = l => l.LogType.Value == 3;
break;
default:
throw new ArgumentOutOfRangeException();
}
return predicate;
}
public static IEnumerable<SelectListItem> ToSelectList(this Type enumType, string selectedItem, string noItemSelected)
{
var items = (from item in Enum.GetValues(enumType).OfType<Enum>()
@@ -99,6 +142,13 @@ namespace MileageTraker.Web.Utility
return value;
}
public static IEnumerable<string> GetPropertyNames(this Type type)
{
return
from property in type.GetProperties()
select property.GetName();
}
public static bool IsSqlMinValue(this DateTime dt)
{
return dt == SqlDateTime.MinValue.Value;
+93 -54
View File
@@ -2,20 +2,21 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ExcelLibrary.SpreadSheet;
using MileageTraker.Web.Models;
namespace MileageTraker.Web.Utility
{
public static class ExcelWriter
public abstract class ExcelWriter<T>
{
public static void WriteXls<T>(IEnumerable<T> items, string filename, string worksheetTitle, string worksheetName)
public static void WriteXls(IEnumerable<T> items, string filename, string worksheetTitle, string worksheetName)
{
using (var fileStream = new FileStream(filename, FileMode.Create))
WriteXls(items, fileStream, worksheetTitle, worksheetName);
}
public static byte[] WriteXls<T>(IEnumerable<T> items, string worksheetTitle, string worksheetName)
public static byte[] WriteXls(IEnumerable<T> items, string worksheetTitle, string worksheetName)
{
using (var stream = new MemoryStream())
{
@@ -24,69 +25,107 @@ namespace MileageTraker.Web.Utility
}
}
public static void WriteXls<T>(IEnumerable<T> items, Stream stream, string worksheetTitle, string worksheetName)
public static void WriteXls(IEnumerable<T> items, Stream stream, string worksheetTitle, string worksheetName)
{
var workbook = new Workbook();
workbook.Worksheets.Add(WriteWorksheet(items, worksheetTitle, worksheetName));
workbook.Save(stream);
}
protected static IEnumerable<PropertyInfo> GetProperties()
{
return
typeof (T).GetProperties()
.Where(p => !p.PropertyType.IsCollection())
.Where(p => !p.Name.Contains("PreviousLog"))
.ToList();
}
public static int GetPropertyCount()
{
return GetProperties().Count();
}
public static Worksheet WriteWorksheet(IEnumerable<T> items, string worksheetTitle, string worksheetName)
{
var worksheet = new Worksheet(worksheetName);
// write worksheet header
worksheet.Cells[0, 3] = new Cell(worksheetTitle);
// write column headers
var properties =
typeof(T).GetProperties()
.Where(p => !p.PropertyType.IsCollection())
.Where(p => !p.Name.Contains("PreviousLog"))
.ToList();
properties.Zip(
CustomExtensions.GetNumbers(),
(p, c) =>
worksheet.Cells[2, c] = new Cell(p.GetName())
).ToList();
WriteColumnHeaders(worksheet, 2);
// write the data
items.Zip(
CustomExtensions.GetNumbers().Skip(3),
(item, r) =>
properties.Zip(CustomExtensions.GetNumbers(),
(p, c) => {
var value = p.GetValue(item);
string formatString = null;
if (value is decimal) // assume that it's currency
formatString = "#,##0.00";
if (value is DateTime && p.Name == "Date")
formatString = @"YYYY\-MM\-DD";
else if (value is DateTime)
formatString = @"YYYY\-MM\-DD hh:mm:ss";
int intValue; // write int-looking values as numbers
if (value is string && int.TryParse((string)value, out intValue))
value = intValue;
double doubleValue; // write double-looking values as numbers
if (value is string && double.TryParse((string)value, out doubleValue))
value = doubleValue;
if (value is MileageLogTypeWrapper)
value = ((MileageLogTypeWrapper) value).Enum.GetDisplayName();
var cell = formatString != null ? new Cell(value,formatString) : new Cell(value);
worksheet.Cells[r, c] = cell;
return (string)null;
}).ToList()).ToList();
WriteItems(worksheet, items, 3);
PadWorksheet(worksheet);
workbook.Worksheets.Add(worksheet);
workbook.Save(stream);
return worksheet;
}
private static void PadWorksheet(Worksheet worksheet)
public static void WriteColumnHeaders(Worksheet worksheet, int startRow = 0, int startCol = 0, string formatString = "")
{
// write column headers
GetProperties().Zip(
CustomExtensions.GetNumbers().Skip(startCol),
(p, c) =>
worksheet.Cells[startRow, c] = new Cell(p.GetName(), formatString)
).ToList();
}
public static void WriteItems(Worksheet worksheet, IEnumerable<string> items, int startRow = 0, int startCol = 0)
{
var r = startRow;
foreach (var cell in items.Select(item => new Cell(item)))
{
worksheet.Cells[r, startCol] = cell;
r++;
}
}
public static void WriteItems(Worksheet worksheet, IEnumerable<T> items, int startRow = 0, int startCol = 0)
{
// write the data
items.Zip(
CustomExtensions.GetNumbers().Skip(startRow),
(item, r) =>
GetProperties().Zip(CustomExtensions.GetNumbers().Skip(startCol),
(p, c) => {
var value = p.GetValue(item);
string formatString = null;
if (value is decimal) // assume that it's currency
formatString = "#,##0.00";
if (value is DateTime && p.Name == "Date")
formatString = @"YYYY\-MM\-DD";
else if (value is DateTime)
formatString = @"YYYY\-MM\-DD hh:mm:ss";
int intValue; // write int-looking values as numbers
if (value is string && int.TryParse((string) value, out intValue))
value = intValue;
double doubleValue; // write double-looking values as numbers
if (value is string && double.TryParse((string) value, out doubleValue))
value = doubleValue;
if (value is MileageLogTypeWrapper)
value = ((MileageLogTypeWrapper) value).Enum.GetDisplayName();
var cell = formatString != null ? new Cell(value, formatString) : new Cell(value);
worksheet.Cells[r, c] = cell;
return (string) null;
}).ToList()).ToList();
}
/// <summary>
/// Fill the worksheet with some empty space if not "enough" cells
/// </summary>
public static void PadWorksheet(Worksheet worksheet)
{
Func<int> totalCells = () => (worksheet.Cells.LastRowIndex + 1)*(worksheet.Cells.LastColIndex + 1);
for (var r = worksheet.Cells.LastRowIndex + 1; totalCells() < 320; r++)