【问题标题】:C# Read data using using Microsoft.Office.Interop.ExcelC# 使用 Microsoft.Office.Interop.Excel 读取数据
【发布时间】:2016-09-20 06:40:40
【问题描述】:

我有一个数据表,我需要根据用户输入从 excel 中读取它,并将其作为数组存储在 VS 中。

如果用户输入C1,搜索并获取相关数据:

数组[0]:X1E1M101

数组[1]:F2G1M202

如果用户输入C2:

数组[0]:X1E1M105

数组[1]:F1G2M304


我的数据:

     A      B      C     D     E
1   C1
2
3   X1     E1     M1     01 
4   F2     G1     M2     02
5
6   C2
7
8   X1     E1     M1     05 
9   F1     G2     M3     04
10

我的代码:

//I declared the Interop
using Excel = Microsoft.Office.Interop.Excel;


        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(ManufacturingFile);
        //xlWorkSheet = ("Default Value");  // i get an error here when trying to define the worksheet name i want to select. "Cannot impicitly convert type string to ... excel.worksheet'

        xlWorkSheet = ((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select(); // This method also I get an error.
        xlWorkSheet.Activate(); 

在这部分之后我被困住了,因为我是使用互操作的新手。 希望有人可以帮助我,我是 C# 的初学者,非常感谢您的帮助。

【问题讨论】:

  • 访问this帖子,了解Interop库读取excel文件的详细用法
  • stackoverflow 上已经有几篇文章了 - 请参阅:stackoverflow.com/questions/36844037
  • 贴出的代码甚至不是合法的C#代码。

标签: c# arrays excel


【解决方案1】:

你必须打开你的工作簿和工作表,有很多例子解释了如何做到这一点,这里有一个示例

      Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;

        string str;
        int rCnt = 0;
        int cCnt = 0;

        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open("testone.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


        range = xlWorkSheet.UsedRange;

        Microsoft.Office.Interop.Excel.Range xlFound =range.EntireRow.Find("C2",misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlNext,true, misValue, misValue);

        if (!(xlFound == null))
        {
            int ID_Number = xlFound.Column;
            int rownum = xlFound.Row;
            Console.WriteLine(ID_Number);
            Console.WriteLine(rownum);
            Console.Read();
        }

您可以首先获取搜索的范围值,例如,如果“C1”位于 a1,则您必须从 a(n+2) 读取整行并在找到空行时停止。

以上代码未编译,取自here

【讨论】:

  • 嗨@akhila 你如何进行搜索以找到“C1”的行和列?我设法写到选择特定的工作表。
  • 嘿@Manick9 这应该可以帮助你herethis link 这里还展示了如何选择列 ID,您可以根据自己的情况选择行和列。希望它有帮助..
  • 谢谢,我之前在看它。但是,我收到一个错误,即 currentFind 未找到或未声明。
  • 能否请您分享代码和错误详细信息。这样会更容易理解情况
  • @Manick9 请在上面找到更新后的代码以获取行和列值
【解决方案2】:

如果您只想从 excel 文件中读取数据,我建议您改用 ExcelDataReader 库,与 Interop 相比,它提供了更好的性能并且不会留下幽灵进程。以下是一些设置示例代码:

    IExcelDataReader reader = null;

    string FilePath = "PathToExcelFile";

    //Load file into a stream
    FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);

    //Must check file extension to adjust the reader to the excel file type
    if (System.IO.Path.GetExtension(FilePath).Equals(".xls"))
    {
        reader = ExcelReaderFactory.CreateBinaryReader(stream);
    }
    else if (System.IO.Path.GetExtension(FilePath).Equals(".xlsx"))
    {
        reader = ExcelReaderFactory.CreateBinaryReader(stream);
    }

    if (reader != null)
    {
        //Fill DataSet
        System.Data.DataSet result = reader.AsDataSet();
        try
        {
            //Loop through rows for the desired worksheet
            //In this case I use the table index "0" to pick the first worksheet in the workbook
            foreach (DataRow row in result.Tables[0].Rows)
            {
                string FirstColumn = row[0].ToString();
            }
        }
        catch
        {

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多