We help IT Professionals succeed at work.

How to give styles to .CSV file while exporting to .CSV

COANetwork
COANetwork asked
on
Medium Priority
685 Views
Last Modified: 2012-05-06
Could any one pls explaing how to give styles(like make font bold) while exporting gridview to .CSV in Asp.Net
Comment
Watch Question

Dim objStreamWriter As IO.StreamWriter

'Pass the file path and the file name to the StreamWriter
constructor.
'make sure this is a path that you have permissions to save in

objStreamWriter = New
IO.StreamWriter("c:\myfiles\mycsvfile.csv")
'Write text.

Dim Str As String
Dim i As Integer
Dim j As Integer

Dim headertext =
"field1,field2,field3,field4,field5,field5,field6"
objStreamWriter.WriteLine(headertext)
For i = 0 To (Me.GridView2.Rows.Count - 1)
For j = 0 To (Me.GridView2.Columns.Count - 1)

'this IF statement stops it from adding a comma after
the last field
If j = (Me.GridView2.Columns.Count - 1) Then
Str = "<html><b>" & (Me.GridView2.Rows(i).Cells(j).Text.ToString) & "</b></html>"

Else
Str = "<html><b>" & (Me.GridView2.Rows(i).Cells(j).Text.ToString
& ",") & "</html></b>"
End If
objStreamWriter.Write(Str)
Next
objStreamWriter.WriteLine()
Next
'Close the file.
objStreamWriter.Close()

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Which programming language are you using C#/VB.net.

Author

Commented:
but I am using string builder for this.
 
pls check my code below and kindly advise.

 System.Text.StringBuilder sbCsv=new StringBuilder();
        DataSet ds = (DataSet)Session[this.ReportDSID];
        DataTable dtTemp = new DataTable();
        dtTemp = ds.Tables[0];
        int colcount = dtTemp.Columns.Count;
        int rowcount = dtTemp.Rows.Count;
        sbCsv.Append("<html><b>Downloaded time</b></html>");
        for (int i = 0; i < colcount; i++)
        {
            sbCsv.Append(this.ReportGridView.Columns[i]);
            if (i < colcount - 1)
            {
                sbCsv.Append(",");
            }
        }
        for (int intRow = 0; intRow < rowcount; intRow++)
        {
            StringBuilder sbRow=new StringBuilder();
            for (int intCol = 0; intCol < colcount; intCol++)
            {
                if (sbRow.Length > 0)
                {
                    sbRow.Append(",");
                }
                sbRow.Append(_EscapeCsvField(dtTemp.Rows[intRow][intCol].ToString()));
            }
            if(sbCsv.Length > 0) 
            {
                sbCsv.Append("\n");
            }       
            sbCsv.Append(sbRow.ToString());
        }
        Response.Clear();
        Response.ContentType = "application/excel";
        Response.BinaryWrite(System.Text.ASCIIEncoding.ASCII.GetBytes(sbCsv.ToString()));
        Response.AppendHeader("content-disposition", "attachment; filename=wtf.csv");
        Response.End();

Open in new window

Author

Commented:
when I gave the <html> tags to stringbuilder,it is not working.Kindly provide the solution

Author

Commented:
I gave stringbuilder.Append("<b>sample item</b>");
But it is not working,It is showing message with tags.but font type is not changing

Author

Commented:
Pls can any one respond.

Author

Commented:
The above link mentioned that we need to use literal control.But how to give styles to literal control.Could you pld provide the code for this.

Author

Commented:
thanks for help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.