2012-08-02 86 views
1

我有一个问题,当我将一个flexgrid导出为ex​​cel从vb.net vs2008到office 2010英文版时,excel文件被打开但它是空的。然而,当我使用Office法文版在打开[正确]导出为ex​​cel vb.net

我的代码是:

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

为什么你要在日常工作中改变文化? – 2013-07-24 01:03:33

+0

所以你真正的问题是为什么英国文化没有在法国文化正常工作的时候工作。 – 2013-07-24 01:10:29

+0

我还建议您使用Try ... Catch块代替On Error GoTo ErrorHandler – 2013-07-24 01:15:01

回答

0

检查出这个网站Exporting MSFlexGrid to Excel

我怀疑你的问题是与vHead = Split(g.FormatString, "|")使用调试器来发现出什么值g.FormatString。它是否导致错误?一次只读一行代码,看看会发生什么。

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