Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用openxml读取excel文件的单元格数据_Excel_C# 4.0_Openxml - Fatal编程技术网

如何使用openxml读取excel文件的单元格数据

如何使用openxml读取excel文件的单元格数据,excel,c#-4.0,openxml,Excel,C# 4.0,Openxml,我正在使用DocumentFormat.openxml-nuget-pkg读取.xlsx文件。我试图读取日期单元格[G2]和从[b5]到[m5]的数据 static void Main(字符串[]args) { string sFileTypeError=string.Empty; 字符串myFilePath=@“C:\Landing\file.xlsx”; //字符串ext=Path.GetExtension(myFilePath); var fileName=Path.GetFileNam

我正在使用DocumentFormat.openxml-nuget-pkg读取.xlsx文件。我试图读取日期单元格[G2]和从[b5]到[m5]的数据

static void Main(字符串[]args)
{
string sFileTypeError=string.Empty;
字符串myFilePath=@“C:\Landing\file.xlsx”;
//字符串ext=Path.GetExtension(myFilePath);
var fileName=Path.GetFileName(myFilePath);
var fileExtension=Path.GetExtension(文件名);
if((文件扩展名!=“.xlsx”)&&(文件扩展名!=“.xls”))
sFileTypeError=“无效文件。\n\n请浏览要上载的正确Excel文件。”;
其他的
{
使用(SpreadsheetDocument doc=SpreadsheetDocument.Open(myFilePath,false))
{
IEnumerable sheets=doc.WorkbookPart.Workbook.GetFirstChild().Elements();
工作表部件工作表部件=(工作表部件)doc.WorkbookPart.GetPartById(sheets.First().Id.Value);
IEnumerable rows=worksheetPart.Worksheet.GetFirstChild().subjects();
}
}
}
请让我知道你怎么能读这个。谢谢

如果您想一行一行地进行,然后一个单元格一个单元格地进行,您可以执行以下操作:

foreach (Row actualRow in rows)
{
    IEnumerable<Cell> cells = actualRow.Descendants<Cell>();
    foreach (Cell actualCell in cells)
    {
         // It gets the good value if it's not in the shared string table
         string value = actualCell.CellValue.Text;
    }
}
foreach(行实际行数)
{
IEnumerable cells=actualRow.subjects();
foreach(单元格中的单元格实际单元格)
{
//如果它不在共享字符串表中,它将获得良好的值
字符串值=actualCell.CellValue.Text;
}
}
foreach (Row actualRow in rows)
{
    IEnumerable<Cell> cells = actualRow.Descendants<Cell>();
    foreach (Cell actualCell in cells)
    {
         // It gets the good value if it's not in the shared string table
         string value = actualCell.CellValue.Text;
    }
}