using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.IO;
using Aspose.Cells;
using System.Data;
using System.ComponentModel;
using System.Configuration;
namespace src.Common
{
public static class Excel
{
///
/// list转为excel保存
///
///
/// 数据源
/// 保存路径
/// 文件名
///
public static string ListToExcel(List list, string saveWay, string name)
{
Workbook workBook = new Workbook();
Worksheet worksheet = workBook.Worksheets[0];
Cells cells = worksheet.Cells;
Type t = typeof(T);
string tempName = "";
int i = 0, k = 1;
foreach (PropertyInfo p in t.GetProperties())
{
DescriptionAttribute attr = (DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
if (attr != null)
{
tempName = attr.Description;
}
else
{
tempName = p.Name;
}
cells[0, i].PutValue(tempName);
Style style = cells[0, i].GetStyle();
style.BackgroundColor = System.Drawing.Color.Blue;
style.HorizontalAlignment = TextAlignmentType.Center;
cells[0, i].SetStyle(style);
i++;
}
foreach (T tt in list)
{
i = 0;
foreach (PropertyInfo p1 in t.GetProperties())
{
if (p1.GetValue(tt, null) != null)
{
cells[k, i].PutValue(p1.GetValue(tt, null).ToString());
}
i++;
}
k++;
}
//row.Style.BackgroundColor = System.Drawing.Color.Blue;
//row.Style.HorizontalAlignment = TextAlignmentType.Center;
worksheet.AutoFitColumns();
worksheet.AutoFitRows();
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string baseWay = System.AppDomain.CurrentDomain.BaseDirectory;
string currentTime = DateTime.Now.ToString("yyyy-MM");
string fileWay = baseWay + saveWay + "\\" + currentTime;
if (!File.Exists(fileWay))
{
Directory.CreateDirectory(fileWay);
}
workBook.Save(fileWay + "\\" + name + fileName);
return ConfigurationManager.AppSettings["Domain"] + "/" + saveWay + "/" + currentTime + "/" + name + fileName;
}
///
/// 表格转为datatable
///
///
///
///
///
public static bool ExcelToDataTable(string filepath, out DataTable datatable, out string error)
{
error = "";
datatable = null;
try
{
if (!File.Exists(filepath))
{
error = "文件不存在";
datatable = null;
return false;
}
Workbook workbook = new Workbook(filepath);
Worksheet worksheet = workbook.Worksheets[0];
datatable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1);
return true;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
///
/// datatable转list,第一行为列名
///
///
///
///
public static List ToList1(this DataTable dt, string primaryKey)
{
List ts = new List();
Type t = typeof(T);
List<object> cols = dt.Rows[0].ItemArray.ToList();
bool isNull = false;
if (!cols.Contains(primaryKey))
{
return null;
}
else
{
for (int i = 1; i < dt.Rows.Count - 1; i++)
{
isNull = false;
T tt = System.Activator.CreateInstance();
string tempName = "";
foreach (PropertyInfo p in t.GetProperties())
{
DescriptionAttribute attr = (DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
if (attr != null)
{
tempName = attr.Description;
}
else
{
tempName = p.Name;
}
if (cols.Contains(tempName))
{
object value = dt.Rows[i][cols.IndexOf(tempName)];
if (tempName == primaryKey && (value == null||string.IsNullOrEmpty(value.ToString())))
{
isNull = true;
}
if (value != null && (!string.IsNullOrEmpty(value.ToString())))
{
if (!typeof(DBNull).Equals(p.PropertyType.GetType()))
{
p.SetValue(tt, Convert.ChangeType(value, p.PropertyType), null);
}
else
{
p.SetValue(tt, null, null);
}
}
}
}
if (!isNull)
{
ts.Add(tt);
}
}
return ts;
}
}
}
}