简体   繁体   English

使用FileHelpers,如何从上传的Excel文件中提取记录

[英]Using FileHelpers, how to extract records from an uploaded Excel file

One of the bundled examples of FileHelpers shows how to extract and insert records from/to a Microsoft Excel Worksheet as follow: FileHelpers的捆绑示例之一显示了如何从Microsoft Excel工作表中提取记录或将记录插入到Microsoft Excel工作表中,如下所示:

Define a class with members that correspond to the columns in the input Excel file 用与输入Excel文件中的列相对应的成员定义一个类

[DelimitedRecord("|")]
public class CustomersVerticalBar
{
    public string CustomerID;
    public string CompanyName;
    public string ContactName;
    public string ContactTitle;
    public string Address;
    public string City;
    public string Country;
}

Then use ExcelDataStorage to extract records as follow: 然后使用ExcelDataStorage提取记录,如下所示:

ExcelStorage provider = new ExcelStorage(typeof(CustomersVerticalBar));
provider.StartRow = 3;
provider.StartColumn = 2;           
provider.FileName = "Customers.xls";
CustomerVerticalBar[] res = (CustomerVerticalBar[]) provider.ExtractRecords();

The problem is if there's no input Excel file, but the input is only available as an input stream from an uploaded file (from HttpPostedFileBase for example), what would be the solution in such case since ExcelDataStorage only takes a filename as input and not an input stream? 问题是,如果没有输入Excel文件,但输入仅作为上载文件(例如,来自HttpPostedFileBase的文件)的输入流提供,那么在这种情况下的解决方案是什么,因为ExcelDataStorage仅将文件名作为输入而不是文件名。输入流? It's OK if the solution uses some class other than ExcelDataStorage. 如果解决方案使用ExcelDataStorage以外的其他类也可以。

您应该将其写入文件,然后在处理后根据需要将其删除,因为该库可能需要加载部分数据而不是全部数据,并且可能需要多次引用该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM