You must Sign In to post a response.
  • Category: .NET

    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
  • #768880
    Hi,

    What's the issue are you facing.?

    Please elaborate the issue that will improve to resolve the issue...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768885
    I have not tried below snippet with myself but hope it helps

    protected void ExportTextFile(object sender, EventArgs e)

    {

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))

    {

    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))

    {

    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 + "\t\t";

    }



    //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() + "\t\t";

    }



    //Add new line.

    txt += "\r\n";

    }



    //Download the Text file.

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition", "attachment;filename=SqlExport.txt");

    Response.Charset = "";

    Response.ContentType = "application/text";

    Response.Output.Write(txt);

    Response.Flush();

    Response.End();

    }

    }

    }

    }

    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments