My Name is Kay....

DIY , 먹방 , 개발 , 육아 , 여행 좋아합니다.
AdBlock 사용시 화면이 정상적으로 노출되지 않습니다.
포스팅 관련 문의 및 개발 문의는 Email : wkzkfmxksi@gmail.com

추가 포스팅이 이뤄지지 않는 블로그입니다. 문의는 wkzkfmxksi@gmail.com 으로 연락주세요.
kay
조회 수 4347 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

SQL 쿼리로 Excel 파일 내보내기 샘플입니다.


기존에 DataGridView 를 내보내는것을 약간 수정했습니다.   ( 엑셀 내보내기 , Exporting DataGridview To Excel : http://www.uhoon.co.kr/dotnet/1897 )



  private void button1_Click(object sender, EventArgs e) {
  	SaveFileDialog sfd = new SaveFileDialog();
  	sfd.Filter = "Excel Documents (*.xls)|*.xls";
  	sfd.FileName = "fileName.xls";
  	if (sfd.ShowDialog() == DialogResult.OK) { 
  		ToCsV(sfd.FileName);  
  	}
  }
  private void ToCsV(string filename) {
  	string stOutput = "";
  	string sHeaders = "";
  	string SQL = "SELECT * FROM *********";
  	SqlConnection conn = new SqlConnection(ConnectionString);
  	conn.Open();
  	SqlCommand cmd = new SqlCommand(SQL, conn);
  	SqlDataReader dr = cmd.ExecuteReader();
  	// Loop through the fields and add headers
  	for (int i = 0; i < dr.FieldCount; i++) {
  		string name = dr.GetName(i);
  		sHeaders = sHeaders.ToString() + name.ToString() + "\t";
  	}
  	stOutput += sHeaders + "\r\n";
  	// Loop through the rows and output the data
  	while (dr.Read()) {
  		string stLine = "";
  		for (int i = 0; i < dr.FieldCount; i++) {
  			string value = dr[i].ToString();
  			stLine = stLine.ToString() + value.ToString() + "\t";
  		}
  		stOutput += stLine + "\r\n";
  	}
  	Encoding utf16 = Encoding.GetEncoding(1254);
  	byte[] output = utf16.GetBytes(stOutput);
  	FileStream fs = new FileStream(filename, FileMode.Create);
  	BinaryWriter bw = new BinaryWriter(fs);
  	bw.Write(output, 0, output.Length); //write the encoded file
  	bw.Flush();
  	bw.Close();
  	fs.Close();
  }
?

  1. IIS에서 .Net 날짜 형식 YYYY-MM-DD

    Date2015.08.24 CategoryVS Bykay Views944
    Read More
  2. 웹 사이트 모니터링 프로그램 C#

    Date2015.08.18 CategoryC# Bykay Views2592
    Read More
  3. C# SMTP 서버를 이용한 메일 발송

    Date2015.07.30 CategoryC# Bykay Views1408
    Read More
  4. .net Release / Debug 콤보박스 안보일때

    Date2015.05.08 CategoryVS Bykay Views771
    Read More
  5. C# 엑셀데이터 읽어서 디비에 저장

    Date2014.10.14 CategoryC# Bykay Views5139
    Read More
  6. C# 소수점 지정

    Date2014.10.14 CategoryC# Bykay Views1835
    Read More
  7. C# FTP 이용시 유용한 클래스

    Date2014.09.29 CategoryC# Bykay Views6206
    Read More
  8. appSettings 값 추가하기 , 읽어오기 , 값 존재 유무 체크

    Date2013.12.23 CategoryC# Bykay Views5240
    Read More
  9. 스레드에서 함수 호출시 매개변수 넘기기

    Date2013.10.29 CategoryC# Bykay Views3211
    Read More
  10. multiLine TextBox 자동 스크롤시키기

    Date2013.10.29 CategoryC# Bykay Views3389
    Read More
  11. 파일 읽기 , 복사 , 삭제 , 쓰기 , 파일 유무

    Date2013.09.27 CategoryVB Bykay Views3950
    Read More
  12. 엑셀 내보내기 , Exporting SQL To Excel

    Date2013.09.05 CategoryC# Bykay Views4347
    Read More
  13. [담아온글] For vs Foreach Performance 속도 대결

    Date2013.09.04 CategoryC# Bykay Views11801
    Read More
  14. DataGridView ComboBox editingcontrolshowing 이벤트 걸기

    Date2013.09.04 CategoryC# Bykay Views4700
    Read More
  15. 문자열 검색 " String.Contains() "

    Date2013.09.04 CategoryC# Bykay Views3074
    Read More
  16. DataGridViewComboBoxColumn Change 이벤트 Value 값 가져오기

    Date2013.09.02 CategoryC# Bykay Views5391
    Read More
  17. 화면 캡쳐하기

    Date2013.09.02 CategoryC# Bykay Views4060
    Read More
  18. 엑셀 내보내기 , Exporting DataGridview To Excel

    Date2013.08.08 CategoryC# Bykay Views5117
    Read More
  19. C# 외부 프로그램 실행하기 ( Process.Start )

    Date2013.08.06 CategoryC# Bykay Views10566
    Read More
  20. TextBox KeyEvent 엔터 이벤트 실행하기

    Date2013.07.26 CategoryC# Bykay Views3839
    Read More
Board Pagination Prev 1 2 Next
/ 2