导出到excel vb.net

时间:2012-08-02 15:19:26

标签: vb.net

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

我的代码是:

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()

2 个答案:

答案 0 :(得分:0)

查看此网站Exporting MSFlexGrid to Excel

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

答案 1 :(得分:0)

Settings