Check this function
Exporting DataGridview To Excel - Some code suggestions[
^]
Modify(also split) the above function like below
public static void GridToString(DataGridView dgv, int iRowstobeExported)
{
}
I did split the fuction for you. After you done with above function, concatenate the
outputStr
strings & pass it to below function.
public static void SaveAsCSV(string outputStr, string filename)
{
byte[] output = Encoding.ASCII.GetBytes(outputStr);
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(output, 0, output.Length);
bw.Flush();
}
}
}
Now go ahead to customize(change the logic in 1st function)