开发者

How show colors in export to excel in asp.net

iam doing export functionality in asp.net1.1.... I want to highlight some records in red color when i export to excel... following is my code to export records in excel but i want some records in red colors... So how to do this plz help me out.

Public Shared Sub ExportToExcelInvitee(ByVal query As String, _
                                       ByRef Response As System.Web.HttpResponse, _
                                       Optional ByVal exportDataset As DataSet = Nothing)
        Dim index As Integer
        Dim colIndex As Integer
        Dim columnCount As Integer
        Dim excelDataSet As DataSet
        Dim cnt As Integer

        Const PROC As String = CLASSNAME & ".ExportToExcelInvitee"
        Try

            If IsNothing(exportDataset) Then
                excelDataSet = ExecuteDataset(query)
            Else
                excelDataSet = exportDataset
            End If

            If Not IsNothing(excelDataSet) Then
                If excelDataSet.Tables(0).Rows.Count <> 0 Then

                    Response.Clear()
                    Response.ContentType = "application/vnd.ms-excel"
                    Response.Charset = ""
                    Response.AddHeader("Content-Disposition", "attachment; filename=List.xls")

                    Response.Write("<TABLE border=1>")
                    Response.Write("<TR>")
                    Response.Write("<TD><DIV align=center><B>Sr. No.</B></DIV></TD>")
                    columnCount = excelDataSet.Tables(0).Columns.Count - 1
                    For index = 2 To columnCount

                        Response.Write("<TD>" & _
                                          "<DIV align=center>" + _
                                                "<B>" & excelDataSet.Tables(0).Columns(index).ColumnName.ToString & "</B>" + _
                                          "</DIV>" & _
                                       "</TD>")

                    Next
                    Response.Write("</TR>")
                    Response.Write("<TR>")
                    ' Loop to leave one empty line after header, 
                    ' Loopimg to add the TD with black boders which doens not get added if only TR added
                    For index = 2 To columnCount
                        Response.Write("<TD></TD>")
                    Next
                    Response.Write("</TR>")

                    cnt = 1
                    For index = 0 To excelDataSet.Tables(0).Rows.Count - 1
                        If Not (excelDataSet.Tables(0).Rows(index).RowState = DataRowState.Deleted) Then

                            Response.Write("<TR>")
                            Response.Write("<TD>" & _
                                              "<DIV align=left>" & _
                                                  (cnt).ToString() & _
                                              "</DIV>" & _
                                            "</TD>")
                            For colIndex = 2 To columnCount

                                Response.Write("<TD valign=top>" & _
                                                  "<DIV align=left>" & _
                                                       excelDataSet.Tables(0).Rows(index).Item(colIndex).ToString() & _
                                                  "</DIV>" & _
                                               "</TD>")

                            Next
                            Response.Write("</TR>")
                            cnt = cnt + 1
                        End If
                    Next
                    Response.Write("<开发者_如何转开发;/TABLE>")
                    Response.End()

                End If 'DataSet must contain data
            End If 'DataSet must contain data

        Catch ex As Exception
            Call ErrorLog(PROC & ", " & ex.Source, ex.Message)
        End Try

    End Sub


In your case, that would be simply:

<TD style='color: red'>Some value</TD>


I can't see where you decide which cells should be highlighted red - however, since you're basically outputting an html table, you should be able to use standard markup to change either the 'color' or 'backgroundColor' attribute for that html element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜