在 C# 中将 Excel 工作表提取为字符串

发布于 2024-10-16 19:06:17 字数 82 浏览 4 评论 0原文

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

How can i extract excel worksheet into a string using C#?
i already have the sheet and can save as txt file but instead of that
I want to extract it into a string directly and do some processing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懷念過去 2024-10-23 19:06:17

尝试使用出色的 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++;
}

Try using a brilliant EPPlus library which allows you to load the spreadsheet and access all cells programatically.

Here's a few examples:

//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++;
}
下雨或天晴 2024-10-23 19:06:17

类似以下(未经测试)的代码应该可以工作:

Excel.Application oXL= new Excel.Application();
oXL.Visible = true;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
string s = (string)oSheet.Cells[1, 1].Value;

尽管查看此示例以了解如何清理正确地安排一切等等。

Something like the following (untested) code should work:

Excel.Application oXL= new Excel.Application();
oXL.Visible = true;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
string s = (string)oSheet.Cells[1, 1].Value;

Though look at this sample to see how to clean up everything properly etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文