2011-03-26 12 views
0

어떻게 C# 프로젝트로 Excel 파일을 가져올 수 있습니까?Excel 파일 가져 오기

+0

당신이 C# 프로그램으로 프로젝트에 추가하거나 값을 읽을 하시겠습니까? (예 : DataTable로 변환) – digEmAll

+0

자세한 내용이 필요합니다. 너 정확히 뭘 하려구? 궁극적으로 무엇을 달성하기를 원하십니까? –

+0

내 프로젝트에서이 데이터를 편집하고 싶습니다. –

답변

0

프로젝트 디렉토리의 올바른 폴더에 있는지 확인한 다음 (Visual Studio를 사용한다고 가정) 솔루션 탐색기에있는 모든 파일 표시를 클릭합니다 (도구 맨 위에있는 작은 버튼 중 하나임).) 그리고 나서 이제 나타날 ecel 파일을 마우스 오른쪽 버튼으로 클릭하고 프로젝트에 추가를 클릭하십시오.

1

Office 개체 모델을 사용하여 Excel 시트 작업을 할 수 있습니다. Microsoft.Excel.Interop assembly에 대한 참조를 추가하십시오. Excel 시트 작업에 this 참조를 사용할 수 있습니다.

0

참고 Excel 파일의 경로를 동적으로 설정됩니다

// using System.Data.OleDb 
OleDbConnection ExcelConection = null; 
OleDbCommand ExcelCommand = null; 
OleDbDataReader ExcelReader = null; 
OleDbConnectionStringBuilder OleStringBuilder = null; 

try 
{ 
    OleStringBuilder = 
     new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"); 
    OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls"); 

    ExcelConection = new OleDbConnection(); 
    ExcelConection.ConnectionString = OleStringBuilder.ConnectionString; 

    ExcelCommand = new OleDbCommand(); 
    ExcelCommand.Connection = ExcelConection; 
    ExcelCommand.CommandText = "Select * From [Sheet1$]"; 

    ExcelConection.Open(); 
    ExcelReader = ExcelCommand.ExecuteReader(); 

    GridView1.DataSource = ExcelReader; 
    GridView1.DataBind(); 
} 
catch (Exception Args) 
{ 
    LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message; 
} 
finally 
{ 
    if (ExcelCommand != null) 
     ExcelCommand.Dispose(); 
    if (ExcelReader != null) 
     ExcelReader.Dispose(); 
    if (ExcelConection != null) 
     ExcelConection.Dispose(); 
}