2016-07-08 7 views
0

EPPlusを使用しています。Excelからデータを高速に読み込み、ファイルストリームからリストに変換する方法

アップロードには2行目の列ヘッダーがあります。また、4行目以降は、2kレコードまで変化する可能性のあるデータがあります。

enter image description here

私はそれをやっている方法は、それが2kのレコードを読み込み、リストに置くために多くの時間を要します。

using (var excel = new ExcelPackage(hpf.InputStream)) 
    {       

     var ws = excel.Workbook.Worksheets["Sheet1"]; 

     //Read the file into memory 

     for (int rw = 4; rw <= ws.Dimension.End.Row; rw++) 
     { 
      if (!ws.Cells[rw, 1, rw, 24].All(c => c.Value == null)) 
      { 
       int headerRow = 2; 

       GroupMembershipUploadInput gm = new GroupMembershipUploadInput(); 

       for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++) 
       { 
        var s = ws.Cells[rw, col].Value; 

        if (ws.Cells[headerRow, col].Value.ToString().Equals("Existing Constituent Master Id")) 
        { 
         gm.cnst_mstr_id = (ws.Cells[rw, col].Value ?? (Object)"").ToString(); 
        } 
        else if (ws.Cells[headerRow, col].Value.ToString().Equals("Prefix of the constituent(Mr, Mrs etc)")) 
        { 
         gm.cnst_prefix_nm = (ws.Cells[rw, col].Value ?? (Object)"").ToString(); 
        } 

       } 
       lgl.GroupMembershipUploadInputList.Add(gm); 
      } 
     } 

GroupMembershipUploadInputListは、私が賢いのセルから読み取った後にExcelの値を追加してい型GroupMembershipUploadInputのオブジェクトのリストです。

もっと速くすることはできますか?私はここで何が欠けていますか?

パフォーマンスを向上させる手助けをしてください。

+0

を確認してください。 http://stackoverflow.com/questions/7613898/how-to-read-an-excel-spreadsheet-in-c-sharp-quickly – rochastuff

答えて

0

ここでは多くの反復が行われています。行ごとに、各列を2回訪問します。私はあなただけなので、次のコードを大幅に時間を短縮なり、行ごととあれば、これら2つの値を必要とすることを前提としています

using (var excel = new ExcelPackage(hpf.InputStream)) 
{ 

    var ws = excel.Workbook.Worksheets["Sheet1"]; 
    int headerRow = 2; 
    // hold the colum index based on the value in the header 
    int col_cnst_mstr_id = 2; 
    int col_cnst_prefix_nm = 4; 
    // loop once over the columns to fetch the column index 
    for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++) 
    { 
     if ("Existing Constituent Master Id".Equals(ws.Cells[headerRow, col].Value)) 
     { 
      col_cnst_mstr_id = col; 
     } 
     if ("Prefix of the constituent(Mr, Mrs etc)".Equals(ws.Cells[headerRow, col].Value)) 
     { 
      col_cnst_prefix_nm = col; 
     } 
    } 
    //Read the file into memory 
    // loop over all rows 
    for (int rw = 4; rw <= ws.Dimension.End.Row; rw++) 
    { 
     // check if both values are not null 
     if (ws.Cells[rw, col_cnst_mstr_id].Value != null && 
      ws.Cells[rw, col_cnst_prefix_nm].Value != null) 
     { 
      // the correct cell will be selcted based on the column index 
      var gm = new GroupMembershipUploadInput 
      { 
       cnst_mstr_id = (string) ws.Cells[rw, col_cnst_mstr_id].Value ?? String.Empty, 
       cnst_prefix_nm = (string) ws.Cells[rw, col_cnst_prefix_nm].Value ?? String.Empty 
      }; 

      lgl.GroupMembershipUploadInputList.Add(gm); 
     } 
    } 
} 

私はインナーコラムループを削除し、メソッドの開始にそれを移動しました。そこでは、興味のあるフィールドごとにcolumnindexを取得するだけです。高価なnullチェックも減らすことができます。値をフェッチするには、行内の単純な索引参照が必要です。

関連する問題