2012-08-02 87 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