1

当我将 flexgrid 从 vb.net vs2008 导出到 office 2010 英文版时,我遇到了一个问题,打开了一个 excel 文件,但它是空的。但是,当我使用 Office 法语版本时,它会打开 [正确]

我的代码是:

On Error GoTo ErrorHandler

    Dim iRow As Short
    Dim iCol As Short
    Dim objExcl As Excel.Application
    Dim objWk As Excel.Workbook
    Dim objSht As Excel.Worksheet
    Dim iHead As Short
    Dim vHead As Object

    objExcl = New Excel.Application
    objExcl.Visible = True
    objExcl.UserControl = True


    Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCulture
    System.Threading.Thread.CurrentThread.CurrentCulture = _
        New System.Globalization.CultureInfo("en-US")





    objWk = objExcl.Workbooks.Add
    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
    objSht = objWk.Sheets(1)


    vHead = Split(g.FormatString, "|")
    'populate heading in the sheet
    'take the column heading from flex grid and put it in the sheet
    For iHead = 1 To UBound(vHead)

        If Len(Trim(vHead(iHead))) > 0 Then objSht.Cells._Default(1, iHead) = vHead(iHead)
    Next 

    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

    For iRow = 0 To g.Rows - 1
        For iCol = 0 To g.get_Cols() - 1
            g.Row = iRow
            g.Col = iCol
            '
            'If g.Text <> "" Then objSht.Cells._Default(iRow + 2, iCol + 1) = g.Text
            If g.Text <> "" Then
                objSht.Range(NumCol2Lattre(iCol + 1) & "" & iRow + 2 & ":" & NumCol2Lattre(iCol + 1) & "" & iRow + 2 & "").Select()
                objExcl.ActiveCell.Value = g.Text
            End If
        Next iCol


    Next iRow

    objExcl.Application.Visible = True


    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default


    objSht = Nothing

    objWk = Nothingl may not be destroyed until it is garbage collected. Click 
    objExcl = Nothing
    Exit Sub

ErrorHandler:    
    objSht = Nothing

    objWk = Nothing

    objExcl = Nothing

    MsgBox("Error In expotation task & " & Err.Description, MsgBoxStyle.Information)
    Err.Clear()
4

2 回答 2

0

查看此网站将 MSFlexGrid 导出到 Excel

我怀疑你的问题是vHead = Split(g.FormatString, "|")使用调试器找出g.FormatString. 它会导致错误吗?一次单步执行一行代码,看看会发生什么。

于 2013-07-24T01:02:43.207 回答
0
On Error GoTo ErrorHandler

Imports System.Data
Imports System.IO
Imports System.Web.UI

Partial Class ExportGridviewDatainVB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    Protected Sub BindGridview()
        Dim dt As New DataTable()
        dt.Columns.Add("UserId", GetType(Int32))
        dt.Columns.Add("UserName", GetType(String))
        dt.Columns.Add("Education", GetType(String))
        dt.Columns.Add("Location", GetType(String))
        dt.Rows.Add(1, "SureshDasari", "B.Tech", "Chennai")
        dt.Rows.Add(2, "MadhavSai", "MBA", "Nagpur")
        dt.Rows.Add(3, "MaheshDasari", "B.Tech", "Nuzividu")
        dt.Rows.Add(4, "Rohini", "MSC", "Chennai")
        dt.Rows.Add(5, "Mahendra", "CA", "Guntur")
        dt.Rows.Add(6, "Honey", "B.Tech", "Nagpur")
        gvDetails.DataSource = dt
        gvDetails.DataBind()
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered 

    End Sub
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.ClearContent()
        Response.Buffer = True
        Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "Customers.xls"))
        Response.ContentType = "application/ms-excel"
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        gvDetails.AllowPaging = False
        BindGridview()
        'Change the Header Row back to white color
        gvDetails.HeaderRow.Style.Add("background-color", "#FFFFFF")
        'Applying stlye to gridview header cells
        For i As Integer = 0 To gvDetails.HeaderRow.Cells.Count - 1
            gvDetails.HeaderRow.Cells(i).Style.Add("background-color", "#df5015")
        Next
        gvDetails.RenderControl(htw)
        Response.Write(sw.ToString())
        Response.[End]()
    End Sub
End Class
于 2017-03-06T07:02:28.363 回答