/// <summary>
/// excel 导出列辅助类
/// </summary>
public class Col
{ /// <summary>
/// 中文名(显示名称)
/// </summary>
public string Name { get; set; }
/// <summary>
/// 英文(数据库查询字段)
/// </summary>
public string ColName { get; set; }
}
public class AsposeExportExcel
{
/// <summary>
/// 将list 导出excel
/// </summary>
/// <typeparam name="T">model</typeparam>
/// <param name="list">需要导出的list数据集</param>
/// <param name="sheetName">标题</param>
/// <param name="cols">需要导出的字段</param>
/// <returns>excel保存的路径</returns>
public string ExportExecl<T>(IList<T> list, string sheetName, List<Col> cols) where T : class, new()
{
string rptPath = AppContext.BaseDirectory + "Report/WorkNoteList.xls";
try
{
#region Aspose 创建工作簿
/*创建工作簿*/
Workbook workbook = new Workbook(rptPath);
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//单元格
//标题
Style title = workbook.Styles[workbook.Styles.Add()];
title.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
// cells.Merge(0, 0, 1, cols.Count);//合并单元格 前两个为单元格坐标 第三个参数为合并多少行,第四个参数合并多少列
title.Font.Size = 25;
title.Font.IsBold = true;//粗体
// 表头
Style header = workbook.Styles[workbook.Styles.Add()];
header.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
header.Font.Size = 14;
header.Font.IsBold = true;//粗体
header.ForegroundColor = System.Drawing.Color.FromArgb(204, 204, 204);//设置背景色 可以参考颜色代码对照表
header.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线
header.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
header.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线
header.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线
//边框
Style border = workbook.Styles[workbook.Styles.Add()];
border.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
border.IsTextWrapped = true;//单元格内容自动换行
border.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线
border.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
border.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线
border.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线
#endregion
int row = 0;
cells[row, 0].PutValue(sheetName);
cells[row, 0].SetStyle(title);
cells.Merge(row, 0, 1, cols.Count);
cells.SetColumnWidth(0, 30);//设置列宽
//生成列名
row++;
for (int i = 0; i < cols.Count; i++)
{
cells[row, i].PutValue(cols[i].Name);
cells[row, i].SetStyle(header);
}
//生成数据
if (list != null && list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
row++;
for (int j = 0; j < cols.Count; j++)
{
string Code = cols[j].ColName;
if (Code.Contains("DateTime"))
{
string code = typeof(T).GetProperty(Code).GetValue(list[i]).ToString();
DateTime date = DateTime.Parse(code);
cells[row, j].PutValue(date.ToString("yyyy-MM-dd").ToString());
}
//else if (Code == "images")
//{
// int index = sheet.Pictures.Add(row, j, row+1, j+1, "C:\\Users\\aaa\\Pictures\\b4efae6eddc451da139cac37b6fd5266d11632ba.jpg");
// Aspose.Cells.Drawing.Picture picture = sheet.Pictures[index];
// picture.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
// Aspose.Cells.Hyperlink link = picture.AddHyperlink("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1398895269,2245330748&fm=26&gp=0.jpg");
// link.ScreenTip = "单击跳转到图片";
//}
else
{
cells[row, j].PutValue(typeof(T).GetProperty(Code).GetValue(list[i]));
}
cells[row, j].SetStyle(border);
}
}
}
#region 验证是否存在文件夹
string path = "/Upload/";
string rootPath = AppContext.BaseDirectory + path;
if (!System.IO.Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
//删除文件
DirectoryInfo dir = new DirectoryInfo(rootPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
#endregion
string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
string savePath = rootPath + FileName;
workbook.Save(savePath);
return "Upload/" + FileName;
}
catch (Exception e)
{
return " IListToExcel: " + e.Message;
}
}
/// <summary>
/// 导入excel 返回DataTable
/// </summary>
/// <param name="strFileName">excel文件地址</param>
/// <returns>DataTable</returns>
public System.Data.DataTable ReadExcel(String strFileName)
{
Workbook book = new Workbook(strFileName);
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
}
}