public void Export(DataSet ds, string path)
{
try
{
StringBuilder sb = new StringBuilder();
foreach (DataGridViewColumn column in gvList.Columns)
{
sb.Append(column.HeaderText + "\t");
}
sb.Append("\n");
foreach (DataGridViewRow row in gvList.Rows)
{
foreach (DataGridViewColumn column in gvList.Columns)
{
sb.Append(row.Cells[column.Index].Value.ToString() + "\t");
}
sb.Append("\n");
}
sb.Append("\n");
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write);
byte[] b = Encoding.Default.GetBytes(sb.ToString());
fs.Write(b, 0, b.Length);
fs.Flush();
fs.Close();
fs.Dispose();
MessageBox.Show("导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("导出失败,请稍后重试!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
{
try
{
StringBuilder sb = new StringBuilder();
foreach (DataGridViewColumn column in gvList.Columns)
{
sb.Append(column.HeaderText + "\t");
}
sb.Append("\n");
foreach (DataGridViewRow row in gvList.Rows)
{
foreach (DataGridViewColumn column in gvList.Columns)
{
sb.Append(row.Cells[column.Index].Value.ToString() + "\t");
}
sb.Append("\n");
}
sb.Append("\n");
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write);
byte[] b = Encoding.Default.GetBytes(sb.ToString());
fs.Write(b, 0, b.Length);
fs.Flush();
fs.Close();
fs.Dispose();
MessageBox.Show("导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("导出失败,请稍后重试!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2011/03/14/1983155.html,如需转载请自行联系原作者