Winform 빠른 내보내기

1372 단어 WinForm
public static void ExportExcel(DataGridView DataGridView01){ Stream stream = null; StreamWriter writer = null; SaveFileDialog dialog = new SaveFileDialog { Filter = "Execl files (*.xls)|*.xls", FilterIndex = 0, RestoreDirectory = true, CreatePrompt = true, Title = "Export Excel File To"}; if (DialogResult.OK == dialog.ShowDialog()) { try { stream = dialog.OpenFile(); writer = new StreamWriter(stream, Encoding.GetEncoding(0)); string str = ""; for (int i = 0; i < DataGridView01.ColumnCount; i++) { if (i > 0) { str = str + "\t"; } str = str + DataGridView01.Columns[i].HeaderText; } writer.WriteLine(str); int columnCount = DataGridView01.ColumnCount; for (int j = 0; j < DataGridView01.Rows.Count; j++) { string str2 = ""; for (int k = 0; k < columnCount; k++) { if (k > 0) { str2 = str2 + "\t"; } if (k > 0x16) { } if (DataGridView01.Rows[j].Cells[k].Value == null) { str2 = str2 ?? ""; } else { str2 = str2 + DataGridView01.Rows[j].Cells[k].Value.ToString(); } } writer.WriteLine(str2); } writer.Close(); stream.Close(); } catch(Exception exception) {MessageBox.Show("파일을 내보내는 중 오류가 발생했습니다. 파일이 열려 있을 수 있습니다!"+exception.Message); } finally { writer.Close(); stream.Close(); } MessageBox.Show(저장 성공, 프롬프트, MessageBoxButtons.OK);}
 
 //
  ??   null, , 。

좋은 웹페이지 즐겨찾기