2017-03-18 2 views
0

ここでExcelDataReaderを使用しています。c#importingがdatagridviewにExcelが動作しない

以下のコードが動作しない理由はわかりません。 エラーは発生しませんが、それでもExcelのデータは表示されません。 Excelファイルを読み込んだ後は何もしません。 私はC#の初心者ですが、5時間の掘り下げ後、なぜこれが機能しないのかわからないので、私は不満を感じます。助けのための

using Excel; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication5 
{ 
    public partial class ExcelForm : Form 
    { 
     public ExcelForm() 
     { 
      InitializeComponent(); 
     } 



     private void ExcelForm_Load(object sender, EventArgs e) 
     { 

     } 


     DataSet result; 

    private void btnOpen_Click(object sender, EventArgs e) 
     { 
      using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel(.xls)|*.xls|Excel(.xlsx)|*.xlsx", ValidateNames = true }) 
      { 
       if(ofd.ShowDialog() == DialogResult.OK) 
       { 
        FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read); 
        IExcelDataReader reader; 
        if (ofd.FilterIndex == 1) 
         reader = ExcelReaderFactory.CreateBinaryReader(fs); 
        else 
         reader = ExcelReaderFactory.CreateOpenXmlReader(fs); 
        reader.IsFirstRowAsColumnNames = true; 
        result = reader.AsDataSet(); 
        reader.Close(); 

        dataGridView.DataSource = result; 




       } 
      } 
     } 

    } 
} 

答えて

0
dataGridView.DataSource = DtSet.Tables[0]; 
+0

感謝。 ~~ :) –

関連する問題