gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:48:49 25 4
gpt4 key购买 nike

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

我正在尝试从 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 查询部分时显示的错误。请问我怎样才能让它工作?

enter image description here

最佳答案

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

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);
}

}
}

关于c# - 使用C#读取Excel文件并清理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com