【发布时间】:2020-07-02 11:02:32
【问题描述】:
读取包含财务数据的 excel 文件并使用 ExcelMapper 将其映射到对象。
public static async IAsyncEnumerable<TReceipt> ReadBankEntry(string file)
{
using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite))
{
ExcelMapper mapper = new ExcelMapper()
{
HeaderRowNumer = 0 // zero is the default
MinRowNumber = 2 // actual data starts from here (skipping row=1)
// MaxRowNumber = ? this is dynamic and can change in every excel file.
};
// my mapping table here
foreach (TReceipt bankEntry in await mapper.FetchAsync<TReceipt>(stream, "Sheet1"))
{
yield return bankEntry;
}
};
}
文件中的最后 3 行包含我不需要的信息,并且在某些列中还包含应该只包含十进制类型的字符串值,这最终会引发异常。从这一行开始,有些列也是空的。
例子-
+----+-----------------+--------------+
| SL | Date | P1 |
+----+-----------------+--------------+
| | Opening Balance | USD 10254.66 |
| 1 | 01-07-2020 | 445.25 |
| 2 | 01-07-2020 | 234.80 |
| 3 | 02-07-2020 | 13.00 |
| | Total | USD 10947.71 |
+----+-----------------+--------------+
我想在数据到达此行后停止读取数据。如何使用 ExcelMapper 做到这一点?
编辑:任何其他具有类似功能的库都可以。请注意,文件将具有 xls 扩展名(旧格式)。
【问题讨论】:
标签: c# .net excel .net-core poco