2016-09-20 122 views
1

使用读取数据我有一个数据表,我需要根据用户输入从excel中读取数据并将其作为数组存储在VS中。C#使用Microsoft.Office.Interop.Excel

如果用户输入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#的初学者,非常感谢您的帮助。

+0

访问[this](https://coderwall.com/p/app3ya/read-excel-file-in-c)post的详细用法的Interop库for阅读excel文件 – Sagar

+1

有几个帖子已经在stackoverflow - 见:http://stackoverflow.com/questions/36844037/ –

+0

发布的代码甚至不是合法的C#代码。 –

回答

2

你将不得不打开woorkbook和工作表中,也有一些例子,说明如何做到这一点,这里是一个样本

 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处,您将不得不从(n + 2)中读取整行,并在发现空行时停止。

上面的代码是不编译它takenfrom here

+0

嗨@akhila你如何做搜索找到'C1'的行和列?我设法写下选择具体表。 – Manick9

+1

嗨@ Manick9这应该可以帮助你[这里](https://msdn.microsoft.com/en-us/library/e4x1k99a.aspx)。此外[这个链接](http://stackoverflow.com/questions/19973982/how-to-find-column-id-by-cell-value)这里显示如何选择列Id,你可以选择行和阴影你的情况。希望它有帮助.. – akhila

+0

谢谢我早些时候在看。但是,我得到一个错误,找不到或未声明currentFind。 – Manick9

1

如果你打算只读取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 
     { 

     } 
    } 
相关问题