c#读写excel文件使用示例

c#读写excel文件使用示例,第1张

概述因为支持csv,所以就一块写上了Workbook,WorksheetusingAspose.Cells(第三方)把Excel读取到属性对象列表,需要传入对象类型和文件路径.例:List<PropSetCurrency>currencyList=this.GetObjectList<PropSetCur

因为支持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,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;
}

总结

以上是内存溢出为你收集整理的c#读写excel文件使用示例全部内容,希望文章能够帮你解决c#读写excel文件使用示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1261907.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存