c#讀寫excel文件應用示例。本站提示廣大學習愛好者:(c#讀寫excel文件應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#讀寫excel文件應用示例正文
由於支撐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;
}