2016-04-06 8 views
0

を使用して配列にデータを取得するファイルパスは@ "E:\ BCFNA-orig-1.xsl" Excelファイルは9列と500行で構成されています。各行を配列に格納する[] NumberOfInputs = {7,4,4,4,2,4,5,5,0}; 「アレイ内の値は、Excelファイルから取得することになっている、私のプログラムでは、次の行からデータを取得するよりも、それを使用しています。excelile "* .xlsx"からC#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.OleDb; 
using System.IO; 

namespace ConsoleApplication3 
{ 
class Program 
{ 
    static void Main() 
    { 
    } 


public class SomethingSometingExcelClass 
{ 
    public void DoSomethingWithExcel(string filePath) 
    { 

     List<DataTable> worksheets = ImportExcel(filePath); 
     foreach(var item in worksheets){ 
      foreach (DataRow row in item.Rows) 
      { 
       //add to array 

      }    
     } 
    } 
    /// <summary> 
    /// Imports Data from Microsoft Excel File. 
    /// </summary> 
    /// <param name="FileName">Filename from which data need to import data  
    /// <returns>List of DataTables, based on the number of sheets</returns> 
    private List<DataTable> ImportExcel(string FileName) 
    { 
     List<DataTable> _dataTables = new List<DataTable>(); 
     string _ConnectionString = string.Empty; 
     string _Extension = Path.GetExtension(FileName); 
     //Checking for the extentions, if XLS connect using Jet OleDB       

     _ConnectionString = 
       "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\BCFNA- 

      orig-1.xls;Extended  

     Properties=Excel 8.0"; 

     DataTable dataTable = null; 

     using (OleDbConnection oleDbConnection = 
      new OleDbConnection(string.Format(_ConnectionString, FileName))) 
     { 
      oleDbConnection.Open(); 
      //Getting the meta data information. 
      //This DataTable will return the details of Sheets in the Excel 

      File.DataTable dbSchema = 
    oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null); 
      foreach (DataRow item in dbSchema.Rows) 
      { 
       //reading data from excel to Data Table 
       using (OleDbCommand oleDbCommand = new OleDbCommand()) 
       { 
        oleDbCommand.Connection = oleDbConnection; 
       oleDbCommand.CommandText = string.Format("SELECT * FROM 

       [B1415:J2113]", item["TABLE_NAME"].ToString()); 
        using (OleDbDataAdapter oleDbDataAdapter = new 
        OleDbDataAdapter()) 
        { 
         oleDbDataAdapter.SelectCommand = oleDbCommand; 
         dataTable = new 
       DataTable(item["TABLE_NAME"].ToString()); 
         oleDbDataAdapter.Fill(dataTable); 
         _dataTables.Add(dataTable); 
        } 
       } 
      } 
     } 
     return _dataTables; 
    } 
     } 
     } 
      } 

    ////////////////////////////////////// 
      above is the code which i am using to get data from excel but      

     /////////////////////////////////////////////////////// 


     below is the nested loop in which i want to use data 
     ///////////////////////////////////////////////// 
     for (ChromosomeID = 0; ChromosomeID < PopulationSize; ChromosomeID++) 
       { 
        Fitness = 0; 
        Altemp = (int[])AlPopulation[ChromosomeID]; 
        for (int z = 0; z < 500; z++) 
        { 
         int[] NumberOfInputs = new int[9]; 
      //// this is the array where in which data need to be added 

       InputBinary.AddRange(DecBin.Conversion2(NumberOfInputs)); 

         for (i = 0; i < Altemp.Length; i++) 
         { 
          AlGenotype[i] = (int)Altemp[i]; 
         } 
         Class1 ClsMn = new Class1(); 
         AlActiveGenes = ClsMn.ListofActiveNodes(AlGenotype); 

         ClsNetworkProcess ClsNWProcess = new 
        ClsNetworkProcess(); 
         AlOutputs = ClsNWProcess.NetWorkProcess(InputBinary, 

         AlGenotype, AlActiveGenes); 
         int value = 0; 
         for (i = 0; i < AlOutputs.Count; ++i) 
         { 
          value ^= (int)AlOutputs[i];  // xor the 
         output of the system 
         } 

         temp = Desired_Output[0]; 
         if (value == temp) // compare system Output with 
        DesiredOutput bit by bit 
          Fitness++; 
         else 
          Fitness = Fitness; 
        } 
        AlFitness.Add(Fitness); 
       } 
      } 
+0

をアプリケーションを実行しているコンピュータが、Excelがインストールされていないオブジェクト。その場合は、Excel VSTOライブラリを使用してデータにアクセスできます。 –

+1

"私は何も試していないし、アイデアがすべて抜けている"。 –

答えて

0

ザーラ、それらに答えるために支払われている質問に答えるされ、ここに誰も。我々あなたが「すべてのリファレンスアセンブリを使用して完全なコードを使用したい」というあなたの態度は、むしろ厳しいように思えます。xlsxは独自のフォーマットです。 ExcelLibraryのように、この回答はxlsxへの書き込みに関連していますが、さらにいくつかのオプションがあります:https://stackoverflow.com/a/2603625/550975

+0

私は@ SerjSagenに同意します。皆さんが私たちがやることをやってくれて、私たちが無料でやってくれるよう手伝ってくれるので、おそらく少し難しいはずです。私たちのほとんどはそれをして喜んでいますが、あなたは$ 500のコストと同じタイムラインと程度でパフォーマンスを発揮することはできません(有料とボランティア)。そのメモで、ClosedXMLを試してください。最小限の要件でデータをExcel形式から読み込み可能なフォーマットに変換するのに最適です。 :) https://closedxml.codeplex.com/ –

+0

また、いくつかの素晴らしいドキュメントと例があります。運が良かった! –

+0

それは大変申し訳ありませんが.......というわけではありませんでしたが、 – Zahra

0

私のツールNpoi.Mapperは、普及しているライブラリNPOIに基づいています。 POCO型を使用して、規則ベースのマッピングまたは明示的なマッピングを使用して直接インポートおよびエクスポートできます。

エクセル(XLSまたはXLSX)からオブジェクトを取得します

var mapper = new Mapper("Book1.xlsx"); 
var objs1 = mapper.Take<SampleClass>("sheet2"); 

// You can take objects from the same sheet with different type. 
var objs2 = mapper.Take<AnotherClass>("sheet2"); 

エクスポートが

//var objects = ... 
var mapper = new Mapper(); 
mapper.Save("test.xlsx", objects, "newSheet", overwrite: false); 
関連する問題