C#의 413은 DataGridView에서 Excel로 데이터 내보내기

publicvoid outExcel(DataGridView gridView)
{
if (gridView.Rows.Count== 0)
{
MessageBox.Show(" !"," ", MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
{
 
SaveFileDialog saveFileDialog= new SaveFileDialog();
saveFileDialog.Filter= "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex= 0;
saveFileDialog.RestoreDirectory= true;
saveFileDialog.CreatePrompt= true;
saveFileDialog.Title= " Excel ";
saveFileDialog.ShowDialog();
 
if (saveFileDialog.FileName.IndexOf(":")< 0) return; // " "
 
Stream myStream;
myStream= saveFileDialog.OpenFile();
StreamWriter sw= new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string columnTitle= "";
try
{
// 
for (inti = 0; i < this.dataGridView1.ColumnCount; i++)
{
if (i> 0)
{
columnTitle+= "\t";
}
columnTitle+= this.dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(columnTitle); 
// 
for (intj = 0; j
{
string columnValue= "";
for (intk = 0; k < this.dataGridView1.Columns.Count; k++)
{
if (k> 0)
{
columnValue+= "\t";
}
if (this.dataGridView1.Rows[j].Cells[k].Value== null)
columnValue+= "";
else
columnValue+= this.dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
}
sw.WriteLine(columnValue);
}
sw.Close();
myStream.Close();
}
catch (Exceptione)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
 

좋은 웹페이지 즐겨찾기