• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

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

Could any one pls explaing how to give styles(like make font bold) while exporting gridview to .CSV in Asp.Net
0
COANetwork
Asked:
COANetwork
  • 6
  • 2
1 Solution
 
lakshmidurgaCommented:
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()
0
 
MushqCommented:
Which programming language are you using C#/VB.net.
0
 
COANetworkAuthor 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

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
COANetworkAuthor Commented:
when I gave the <html> tags to stringbuilder,it is not working.Kindly provide the solution
0
 
COANetworkAuthor 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
0
 
COANetworkAuthor Commented:
Pls can any one respond.
0
 
COANetworkAuthor 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.
0
 
COANetworkAuthor Commented:
thanks for help
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now