2014년 8월 7일 목요일

[예제]C#에서 Excel 파일 읽기/변경하기[닷넷C#교육/ADO.NET강좌/ASP.NET교육잘하는곳/C#,ASP.NET교육추천/닷넷실무교육/.NET,C#/ADO.NET교육/닷넷학원/ASP.NET실무교육]

이 예제는 student.xls라는 엑셀 파일을 읽고 변경 하는 간단한 예제 입니다. 참고하세요~ 

실행화면은 아래와 같습니다. 

 


Excel파일은 C# 실행 화일이 있는곳에 두고 실행 하시면 됩니다. 
그 내용은 아래와 같습니다. 

-------------------------------------------------- 

[student.xls] 

학번 성명 주소 전화 학년 
970302 가길동 서울 111-2222 1 
970303 나길동 경기 222-3333 2 
970304 다길동 제주 333-4444 3 

------------------------------------------------------ 

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.OleDb; 

namespace Read 

///

/// Form1에 대한 요약 설명입니다. 
///

public class Form1 : System.Windows.Forms.Form 

private System.Windows.Forms.ListBox listBox1; 
private System.Windows.Forms.Button button1; 
private System.Windows.Forms.Button button2; 
///

/// 필수 디자이너 변수입니다. 
///

private System.ComponentModel.Container components = null; 

public Form1() 

// 
// Windows Form 디자이너 지원에 필요합니다. 
// 
InitializeComponent(); 

// 
// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다. 
// 


///

/// 사용 중인 모든 리소스를 정리합니다. 
///

protected override void Dispose( bool disposing ) 

if( disposing ) 

if (components != null) 

components.Dispose(); 


base.Dispose( disposing ); 


#region Windows Form 디자이너에서 생성한 코드 
///

/// 디자이너 지원에 필요한 메서드입니다. 
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오. 
///

private void InitializeComponent() 

this.listBox1 = new System.Windows.Forms.ListBox(); 
this.button1 = new System.Windows.Forms.Button(); 
this.button2 = new System.Windows.Forms.Button(); 
this.SuspendLayout(); 
// 
// listBox1 
// 
this.listBox1.ItemHeight = 12; 
this.listBox1.Location = new System.Drawing.Point(8, 40); 
this.listBox1.Name = "listBox1"; 
this.listBox1.Size = new System.Drawing.Size(352, 208); 
this.listBox1.TabIndex = 0; 
// 
// button1 
// 
this.button1.Location = new System.Drawing.Point(8, 8); 
this.button1.Name = "button1"; 
this.button1.Size = new System.Drawing.Size(88, 24); 
this.button1.TabIndex = 1; 
this.button1.Text = "Data Load"; 
this.button1.Click += new System.EventHandler(this.button1_Click); 
// 
// button2 
// 
this.button2.Location = new System.Drawing.Point(296, 8); 
this.button2.Name = "button2"; 
this.button2.Size = new System.Drawing.Size(64, 24); 
this.button2.TabIndex = 2; 
this.button2.Text = "Update"; 
this.button2.Click += new System.EventHandler(this.button2_Click); 
// 
// Form1 
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
this.ClientSize = new System.Drawing.Size(368, 254); 
this.Controls.Add(this.button2); 
this.Controls.Add(this.button1); 
this.Controls.Add(this.listBox1); 
this.Name = "Form1"; 
this.Text = "Form1"; 
this.ResumeLayout(false); 


#endregion 

///

/// 해당 응용 프로그램의 주 진입점입니다. 
///

[STAThread] 
static void Main() 

Application.Run(new Form1()); 


//DataLoad Click 
private void button1_Click(object sender, System.EventArgs e) 

OleDbConnection ExcelConn=null; 
try 

string strCon=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.xls;Extended Properties=""Excel 8.0;HDR=YES""" ; 
ExcelConn=new OleDbConnection(strCon); 
ExcelConn.Open(); 

string SQL =@"SELECT 학번, 성명, 주소, 전화, 학년 FROM [학생$] "; 
OleDbCommand cmd=new OleDbCommand(SQL,ExcelConn); 
OleDbDataReader reader=cmd.ExecuteReader(); 

while(reader.Read()) 

listBox1.Items.Add(reader[0]+"\t"+ reader[1]+"\t"+reader[2]+"\t"+reader[3]+"\t"+reader[4]); 

reader.Close(); 

catch(Exception ex) 

MessageBox.Show(ex.ToString()); 

finally 

ExcelConn.Close(); 





private void button2_Click(object sender, System.EventArgs e) 

OleDbConnection ExcelConn=null; 
try 

string strCon=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.xls;Extended Properties=""Excel 8.0;HDR=YES""" ; 
ExcelConn=new OleDbConnection(strCon); 
ExcelConn.Open(); 

string SQL =@"UPDATE [학생$] SET 주소='제주' WHERE 주소='울산' "; 

OleDbCommand cmd=new OleDbCommand(SQL,ExcelConn); 

int nRow=cmd.ExecuteNonQuery(); 

MessageBox.Show(nRow.ToString() + "행 변경"); 



catch(Exception ex) 

MessageBox.Show(ex.ToString()); 

finally 

ExcelConn.Close(); 




} 



평일주간[100%환급과정]
(8/11)SQL기초에서 Schema Object까지
(8/18)Spring,MyBatis,Hibernate실무과정
(8/18)자바기초JDBC,Servlet/JSP까지
(8/18)PL/SQL,ORACLE HINT,TUNING
(8/25)C#4.0,WinForm,ADO.NET
(8/25)안드로이드개발자과정
(8/25)오라클자바채용확정교육
평일야간[개인80%환급]
(8/11)SQL기초에서실무까지
(8/11)안드로이드개발자과정
(8/12)Spring, MyBatis, Hibernate
(8/13)C#,Network,ADO.NET,ASP.NET
(8/13)HTML5,CSS3,Ajax,jQuery마스터
(8/28)자바JSP,jQuery,Spring,MyBatis
주말주간[개인80%환급]
(8/09)SQL기초에서실무까지
(8/09)안드로이드개발자과정
(8/09)자바JSP,Ajax,jQuery,Spring,MyBatis
(8/16)C#,ASP.NET마스터(8/16)웹퍼블리싱 마스터
(8/16)Spring, MyBatis, Hibernate
(8/23)JAVA,Network&WEB&Framework
주말야간[개인80%환급]
(8/09)SQL기초에서실무까지
(8/23)JAVA,Network&WEB&Framework

댓글 없음:

댓글 쓰기