前言: 最近做了个项目,用了Entity FrameWork +.NET MVC,前台用了EasyUI的Jquery框架,记录一下学到的一些东西,方便以后的工作。。
1、将List<>泛型集合类直接导入excel
突然在网上发现的第三方开源组件【Epplus】(做的项目少,out了)。官方解释:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件,支持对excel文档的汇入汇出,图表(excel自带的图表基本都可以实现)的列印.
最重要的一点是【支持Excel 2007/2010】。他的好处就是不依赖 Microsoft Excel的COM组件,不要求你的服务器必须安装excel。。(感觉超方便了)
不说废话,先贴代码:
#region 保存数据列表到Excel(泛型)+void SaveToExcel<T>(IEnumerable<T> data, string FileName, string OpenPassword = "")
/// <summary>
/// 保存数据列表到Excel(泛型)
/// </summary>
/// <typeparam name="T">集合数据类型</typeparam>
/// <param name="data">数据列表</param>
/// <param name="FileName">Excel文件</param>
/// <param name="OpenPassword">Excel打开密码</param>
public static void SaveToExcel<T>(IEnumerable<T> data, string FileName, string OpenPassword = "")
{
FileInfo file = new FileInfo(FileName);
try
{
using (ExcelPackage ep = new ExcelPackage(file, OpenPassword))
{
ExcelWorksheet ws = ep.Workbook.Worksheets.Add(typeof(T).Name);
ws.Cells["A1"].LoadFromCollection(data, true, TableStyles.Medium10);
ExcelWorksheet worksheet = ep.Workbook.Worksheets[1];
int colStart = worksheet.Dimension.Start.Column; //工作区开始列
int colEnd = worksheet.Dimension.End.Column; //工作区结束列
int rowStart = worksheet.Dimension.Start.Row; //工作区开始行号
int rowEnd = worksheet.Dimension.End.Row; //工作区结束行号
//将每列标题添加到字典中
for (int i = colStart; i <= colEnd; i++)
{
string Name = worksheet.Cells[rowStart, i].Value.ToString();
if (Name.Contains("ID"))
{
//worksheet.get_Range(worksheet.Cells[rowStart, i].d
}
}
ep.Save(OpenPassword);
}
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
}
#endregion
#region 从Excel中加载数据(泛型)+IEnumerable<T> LoadFromExcel<T>(string FileName) where T : new()
/// <summary>
/// 从Excel中加载数据(泛型)
/// </summary>
/// <typeparam name="T">每行数据的类型</typeparam>
/// <param name="FileName">Excel文件名</param>
/// <returns>泛型列表</returns>
public static IEnumerable<T> LoadFromExcel<T>(string FileName) where T : new()
{
FileInfo existingFile = new FileInfo(FileName);
List<T> resultList = new List<T>();
Dictionary<string, int> dictHeader = new Dictionary<string, int>();
using (ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colStart = worksheet.Dimension.Start.Column; //工作区开始列
int colEnd = worksheet.Dimension.End.Column; //工作区结束列
int rowStart = worksheet.Dimension.Start.Row; //工作区开始行号
int rowEnd = worksheet.Dimension.End.Row; //工作区结束行号
//将每列标题添加到字典中
for (int i = colStart; i <= colEnd; i++)
{
dictHeader[worksheet.Cells[rowStart, i].Value.ToString()] = i;
}
List<PropertyInfo> propertyInfoList = new List<PropertyInfo>(typeof(T).GetProperties());
for (int row = rowStart + 1; row < rowEnd; row++)
{
T result = new T();
//为对象T的各属性赋值
foreach (PropertyInfo p in propertyInfoList)
{
try
{
ExcelRange cell = worksheet.Cells[row, dictHeader[p.Name]]; //与属性名对应的单元格
if (cell.Value == null)
continue;
switch (p.PropertyType.Name.ToLower())
{
case "string":
// p.SetValue(propertyInfoList, result, cell.GetValue<String>());
break;
case "int16":
// p.SetValue(result, cell.GetValue<Int16>());
break;
case "int32":
// p.SetValue(result, cell.GetValue<Int32>());
break;
case "int64":
// p.SetValue(result, cell.GetValue<Int64>());
break;
case "decimal":
// p.SetValue(result, cell.GetValue<Decimal>());
break;
case "double":
// p.SetValue(result, cell.GetValue<Double>());
break;
case "datetime":
// p.SetValue(result, cell.GetValue<DateTime>());
break;
case "boolean":
// p.SetValue(result, cell.GetValue<Boolean>());
break;
case "byte":
// p.SetValue(result, cell.GetValue<Byte>());
break;
case "char":
// p.SetValue(result, cell.GetValue<Char>());
break;
case "single":
// p.SetValue(result, cell.GetValue<Single>());
break;
default:
break;
}
}
catch (KeyNotFoundException ex)
{ }
}
resultList.Add(result);
}
}
return resultList;
}
#endregion
备注:只是用了导出到EXCEl的方法,读取excel的方法还有待验证。。
官网地址:http://epplus.codeplex.com/
以下为转帖:
转自http://www.cnblogs.com/liudeyun/p/3535740.html
官网地址:http://epplus.codeplex.com/
以下为转帖:
转自http://www.cnblogs.com/liudeyun/p/3535740.html
1> 添加dll文件至工程bin文件中
2>在程式中添加引用
using OfficeOpenXml; using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style;
3>所有的操作语句需要放置在下面的using中
using (ExcelPackage package = new ExcelPackage()) { }
4.添加新的sheet
var worksheet = package.Workbook.Worksheets.Add(“sheet1");
5.单元格赋值,这里多说一句,NPOI必须先创建单元格,然后再给单元格赋值,而Epplus不需要,直接找到单元格进行赋值就可以了.
worksheet.Cells[int row, int col].Value = “”; 或者 worksheet.Cells["A1"].Value = “”;
6.合并单元格
worksheet.Cells[int fromRow, fromCol, int toRow,int toCol].Merge = true;
7.获取某一个区域
var rangeData= worksheet.Cells[fromRow, fromCol, toRow, toCol];
8.设置字体
worksheet.Cells.Style.Font.Name= “正楷”;
worksheet.Cells.Style.Font.Color
worksheet.Cells.Style.Font.Size
9.设置边框的属性
worksheet.Cells.Style.Border.Left.Style= ExcelBorderStyle.Thin ; worksheet.Cells.Style.Border.Right.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Top.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Bottom.Style= ExcelBorderStyle.Thin;
10.对齐方式
worksheet.Cells.Style.HorizontalAlignment=ExcelHorizontalAlignment.Center;
worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
11. 设置整个sheet的背景色
worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid;
worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
12.折行显示
worksheet.Cells.Style.WrapText= true;
13.单元格自动适应大小
worksheet.Cells.Style.ShrinkToFit= true;
14.格式化单元格value值
worksheet.Cells.Style.Numberformat.Format= "0.00";
15.锁定
worksheet.Cells["A1"].Style.Locked= true;
注:此处锁定某一个单元格的时候,只有在整个sheet被锁定的情况下才可以被锁定,不然加上锁定属性也是不起作用的~~
二.
Epplus另一个出色的地方就是支持图表的列印.
功能的實現很簡單
,
難點在于需求比較細的點上
,
epplus
可能不好實現
,
但是總的來說是比較好的一個列印圖表的工具
1.简单介绍一下可以实现的图表类型:
直條圖、折綫圖、圓形圖、橫條圖、散佈圖、區域圖
等類型的圖表
2.使用:分为三步,
第一步是将需要显示在图表中的
数据列印到excel中.
第二步是创建所需要的图表类型(折线图为例)
var chart = (worksheet.Drawings.AddChart("LineChart", eChartType.Line) as ExcelLineChart);
第三步为图表添加第一步列印的数据区间就可以了
chart.Series.Add(Y軸顯示的數據源,X軸顯示的數據源)
3.图表的功能就这样实现了,很简单吧
此处图表的没有写出具体代码,因为觉得代码很简单,只是步骤的问题,上面三步走完,图表即可完成了.
再贴个学习地址:
http://www.cnblogs.com/rumeng/p/3785748.html
就先这样吧,不过好像中文的资料很少,用的再研究吧。。听说还有一个开源组件【NOPI】的开源框架,抽空再研究吧。
再贴个学习地址:
http://www.cnblogs.com/rumeng/p/3785748.html
就先这样吧,不过好像中文的资料很少,用的再研究吧。。听说还有一个开源组件【NOPI】的开源框架,抽空再研究吧。