Arrow Datagridview to Excel
با این متد میتونید اطلاعات داخل یک GridView رو به اکسل ارسال کنید.
کد:
public static void ExportToExcel(DataGridView dg,string ExcelPath)
{
StringBuilder sb = new StringBuilder();
string sep="\t";
for (int i = 0; i < dg.ColumnCount; i++)
{
sb.Append(dg.Columns[i].HeaderText+sep);
}
sb.Append(Environment.NewLine);
for (int i = 0; i < dg.Rows.Count; i++)
{
for (int j = 0; j < dg.ColumnCount; j++)
{
sb.Append((dg.Rows[i].Cells[j].Value != null ? dg.Rows[i].Cells[j].Value.ToString().Replace("\n", "-").Replace("\r", "-") : string.Empty) + sep);
}
sb.Append(Environment.NewLine);
}
try
{
File.WriteAllText("c:\\export.txt", sb.ToString(), Encoding.Unicode);
if (!string.IsNullOrEmpty(ExcelPath))
{
System.Diagnostics.Process.Start(ExcelPath,"c:\\ex port.txt");
}
}
catch
{
}
}