Click here to Skip to main content
15,889,721 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to export to excel .In office 2003 it was running fine .
But in Office 2007 and 2010 its giving issue that
"FILE you are trying to open "FileName" is in a different format specified by the file extension."

I got one solution of modifying registry value but can not do that because the number of users for ur application is very high.

Code I am using for Export to Excel
VB
Response.ClearContent()
            Response.ClearHeaders()

            Response.ContentType = "application/vnd.ms-excel"
            Response.AppendHeader("Content-Disposition", "attachment; filename=AdminReport.xls")
            Response.BufferOutput = True
            Response.ContentEncoding = System.Text.Encoding.UTF8
            Response.Charset = "UTF-8"
            Dim tw As New System.IO.StringWriter
            Dim hw As New System.Web.UI.HtmlTextWriter(tw)
            tblExcelExport.RenderControl(hw)
            Response.Write(tw.ToString())
            Response.End()
Posted

1 solution

Here is the code I use to export to excel, mine is a datagrid-excel, so I'm assuming you will have to change it to use an openfiledialog. I use office 2010 and this works just fine.
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New excel.Application

Try
    Dim excelBook As excel.Workbook = xlApp.Workbooks.Add
    Dim excelWorksheet As excel.Worksheet = CType(excelBook.Worksheets(1), excel.Worksheet)
    xlApp.Visible = True
    rowsTotal = $PersonnelDataGridView$.RowCount - 1
    colsTotal = $PersonnelDataGridView$.Columns.Count - 1

    With excelWorksheet
        .Cells.Select()
        .Cells.Delete()
        For iC = 0 To colsTotal
            .Cells(1, iC + 1).Value = $PersonnelDataGridView$.Columns(iC).HeaderText
        Next

        For I = 0 To rowsTotal
            For j = 0 To colsTotal
                .Cells(I + 2, j + 1).value = $PersonnelDataGridView$.Rows(I).Cells(j).Value
            Next j
        Next I

        .Rows("1:1").Font.FontStyle = "Bold"
        .Rows("1:1").Font.Size = 10
        .Cells.Columns.AutoFit()
        .Cells.Select()
        .Cells.EntireColumn.AutoFit()
        .Cells(1, 1).Select()
    End With

Catch ex As Exception
    MsgBox("Export Excel Error " & ex.Message)
Finally
    'RELEASE ALLOACTED RESOURCES
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
    xlApp = Nothing
End Try
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900