c#讀寫(xiě)excel文件使用示例
因?yàn)橹С謈sv,所以就一塊寫(xiě)上了
Workbook,Worksheet using Aspose.Cells(第三方)
把Excel讀取到屬性對(duì)象列表,需要傳入對(duì)象類(lèi)型和文件路徑.
例:
List<PropSetCurrency> currencyList = this.GetObjectList<PropSetCurrency>(filePath);
注:Excel的表頭需要和對(duì)象名對(duì)應(yīng)(可無(wú)序),且第一列不能為空
把屬性對(duì)象列表保存到Excel,需要傳入對(duì)象列表和保存的文件完整路徑.
例:
this.SetExcelList(currencyList,"c://currencyList.excel");
/// <summary>
/// 從Excel獲取數(shù)據(jù)
/// </summary>
/// <typeparam name="T">對(duì)象</typeparam>
/// <param name="filePath">文件完整路徑</param>
/// <returns>對(duì)象列表</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];
// 獲取標(biāo)題列表
var titleDic = this.GetTitleDic(sheet);
// 循環(huán)每行數(shù)據(jù)
for (int i = 1; i < int.MaxValue; i++)
{
// 行為空時(shí)結(jié)束
if (string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue))
{
break;
}
T instance = new T();
// 循環(huán)賦值每個(gè)屬性
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
{
// 根據(jù)類(lèi)型進(jìn)行轉(zhuǎn)換賦值
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)
{
// 獲取錯(cuò)誤
}
}
}
}
list.Add(instance);
}
return list;
}
/// <summary>
/// 把對(duì)象List保存到Excel
/// </summary>
/// <typeparam name="T">對(duì)象</typeparam>
/// <param name="objList">對(duì)象列表</param>
/// <param name="saveFilePath">保存文件的完整路徑,包括文件類(lèi)型</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];
// 凍結(jié)第一行
sheet.FreezePanes(1, 1, 1, 0);
// 循環(huán)插入每行
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++;
}
// 循環(huán)插入當(dāng)前行的每列
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>
/// 獲取標(biāo)題行數(shù)據(jù)
/// </summary>
/// <param name="sheet">sheet</param>
/// <returns>標(biāo)題行數(shù)據(jù)</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;
}
相關(guān)文章
C#實(shí)現(xiàn)多線(xiàn)程寫(xiě)入同一個(gè)文件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)多線(xiàn)程寫(xiě)入同一個(gè)文件的方法,涉及C#多線(xiàn)程操作文件讀寫(xiě)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08WPF使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖
這篇文章主要為大家詳細(xì)介紹了WPF如何使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下2024-02-02.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器
這篇文章主要介紹了C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-07-07C#實(shí)現(xiàn)類(lèi)似jQuery的方法連綴功能
這篇文章主要介紹了C#實(shí)現(xiàn)類(lèi)似jQuery的方法連綴功能,可以簡(jiǎn)化語(yǔ)句,使代碼變得清晰簡(jiǎn)單,感興趣的小伙伴們可以參考一下2015-11-11C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細(xì)介紹了C#如何利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫(kù)實(shí)例詳解
這篇文章主要介紹了C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫(kù)的方法,以實(shí)例形式較為詳細(xì)的分析了C#連接SQL SERVER數(shù)據(jù)庫(kù)的相關(guān)步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11c#讀寫(xiě)App.config,ConfigurationManager.AppSettings 不生效的解決方法
這篇文章主要介紹了c#讀寫(xiě)App.config,ConfigurationManager.AppSettings 不生效的解決方法,需要的朋友可以參考下2015-10-10