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

标签 c# .net office-interop office-automation

如何使用 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/

相关文章:

c# - 循环遍历 appConfig 文件的不同部分

c# - 如何比较两个无序序列(列表和数组)是否相等?

C# Windows 服务 - 多个计时器

c# - 为什么 ToUpper 比 ToLower 快?

c# - 使用c#以编程方式设置word文档表格单元格边距

c# - 为带有子文件夹的 ASP.NET MVC 区域路由生成 URL

c# - 从 ASP.NET 页面下载 EXCEL 文件,无需在服务器上生成物理文件(动态)

c# - 如何使用编码的 UI 测试通过自定义 HTML 属性搜索元素

c# - 将多个(例如图像和文本)内容复制到剪贴板以粘贴到 MS Office C# Winform 中

c# - 如何在 OpenXML 段落、运行、文本中保留带格式的字符串?