尝试将网格中的数据导出为.Net中的优秀

时间:2017-02-13 08:57:36

标签: .net excel vb.net export export-to-excel

我正在尝试将数据从网格导出到Vb.net中的Excel

代码如下:

Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As EventArgs) Handles btnExpToExcel.Click
    Dim sStringToAppend As String
    Dim sStringToAppend1 As String
    Dim sStringToAppend2 As String
    Dim sStringToAppend3 As String
    Dim strImplicitrate As String
    Dim StrReportFileNAme As String = "[Filename]_" + Session("LoginID") + "_" + DateTime.Now.ToString("yyyyMMdd") + ".xls"
    If searchFieldRow1.Length <> 1 Then
        sStringToAppend = "<td>" & searchFieldRow1(3) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow1(1) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow1(2) & "</td>"
    End If
    If searchFieldRow2.Length <> 1 Then
        sStringToAppend = sStringToAppend + "<tr> <td>" & searchFieldRow2(3) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow2(1) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow2(2) & "</td></tr>"

    End If
    If searchFieldRow3.Length <> 1 Then
        sStringToAppend = sStringToAppend + "<tr> <td>" & searchFieldRow3(3) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow3(1) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow3(2) & "</td></tr>"

    End If
    If searchFieldRow4.Length <> 1 Then
        sStringToAppend = sStringToAppend + "<tr> <td>" & searchFieldRow4(3) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow4(1) & "</td>"
        sStringToAppend = sStringToAppend + "<td>" & searchFieldRow4(2) & "</td></tr>"
    End If

    Try
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=" + StrReportFileNAme + "")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Using sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            '   To Export all pages
            gvCCRowSearch.AllowSorting = False
            gvCCRowSearch.AllowPaging = False
            Me.fncShowdata(Session("SimulationLatestId"))


            gvCCRowSearch.HeaderRow.ForeColor = Color.Black
            gvCCRowSearch.HeaderRow.Style.Add("height", "40px")
            For Each cell As TableCell In gvCCRowSearch.HeaderRow.Cells
                cell.BackColor = gvCCRowSearch.HeaderStyle.BackColor

                gvCCRowSearch.HeaderRow.Cells.Item(2).ForeColor = Color.Red
                gvCCRowSearch.HeaderRow.Cells.Item(11).ForeColor = Color.Red

                gvCCRowSearch.HeaderRow.Cells.Item(7).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(8).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(9).BackColor = Color.LightBlue

                gvCCRowSearch.HeaderRow.Cells.Item(10).BackColor = Color.LightPink
                gvCCRowSearch.HeaderRow.Cells.Item(11).BackColor = Color.LightPink
                gvCCRowSearch.HeaderRow.Cells.Item(12).BackColor = Color.LightPink
                gvCCRowSearch.HeaderRow.Cells.Item(13).BackColor = Color.LightPink
                gvCCRowSearch.HeaderRow.Cells.Item(14).BackColor = Color.LightPink
                gvCCRowSearch.HeaderRow.Cells.Item(15).BackColor = Color.LightPink

                gvCCRowSearch.HeaderRow.Cells.Item(16).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(17).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(18).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(19).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(20).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(21).BackColor = Color.LightBlue
                gvCCRowSearch.HeaderRow.Cells.Item(22).BackColor = Color.LightBlue
                '   gvCCRowSearch.HeaderRow.Cells.Item(22).BackColor = Color.LightBlue
            Next

            For Each row As GridViewRow In gvCCRowSearch.Rows
                '  row.BackColor = Color.Orange
                For Each cell As TableCell In row.Cells
                    row.Cells.Item(3).BackColor = Color.Orange
                    row.Cells.Item(4).BackColor = Color.Orange
                    row.Cells.Item(5).BackColor = Color.Orange
                    row.Cells.Item(6).BackColor = Color.Orange
                    row.Cells.Item(7).BackColor = Color.Orange
                    row.Cells.Item(8).BackColor = Color.Orange
                    row.Cells.Item(9).BackColor = Color.Orange
                    row.Cells.Item(10).BackColor = Color.Orange
                    row.Cells.Item(11).BackColor = Color.Orange
                    row.Cells.Item(12).BackColor = Color.Orange
                    row.Cells.Item(13).BackColor = Color.Orange
                    row.Cells.Item(14).BackColor = Color.Orange
                    row.Cells.Item(15).BackColor = Color.Orange
                    row.Cells.Item(16).BackColor = Color.Orange
                    row.Cells.Item(17).BackColor = Color.Orange
                    row.Cells.Item(18).BackColor = Color.Orange
                    row.Cells.Item(19).BackColor = Color.Orange
                    row.Cells.Item(20).BackColor = Color.Orange
                    row.Cells.Item(21).BackColor = Color.Orange
                    row.Cells.Item(22).BackColor = Color.Orange
                    'If row.RowIndex Mod 2 = 0 Then
                    '    cell.BackColor = gvCCRowSearch.AlternatingRowStyle.BackColor
                    'Else
                    '    cell.BackColor = gvCCRowSearch.RowStyle.BackColor
                    'End If
                    cell.CssClass = "textmode"
                    Dim controls As New List(Of Control)()

                    ' Add controls to be removed to Generic List
                    For Each control As Control In cell.Controls
                        controls.Add(control)
                    Next

                    ' Loop through the controls to be removed And replace then with Literal
                    For Each control As Control In controls
                        Select Case control.GetType().Name
                            Case "HyperLink"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, HyperLink).Text
                                })
                                Exit Select
                            Case "TextBox"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, TextBox).Text
                                })
                                Exit Select
                            Case "LinkButton"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, LinkButton).Text
                                })
                                Exit Select
                            Case "CheckBox"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, CheckBox).Text
                                })
                                Exit Select
                            Case "DropDownList"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, DropDownList).SelectedValue
                                })
                                Exit Select
                            Case "RadioButton"
                                cell.Controls.Add(New Literal() With {
                                 .Text = TryCast(control, RadioButton).Text
                                })
                                Exit Select
                        End Select
                        cell.Controls.Remove(control)

                    Next
                Next
            Next
            ' gvSearchResult.ExportSettings.ExportOnlyData = True

            gvCCRowSearch.RenderControl(hw)

            ' style to format numbers to string
            Dim style As String = "<style> .textmode {} </style>"
            If sStringToAppend = "" Then
                style = style & "<table><tr><td style='bgcolor=#d3d3d3;width=550px;'><b>Lease Accounting Database</b>  as on :</td><td>" & Format(Date.Now, "dd-MMM-yyyy hh:mm") & " </td></tr>" &
              "<tr><td></td></tr><tr><td><b>Vendor Code:</b></td> <td>" & strVendorCode & "</td> </tr><tr><td><b>Entiry:</b></td><td>" & StrEntity & "</td></tr> <tr><td><b>Lessor Name:</b></td><td>" & StrLessorName & "</td></tr> <tr><td><b>Description of Asset:</b></td><td>" & StrAssetDesc & "</td></tr>  <tr><td><b>Country:</b></td><td>" & strCountry & "</td></tr>  <tr><td><b>Station:</b></td><td>" & strSation & "</td></tr> <tr><td><b>Implicit Rate:</b></td><td>" & strImplicitrate & "</td></tr> <tr><td></td></tr>  " &
              " </table>"
            Else
                style = style & "<table><tr><td style='bgcolor=#d3d3d3;width=550px;'><b>Price Incoice and Control Tool</b>  as on :</td><td>" & Format(Date.Now, "dd-MMM-yyyy hh:mm") & " </td></tr> " &
                            "<tr><td></td></tr><tr><td><b>Vendor Code:</b></td> <td>" & strVendorCode & "</td> </tr><tr><td><b>Entiry:</b></td><td>" & StrEntity & "</td></tr> <tr><td><b>Lessor Name:</b></td><td>" & StrLessorName & "</td></tr> <tr><td><b>Description of Asset:</b></td><td>" & StrAssetDesc & "</td></tr>  <tr><td><b>Country:</b></td><td>" & strCountry & "</td></tr>  <tr><td><b>Station:</b></td><td>" & strSation & "</td></tr> <tr><td><b>Implicit Rate:</b></td><td>" & strImplicitrate & "</td></tr> <tr><td></td></tr>  " &
                            "<tr>" & sStringToAppend & "</tr><tr></td></td></tr> </table>"
            End If
            Response.Write(style)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.End()
        End Using
    Catch ex As Exception
        ex.Message().ToString()
    End Try
End Sub 

现在在excel中打开文件时出现以下错误。 错误在Excel文件中说明如下:

[FileName] .xls的文件格式和扩展名不匹配。文件可能是courpted或不安全。除非你信任或损坏或不安全。除非你相信它的来源,不要打开它。你想要打开它吗? ...

然后显示名为Yes / no / help

的按钮

0 个答案:

没有答案