gpt4 book ai didi

c# - 在 C# 中将 excel 工作表提取到字符串中

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

如何使用 C# 将 excel 工作表提取到字符串中?我已经有了工作表,可以另存为 txt 文件,但不是那个我想直接提取成字符串,做一些处理。

最佳答案

尝试使用出色的 EPPlus 允许您加载电子表格并以编程方式访问所有单元格的库。

举几个例子:

//Select all cells in column d between 9990 and 10000
var query1= (from cell in sheet.Cells["d:d"] where cell.Value is double && (double)cell.Value >= 9990 && (double)cell.Value <= 10000 select cell);
//In combination with the Range.Offset method you can also check values of other columns...


//Here we use more than one column in the where clause.
//We start by searching column D, then use the Offset method to check the value of column C.
var query3 = (from cell in sheet.Cells["d:d"]
where cell.Value is double &&
(double)cell.Value >= 9500 && (double)cell.Value <= 10000 &&
cell.Offset(0, -1).Value is double && //Column C is a double since its not a default date format.
DateTime.FromOADate((double)cell.Offset(0, -1).Value).Year == DateTime.Today.Year+1
select cell);

Console.WriteLine();
Console.WriteLine("Print all cells with a value between 9500 and 10000 in column D and the year of Column C is {0} ...", DateTime.Today.Year + 1);
Console.WriteLine();

count = 0;
//The cells returned here will all be in column D, since that is the address in the indexer.
//Use the Offset method to print any other cells from the same row.
foreach (var cell in query3)
{
Console.WriteLine("Cell {0} has value {1:N0} Date is {2:d}", cell.Address, cell.Value, DateTime.FromOADate((double)cell.Offset(0, -1).Value));
count++;
}

关于c# - 在 C# 中将 excel 工作表提取到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4973530/

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