导出到excel vb.net

导出到excel vb.net,vb.net,Vb.net,当我将flexgrid从vb.net vs2008导出到excel到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

当我将flexgrid从vb.net vs2008导出到excel到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()
关于错误转到ErrorHandler
暗淡无光
Dim-iCol简称
Dim objExcl作为Excel.Application
将objWk设置为Excel.工作簿
将对象调整为Excel.工作表
我的头很短
将头像作为对象
objExcl=新的Excel.Application
objExcl.Visible=True
objExcl.UserControl=True
Dim oldCI作为System.Globalization.CultureInfo=_
System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture=_
新系统。全球化。文化信息(“en-US”)
objWk=objExcl.Workbooks.Add
System.Threading.Thread.CurrentThread.CurrentCulture=oldCI
objSht=objWk.Sheets(1)
vHead=Split(g.FormatString,“|”)
'在工作表中填充标题
'从flex grid中获取列标题并将其放入工作表中
对于iHead=1至uBond(vHead)
如果Len(Trim(vHead(iHead)))>0,则对象单元格。\u默认值(1,iHead)=vHead(iHead)
下一个
System.Windows.Forms.Cursor.Current=System.Windows.Forms.Cursors.WaitCursor
对于iRow=0到g行-1
对于iCol=0到g。获取_Cols()-1
g、 行=iRow
g、 Col=iCol
'
“如果是g.Text”,则对象单元格。\u默认值(iRow+2,iCol+1)=g.Text
如果g.文本“”则
对象范围(NumCol2Lattre(iCol+1)&“&iRow+2&“:”&NumCol2Lattre(iCol+1)&“&iRow+2&”)。选择()
objExcl.ActiveCell.Value=g.Text
如果结束
下一个iCol
下一步
objExcl.Application.Visible=True
System.Windows.Forms.Cursor.Current=System.Windows.Forms.Cursors.Default
objSht=无
objWk=在垃圾收集之前,任何东西都不能被销毁。点击
objExcl=无
出口接头
错误处理程序:
objSht=无
objWk=无
objExcl=无
MsgBox(“expotation任务中的错误-”&Err.Description,MsgBoxStyle.Information)
错误清除()
查看此网站


我怀疑您的问题出在
vHead=Split(g.FormatString,“|”)上
使用调试器找出
g.FormatString
的值。它会导致错误吗?一步一个脚印地看一遍代码,看看会发生什么。

为什么你要在例行公事中间改变文化?所以你真正的问题是为什么当法国文化很好的时候,为什么英国文化不起作用呢?我也建议你使用Test.catch块来代替错误的GoTo ErrorHandler。
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