文件导出 Aspose

倖福魔咒の 提交于 2020-01-01 22:10:51

/// <summary>
/// 使用Aspose读取Excel数据
/// </summary>
public class ExcelHelperCore
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fields"></param>
/// <param name="list"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static OperationResult ExportExcel<T>(Dictionary<string, string> fields, IEnumerable<T> list, string fileName)
where T : new()
{
if (fields == null || fields.Count == 0)
{
return new OperationResult(OperationResultType.ParamError, "先指定字段");
}

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
var cells = sheet.Cells;
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style.Font.Name = "宋体";//文字字体
style.Font.Size = 10;//文字大小
style.Font.IsBold = true;//粗体
int i = 0;
foreach (KeyValuePair<string, string> keyValuePair in fields)
{
cells[0, i].SetStyle(style); //给单元格关联样式
cells.SetRowHeight(0, 18); //设置行高
cells.SetColumnWidth(i, 16);//设置列宽
//列头
cells[0, i].PutValue(keyValuePair.Value);
i++;
}

PropertyInfo[] properties = new T().GetType().GetProperties();
int row = 1;
foreach (T item in list)
{
i = 0;
foreach (KeyValuePair<string, string> keyValuePair in fields)
{
foreach (PropertyInfo property in properties)
{
if (property.Name == keyValuePair.Key)
{
if (property.PropertyType.Name.StartsWith("DateTime"))
{
var time = Convertor.ConvertToDateTime(property.GetValue(item), DateTime.MinValue);
if (time == DateTime.MinValue || time < new DateTime(2010, 1, 1))
{
cells[row, i].PutValue("");
}
else
{
cells[row, i].PutValue(time.ToString("yyyy-MM-dd HH:mm"));
}
}
else
{
cells[row, i].PutValue(property.GetValue(item));
}
}
}

i++;
}
row++;
}

workbook.Save(HttpContext.Current.Response, fileName, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
return new OperationResult(OperationResultType.Success, "导出成功");
}

/// <summary>
/// 表单导出
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="colums"></param>
/// <param name="property"></param>
/// <param name="list"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static OperationResult ExportExcelForm<T>(List<string> colums, string property, IEnumerable<IEnumerable<T>> list, string fileName)
where T : new()
{
if (colums == null || colums.Count == 0)
{
return new OperationResult(OperationResultType.ParamError, "先指定字段");
}

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
var cells = sheet.Cells;
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style.Font.Name = "宋体";//文字字体
style.Font.Size = 10;//文字大小
style.Font.IsBold = true;//粗体
int i = 0;
#region collumn set
foreach (var c in colums)
{
cells[0, i].SetStyle(style); //给单元格关联样式
cells.SetRowHeight(0, 18); //设置行高
cells.SetColumnWidth(i, 16);//设置列宽
//列头
cells[0, i].PutValue(c);
i++;
}
#endregion

if (null == list || !list.Any())
{
return new OperationResult(OperationResultType.ParamError, "导出内容不能为空");
}

PropertyInfo[] properties = new T().GetType().GetProperties();
int row = 1;
foreach (IEnumerable<T> lst in list)
{
i = 0;
foreach (T item in lst)
{
if (i > colums.Count)
{
break;
}
foreach (PropertyInfo p in properties)
{
if (p.Name == property)
{
cells[row, i].PutValue(p.GetValue(item));
}
}
i++;
}
row++;
}

workbook.Save(HttpContext.Current.Response, fileName, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
return new OperationResult(OperationResultType.Success, "导出成功");
}

public static DataTable ExcelToDatatalbe(String strFileName)//导入
{
Workbook book = new Workbook(strFileName);
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
//获取excel中的数据保存到一个datatable中
DataTable dt_Import = cells.ExportDataTableAsString(1, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
// dt_Import.
return dt_Import;
}
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!