简体   繁体   English

如何读取Excel单元格值?

[英]How to read Excel cell value?

Looking for help on how to read a few cell values from Excel. 寻找有关如何从Excel中读取一些单元格值的帮助。 I thought I'd start with one. 我以为我会从一个开始。

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false,    Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cell = (Excel.Range)excelWorksheet.Cells[1, 1];

Though I am having no luck. 虽然我没有运气。 I want to be able to read the value of the cell and store it somewhere, and then have it check every few seconds for the value. 我希望能够读取单元格的值并将其存储在某个位置,然后每隔几秒钟检查一次该值。

You seriously want to take a look at EPPLus: http://epplus.codeplex.com/ 您很想看看EPPLus: http ://epplus.codeplex.com/

It's available from NuGet for usage in your projects. NuGet可以在您的项目中使用它。 It's by far the most easy and clean way of interfacing between C# and an excel document. 到目前为止,这是C#和excel文档之间最简单,最干净的接口方式。 It's also free, btw. 它也是免费的,顺便说一句。

Tutorial: http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-Report-in#1 教程: http//www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-Report-in#1

private DataTable WorksheetToDataTable(ExcelWorksheet oSheet)
{
int totalRows = oSheet.Dimension.End.Row;
int totalCols = oSheet.Dimension.End.Column;
DataTable dt = new DataTable(oSheet.Name);
DataRow dr = null;
for (int i = 1; i <= totalRows; i++)
{
    if (i > 1) dr = dt.Rows.Add();
    for (int j = 1; j <= totalCols; j++)
    {
        if (i == 1)
            dt.Columns.Add(oSheet.Cells[i, j].Value.ToString());
        else
            dr[j - 1] = oSheet.Cells[i, j].Value.ToString();
    }
}
return dt;

} }

This reads all the columns and rows in EPPLus. 这将读取EPPLus中的所有列和行。

Something like: 就像是:

Worksheet sheet = excelWorkbook.Worksheets["Sheet1"] as Worksheet;
Range rng = sheet.Cells[1,1] as Range;
object value = rng.Value;

...should do what you want. ...应该做你想做的。 Try this and if you get any errors let me know. 试试这个,如果您遇到任何错误,请告诉我。

The value will either be a string , double , DateTime , bool or int value. 该值可以是stringdoubleDateTimeboolint值。

Aside from the int s, these are quite self-explanatory. 除了int之外,这些都是不言自明的。 The int values represent the different error codes that are possible, eg #VALUE! int值表示可能的不同错误代码,例如#VALUE!

(Incidentally, rather than referencing the Excel PIA, I would recommend usingn NetOffice, which is available free via NuGet, to reference the Excel object model. Advantage being that you are not tied to a specific version of Excel when you deploy) (顺便说一句,我建议您使用NetOffice而不是参考Excel PIA,而可以通过NuGet免费获得该NetOffice来参考Excel对象模型。优点是部署时您不必绑定到特定版本的Excel)

You need to retrieve the .Value or .Value2 property of the cell: 您需要检索单元格的.Value.Value2属性:

if (cell.Value2 == null)
    // cell is blank
else if(cell.Value2 is string)
    // cell is text
else if (cell.Value is double)
    // cell is number;
else if (cell.Value2 is double)
    // cell is date

Note the last two if 's. 注意最后两个if .Value2 will return a double for both numbers and dates, but .Value will return a double only for numbers. .Value2将为数字和日期返回双.Value2 ,但是.Value将仅为数字返回双.Value2 The order that you check is important. 您检查的顺序很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM