使用C#读取Excel文件并清理数据

时间:2016-05-23 14:46:02

标签: c# excel linq

这是我第一次这样做,欢迎任何关于最佳实践的指导。

我试图从excel文件中获取数据,并且需要使用我获得的数据中的几列。我需要确保单元格中不存在换行符和其他特殊字符,并且某些列不是空白。

经过一些谷歌搜索后,香港专业教育学院设法获得了这个代码,它可以填充适配器。

在LINQ部分,我试图在我进一步处理数据之前过滤掉我需要的列,但这就是我卡住的地方。

对于我尝试创建新订单的每一行。我有一个名为Order的类,它现在只有我试图填充的属性。

class Program
{
    static void Main(string[] args)
    {
        var fileName = @"C:\Users\metesting\Bulkuploader\POS ORDERS2016.xlsx";
        var connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";

        var adapter = new OleDbDataAdapter("SELECT * FROM [report$]", connectionString);
        var ds = new DataSet();

        adapter.Fill(ds, "anyNameHere");

        var data = ds.Tables["anyNameHere"].AsEnumerable();

        var query = data.Where(x => x.Field<string>("Store POS Order Ref") != string.Empty).Select(x => new Order
        {
            CPMOrderNo = x.Field<string>("Store POS Order Ref"),
            StoreName = x.Field<string>("Store Name"),
            Street = x.Field<string>("Street"),
            Town = x.Field<string>("Town"),
            County = x.Field<string>("County"),
            PostCode = x.Field<string>("Post Code"),
            POSOrder = x.Field<string>("POS Order: Order Name"),
            OrderItemCode = x.Field<string>("Order Item: POS Code"),
            Quantity = x.Field<int>("Quantity"),
            AuthInStoreBy = x.Field<string>("Authorised In Store By")

        });
    }
}

订单类如下:

    public class Order
{
    public string CPMOrderNo { get; set; }
    public string StoreName { get; set; }
    public string Street { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string PostCode { get; set; }
    public string POSOrder { get; set; }
    public string OrderItemCode { get; set; }
    public int Quantity { get; set; }
    public string AuthInStoreBy { get; set; }

}

我不太清楚LINQ语句的语法!还在学习。

当我进入调试器中的var query部分时,这是显示的错误。我怎么能让这个工作好吗?

enter image description here

1 个答案:

答案 0 :(得分:3)

无效的强制转换异常来自以下行:

Quantity = x.Field<int>("Quantity"),

以下更改解决了问题:

(int)x.Field<double>("Quantity")

为了得到我的答案,我试图模仿您的Excel文件,如下图所示:ReplicatedExcelFile。 然后,我按如下方式修改了您的代码:

static void Main(string[] args)
{
    var fileName = @"C:/Users/Popper/Desktop/Stackoverflow/StackoverflowQuestion1.xlsx";
    var connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";

    var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
    var ds = new DataSet();

    adapter.Fill(ds, "anyNameHere");

    var data = ds.Tables["anyNameHere"].AsEnumerable();

    List<Order> orderList = new List<Order>();
    Order pickedOrder;
    foreach (var x in data)
    {
        if (x.Field<string>("Store POS Order Ref") != string.Empty && x.Field<string>("Store POS Order Ref") != null)
        {
            pickedOrder = new Order();

            pickedOrder.CPMOrderNo = x.Field<string>("Store POS Order Ref");
            pickedOrder.StoreName = x.Field<string>("Store Name");
            pickedOrder.Street = x.Field<string>("Street");
            pickedOrder.Town = x.Field<string>("Town");
            pickedOrder.County = x.Field<string>("County");
            pickedOrder.PostCode = x.Field<string>("Post Code");
            pickedOrder.POSOrder = x.Field<string>("POS Order: Order Name");
            pickedOrder.OrderItemCode = x.Field<string>("Order Item: POS Code");
            pickedOrder.Quantity = (int)x.Field<double>("Quantity");
            pickedOrder.AuthInStoreBy = x.Field<string>("Authorised In Store By");

            orderList.Add(pickedOrder);
        }

    }
}