2013-08-12 3 views
3

안녕하세요 저는 WinForm 응용 프로그램을 작성 중이며 Excel 파일을 읽고 싶습니다.조건부 Excel 파일 읽기

------------------------------------------------------ 
first_name |last_name|ID  |Skill |exam_date |certification_number| 
john  | smith |12345678|engineer|2013/12/12|3543546647 
john  | smith |12345678|electronic|2013/07/12|35477776647 
..... 
..... 

당신은 내가 (20까지) 여러 행을 가질 수있는 한 사람을 볼 수있는 내 엑셀, 기본 키를 가지고 있지 않기 때문에 처음 3 열은 동일합니다 : 내 엑셀이 같은 것입니다.

이 코드는 excel로 작성했지만 한 행만 읽습니다. 같은 ID로 모든 행을 읽을 수있는 방법은 무엇입니까? 사전에

string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\book3.xlsx;Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"; 
     DataSet ds = new DataSet(); 
     OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [SHEET1$]", ConnectionString); 
     adapter.Fill(ds); 

     DataRow dataRow = (from DataRow dr in ds.Tables[0].Rows where dr["ID"].ToString() == textBox1.Text select dr).FirstOrDefault(); 

감사

답변

2

귀하의 FirstOrDefault()는 단지 '먼저'

이 시도 선택합니다 : 내 C# 응용 프로그램에서

IEnumerable<DataRow> dataRows = (from DataRow dr in ds.Tables[0].Rows where dr["ID"].ToString() == textBox1.Text select dr); 
foreach (DataRow dataRow in dataRows) 
{ 
     // do stuff with current dataRow 
} 
+0

죄송 원인은 내가 IEnumrable을 사용하는 방법을 알고하지 않습니다

여기 EPPLUS를 사용하여 파일의 시트를 열고 DataTable을로 데이터를로드하는 내 코드입니다. 내가 사용할 수 있다고 말해 줄 수 있니? – saad

0
var query = from DataRow dr in ds.Tables[0].Rows where dr["ID"].ToString() == textBox1.Text select dr; 

foreach (DataRow dataRow in query.ToList()) 
{ 
     // do something with dataRow 
} 
0

를 I EPPLUS 라이브러리 (http://epplus.codeplex.com/) 정말 좋고 빠릅니다. 나는 C#을 새로운 해요

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Reflection; 
using System.Text; 
using System.Globalization; 
using System.Diagnostics; 
using System.Data; 
using System.Windows; 
using System.Windows.Controls; 
using System.IO; 
using OfficeOpenXml; 
using OfficeOpenXml.Style; 
using OfficeOpenXml.Drawing.Chart; 
using System.Xml; 

public class ProjectLoad { 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="filePath"></param> 
     public void Load (string filePath) { 

      // Get the file we are going to process 
      FileInfo existingFile = new FileInfo(filePath); 

      try { 
       if (existingFile.Exists == true) { 
        // Open and read the XlSX file. 
        using (ExcelPackage package = new ExcelPackage(existingFile)) { 
         // Get the work book in the file 
         ExcelWorkbook workBook = package.Workbook; 
         if (workBook != null) { 
          if (workBook.Worksheets.Count > 0) { 

           // read some data 
           int sheet_number = 0; 
           DataTable table = load_sheet_toDataGrid(workBook.Worksheets[sheet_number]); 
          } 
         } 
      } catch (System.IO.IOException) { 
       //error message 
      } 

     } 

     /// <summary> 
     /// loads the content of a sheet into a datatable 
     /// </summary> 
     /// <param name="currentWorksheet"></param> 
     /// <returns></returns> 
     private DataTable load_sheet_toDataGrid (ExcelWorksheet currentWorksheet) { 
      DataTable dt = new DataTable(); 

      int rows = currentWorksheet.Dimension.End.Row; 
      int cols = currentWorksheet.Dimension.End.Column; 

      //Add columns 
      for (int c = 0 ; c < cols ; c++) { 
       dt.Columns.Add(currentWorksheet.Cells[0 + 1 , c + 1].Value.ToString()); 
      } 

      //add values 
      for (int r = 1 ; r < rows ; r++) { 
       object[] ob = new object[cols]; 
       for (int c = 0 ; c < cols ; c++) { 

        double value; 
        bool isDouble = Double.TryParse(currentWorksheet.Cells[r + 1 , c + 1].Value.ToString() , out value); 

        bool isDate = false; 
        DateTime date = new DateTime(); 
        if (c == 0 && !isDouble) { 
         isDate = DateTime.TryParse(currentWorksheet.Cells[r + 1 , c + 1].Value.ToString() , out date); 
        } 

        if (isDouble) { 
         ob[c] = value; 
        } else if (isDate) { 
         ob[c] = date; 
        } else { 
         ob[c] = currentWorksheet.Cells[r + 1 , c + 1].Value; 
        } 
       } 
       dt.Rows.Add(ob); 
      } 

      return dt; 
     } 
}