My Name is Kay....

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

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

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

References : 


using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.IO;



Function for exporting to Excel :


private void ToCsV(DataGridView dGV, string filename)
{
	string stOutput = "";
	// Export titles:
	string sHeaders = "";

	for (int j = 0; j < dGV.Columns.Count; j++)
	sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
	stOutput += sHeaders + "\r\n";
	// Export data.
	for (int i = 0; i < dGV.RowCount - 1; i++)
	{
		string stLine = "";
		for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
		stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\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();
}  



Code for button for exporting to Excel : 


SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "export.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
	//ToCsV(dataGridView1, @"c:\export.xls");
	ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name 
}



참고 Url : http://www.codeproject.com/Tips/545456/Exporting-DataGridview-To-Excel

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
35 C# WindowsForms Application DataGridView kay 2013.07.05 2646
34 C# ComboBox ReadOnly 속성 kay 2013.07.08 3288
33 C# comboBox 디비 데이터로 채워넣기 kay 2013.07.08 3277
32 C# dataGridView 내용 정렬 ( DefaultCellStyle , ColumnHeadersDefaultCellStyle ) kay 2013.07.08 5877
31 C# MDI 폼 이동 및 시작 폼 설정하기 kay 2013.07.09 3429
30 VS 최근에 사용한 프로젝트 삭제 kay 2013.07.10 2512
29 C# SqlCommand 재사용(Reuse) 하기 kay 2013.07.10 3919
28 C# ComboBox Option ( Add , Set , Get ) kay 2013.07.10 16002
27 C# DataGridView 에서 열 숨기기 kay 2013.07.22 3332
26 C# DataGridView 에서 Cells 값 Null/Empty 체크하기 kay 2013.07.22 9514
25 C# TextBox 에서 줄바꿈 넣기 kay 2013.07.22 10389
24 C# Confirm Massage 창 띄우기 kay 2013.07.22 13001
23 C# 프로그래밍 방식으로 버튼의 Click 이벤트 호출 kay 2013.07.23 2730
22 C# MDI 폼 한번에 모두 닫기 kay 2013.07.25 2824
21 C# 폼에서 새폼 열고 자신은 완전히 닫기. kay 2013.07.26 7607
20 C# TextBox KeyEvent 엔터 이벤트 실행하기 kay 2013.07.26 3838
19 C# C# 외부 프로그램 실행하기 ( Process.Start ) kay 2013.08.06 10549
» C# 엑셀 내보내기 , Exporting DataGridview To Excel kay 2013.08.08 5117
17 C# 화면 캡쳐하기 kay 2013.09.02 4058
16 C# DataGridViewComboBoxColumn Change 이벤트 Value 값 가져오기 kay 2013.09.02 5369
Board Pagination Prev 1 2 Next
/ 2