时间:2021-05-20
因为支持csv,所以就一块写上了
Workbook,Worksheet using Aspose.Cells(第三方)
把Excel读取到属性对象列表,需要传入对象类型和文件路径.
例:
List<PropSetCurrency> currencyList = this.GetObjectList<PropSetCurrency>(filePath);
注:Excel的表头需要和对象名对应(可无序),且第一列不能为空
把属性对象列表保存到Excel,需要传入对象列表和保存的文件完整路径.
例:
this.SetExcelList(currencyList,"c://currencyList.excel");
复制代码 代码如下:
/// <summary>
/// 从Excel获取数据
/// </summary>
/// <typeparam name="T">对象</typeparam>
/// <param name="filePath">文件完整路径</param>
/// <returns>对象列表</returns>
public List<T> GetObjectList<T>(string filePath) where T : new()
{
List<T> list = new List<T>();
if (!filePath.Trim().EndsWith("csv") && !filePath.Trim().EndsWith("xlsx"))
{
return list;
}
Type type = typeof(T);
Workbook workbook = new Workbook(filePath);
Worksheet sheet = workbook.Worksheets[0];
// 获取标题列表
var titleDic = this.GetTitleDic(sheet);
// 循环每行数据
for (int i = 1; i < int.MaxValue; i++)
{
// 行为空时结束
if (string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue))
{
break;
}
T instance = new T();
// 循环赋值每个属性
foreach (var item in type.GetProperties())
{
if (titleDic.ContainsKey(item.Name))
{
string str = sheet.Cells[i, titleDic[item.Name]].StringValue;
if (!string.IsNullOrEmpty(str))
{
try
{
// 根据类型进行转换赋值
if (item.PropertyType == typeof(string))
{
item.SetValue(instance, str);
}
else if (item.PropertyType.IsEnum)
{
item.SetValue(instance, int.Parse(str));
}
else
{
MethodInfo method = item.PropertyType.GetMethod("Parse", new Type[] { typeof(string) });
object obj = null;
if (method != null)
{
obj = method.Invoke(null, new object[] { str });
item.SetValue(instance, obj);
}
}
}
catch (Exception)
{
// 获取错误
}
}
}
}
list.Add(instance);
}
return list;
}
/// <summary>
/// 把对象List保存到Excel
/// </summary>
/// <typeparam name="T">对象</typeparam>
/// <param name="objList">对象列表</param>
/// <param name="saveFilePath">保存文件的完整路径,包括文件类型</param>
public void SetExcelList<T>(List<T> objList, string saveFilePath)
{
if (!saveFilePath.Trim().EndsWith("csv") && !saveFilePath.Trim().EndsWith("xlsx"))
{
return;
}
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// 冻结第一行
sheet.FreezePanes(1, 1, 1, 0);
// 循环插入每行
int row = 0;
foreach (var obj in objList)
{
int column = 0;
var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly);
if (row == 0)
{
foreach (var titName in properties)
{
sheet.Cells[0, column].PutValue(titName.Name);
column++;
}
row++;
}
// 循环插入当前行的每列
column = 0;
foreach (var property in properties)
{
var itemValue = property.GetValue(obj);
sheet.Cells[row, column].PutValue(itemValue.ToString());
column++;
}
row++;
}
workbook.Save(saveFilePath);
}
/// <summary>
/// 获取标题行数据
/// </summary>
/// <param name="sheet">sheet</param>
/// <returns>标题行数据</returns>
private Dictionary<string, int> GetTitleDic(Worksheet sheet)
{
Dictionary<string, int> titList = new Dictionary<string, int>();
for (int i = 0; i < int.MaxValue; i++)
{
if (sheet.Cells[0, i].StringValue == string.Empty)
{
return titList;
}
titList.Add(sheet.Cells[0, i].StringValue, i);
}
return titList;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前一篇文章:“.NET读写Excel工具Spire.Xls使用(2)Excel文件的控制”给大家介绍了C#使用Spire.XLS来控制Excel文件的基本功能和
C#FileStream类在C#语言中文件读写流使用FileStream类来表示,FileStream类主要用于文件的读写,不仅能读写普通的文本文件,还可以读取
本文实例讲述了C#使用oledb操作excel文件的方法。分享给大家供大家参考。具体分析如下:不管什么编程语言都会提供操作Excel文件的方式,C#操作Exce
本文实例为大家分享了C#语言实现ini文件读写操作的具体代码,供大家参考,具体内容如下1、ini文件是什么?见百度百科2、C#语言实现ini文件的读写操作///
通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。但C#本身并不具备读写INI文件的API,只有通过调用非托