Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
C# 如何从现有excel文件中读取单元格值_C#_Excel - Fatal编程技术网

C# 如何从现有excel文件中读取单元格值

C# 如何从现有excel文件中读取单元格值,c#,excel,C#,Excel,我想读取excel文件中的每个单元格值,但即使在NET中尝试了不同的示例,也无法获取单元格值。我没有得到以下代码的结果,任何人都可以得到这个。我正在使用.NETFramework2.0 string filePath = "F:/BulkTest.xlsx"; Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ExcelApp.Visibl

我想读取excel文件中的每个单元格值,但即使在NET中尝试了不同的示例,也无法获取单元格值。我没有得到以下代码的结果,任何人都可以得到这个。我正在使用.NETFramework2.0

string filePath = "F:/BulkTest.xlsx";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = ExcelApp.Workbooks.Open(filePath, Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);

Microsoft.Office.Interop.Excel.Worksheet sh = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets["Sheet1"];
Range excelRange = sh.UsedRange;

for (int i=2; i<= excelRange.Count + 1 ; i++)
{
    string values = sh.Cells[i,2].ToString();
}
stringfilepath=“F:/BulkTest.xlsx”;
Microsoft.Office.Interop.Excel.Application ExcelApp=新的Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible=true;
Microsoft.Office.Interop.Excel.Workbook wb=ExcelApp.Workbook.Open(文件路径,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value);
Microsoft.Office.Interop.Excel.Worksheet sh=(Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[“Sheet1”];
范围excelRange=sh.UsedRange;
对于(int i=2;i
到目前为止,我一直在尝试将单元格值直接带到变量,现在我将尝试将单元格值带到使用范围的数组中。谢谢!!!!–Teja Varma 13分钟前

不,我甚至不是这个意思:)正如我在评论中提到的,可以将整个范围存储在一个数组中。这并不意味着需要循环遍历每个单元格以将其存储在数组中。可以直接将范围的值指定给数组。看看这个例子

xlRng = xlWorkSheet.get_Range("A1", "A20");

Object arr = xlRng.Value;

foreach (object s in (Array)arr)
{
    MessageBox.Show(s.ToString());
}

正确答案是使用:

Sheet.Cells[row,col].Value.ToString();
C#代码
测试正常


完整代码

        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlBook;
        Excel.Worksheet xlSheet;

        string Path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

        xlBook = xlApp.Workbooks.Open(Path + "\\myfile.xlsx");
        xlApp.Visible = true;
        xlSheet = xlBook.ActiveSheet;

        string s = xlSheet.UsedRange.Cells[1, 7].Value.ToString();
        string d = xlSheet.UsedRange.Cells[1, "G"].Value.ToString();

        xlBook.Close();
        xlApp.Quit();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

wb.Sheets[“Sheet1”].ToString()?你在哪里读手机?要读取单元格A1,必须使用
sh.Cells[1,1]
不要迭代每个单独的单元格。那就是放慢速度。如果你做了一些可维护的事情,请使用excel阅读器库,如EPPLusNo。。。我给了您一个关于如何使用
.Cells[]
属性:)的示例,您所做的是在每个循环中读取单元格B2的值。为什么不将值存储在数组中而不是循环?好,回到基本问题。你为什么要遍历每一个单元格来获取它的值?您的主要目标是什么?我需要获取列中的所有值,并且必须将这些值传递给存储过程……由于某种原因,我在xlRng.Value上得到null。@DoodleKana查看excel文件是否正在保存。可能是文件处于编辑模式。这是常见的答案。它不起作用。我用Interop15和14试过了,结果发现一个CS1061错误
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlBook;
        Excel.Worksheet xlSheet;

        string Path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

        xlBook = xlApp.Workbooks.Open(Path + "\\myfile.xlsx");
        xlApp.Visible = true;
        xlSheet = xlBook.ActiveSheet;

        string s = xlSheet.UsedRange.Cells[1, 7].Value.ToString();
        string d = xlSheet.UsedRange.Cells[1, "G"].Value.ToString();

        xlBook.Close();
        xlApp.Quit();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);