Export text file with text qualifier with single quote with single tilt, c#.net
Hi Developers,I want to export a text file with '~' format.
I tried but output is like below ending with columns'~ and rows with '~'
'column1'~'column2'~'column3'~'column4'~
'1'~'aaa'~'bbb'~'ccc'~'
I want to end both headers and rows with ' single quote like below
'column1'~'column2'~'column3'~'column4'
'1'~'aaa'~'bbb'~'ccc'
The written code as below...please any one check my code solve my issue.
protected void btnExportTextFile_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnStrings);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_xyz";
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the Text file data.
string txt = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for Text file.
txt += "'" + column.ColumnName + "'~";
}
//Add new line.
txt += "\r\n'";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
txt += row[column.ColumnName].ToString() + "'~'";
}
//Add new line.
txt += "\r\n'";
}
//Download the Text file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=outputfile.txt");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(txt);
Response.Flush();
Response.End();
}
}
}
Thanks
Kalyan