Hi all I’m using Open XML and ExtremML to generate an Excel File on the Server and Download it, but when you I try to open it it gives me the error “EXCEL FOUND UNREADABLE CONTENT. DO YOU WANT TO RECOVER THE CONTENTS OF THIS WORKBOOK?
IF YOU TRUST THE SOURCE OF THIS WORKBOOK, CLICK YES”, so checking on the file inside y found it’s adding a tag named company, that when i remove it it corrects the error that Excel is giving, It’s in VB .NET, this is my code:
Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
Dim iProcess As String = ""
Dim loOraClientDb As clsOraClientDb = New clsOraClientDb
Dim vFilename As String = ""
Try
Try
iProcess = "btnExcel_Click 1"
Dim ExcelSendInfo As DataTable = Session("dtExcel")
Dim ExcelExportedInfo As DataTable = New DataTable
Dim PkgOutValues() As String = {}
Dim ExcelDataSet As DataSet = New DataSet
Dim vtablename As String = ""
Dim vtabletoname As String = ""
Dim WFT As New WebFileTools
If chkFLayout.Checked Then
iProcess = "btnExcel_Click 2"
Dim sDNS As StringBuilder = New StringBuilder()
If Not chkFInfo.Checked Then
For Each dr In ExcelSendInfo.Rows
sDNS.AppendLine(dr(0))
Next
End If
iProcess = "btnExcel_Click 3"
If Not loOraClientDb.Open(ConnectionStrings("DatabaseSDSTP6301").ConnectionString) Then
EnableErrorByState(loOraClientDb.Message & ", Process: " & iProcess)
Exit Sub
Else
iProcess = "btnExcel_Click 4"
Dim vStatus As String
If ViewState("CurrentWindow") = "Report" Then
vStatus = "G.G_STATUSID!=2"
Else
vStatus = "G.G_STATUSID=2"
End If
iProcess = "btnExcel_Click 5"
Dim PkgInParameters(6)() As String
PkgInParameters(0) = New String() {"VEXPORTEDDNS", "VARCHAR2", "Empty", sDNS.ToString.Replace(Chr(13) & Chr(10), ",")}
PkgInParameters(1) = New String() {"VSTATUS", "VARCHAR2", "Empty", vStatus}
PkgInParameters(2) = New String() {"chkFInfo", "VARCHAR2", "Empty", BoolToStr(chkFInfo.Checked)}
PkgInParameters(3) = New String() {"VSEARCHOPS", "VARCHAR2", "Empty", DDLSearchOps.SelectedValue}
PkgInParameters(4) = New String() {"VSEARCH", "VARCHAR2", "Empty", txtsearch.Text}
PkgInParameters(5) = New String() {"VRANGE1", "VARCHAR2", "Empty", txtrange1.Text}
PkgInParameters(6) = New String() {"VRANGE2", "VARCHAR2", "Empty", txtrange2.Text}
Dim PkgReturnParameters() As String
PkgReturnParameters = New String() {"VRETURN", "CURSOR", "Empty"}
Dim PkgOutParameters(0)() As String
PkgOutParameters(0) = New String() {"VTOTPROJS", "NUMBER", "Empty"}
iProcess = "btnExcel_Click 6"
If Not loOraClientDb.ExecuteProcedureFunction("PKG_GENERALINFO.GENERATEEXCEL", PkgInParameters, _
PkgOutParameters, PkgReturnParameters, , ExcelExportedInfo, _
, PkgOutValues) Then
EnableErrorByState(loOraClientDb.Message & ", Process: " & iProcess)
Exit Sub
End If
vtablename = "MAININFO"
vtabletoname = "MAINTABLE"
End If
Else
iProcess = "btnExcel_Click 7"
ExcelExportedInfo = ExcelSendInfo
vtablename = "TIMEREP"
vtabletoname = "TIMEREPORT"
End If
ExcelDataSet.Tables.Add(ExcelExportedInfo)
ExcelDataSet.Tables(0).TableName = vtablename
Dim vFileTemplate As String = ""
Dim RandomNumber As New Random()
If vtablename = "MAININFO" Then
vFileTemplate = "FullLayout.xlsx"
vFilename = "FullLayout" & RandomNumber.Next(1000).ToString & ".xlsx"
Else
vFileTemplate = "TimeReport.xlsx"
vFilename = "TimeReport" & RandomNumber.Next(1000).ToString & ".xlsx"
End If
Session("vfilename") = vFilename
WFT.CopyServerFile("ExcelTemplates\" & vFileTemplate, "ExcelTemplates\" & vFilename, True)
Using DPSExcelTemplate = ExcelOpenPackage(Server.MapPath(".") & "\ExcelTemplates", vFilename.Replace(".xlsx", ""))
ExcelWorkBookPopulateDS2Table(DPSExcelTemplate, ExcelDataSet, vtablename, vtabletoname)
End Using
Server.ClearError()
Response.Redirect("DownloadPage.aspx", False)
iProcess = "btnExcel_Click 28"
Catch ex As Exception
EnableErrorByState(ex.Message & ", Process: " & iProcess)
If File.Exists(Server.MapPath(".") & "\ExcelTemplates\" & vFilename) = True Then
File.Delete(Server.MapPath(".") & "\ExcelTemplates\" & vFilename)
End If
End Try
Finally
loOraClientDb.Close()
ShowCorrespondingWindows()
End Try
End Sub
The page DownloadPage.aspx has this code:
Imports WebFileTools
Imports System.IO
Partial Class DownloadPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim vfilename = Session("vfilename").ToString
Dim nfilename = ""
If vfilename.IndexOf("FullLayout") <> -1 Then
nfilename = "FullLayout_" & Today.ToShortDateString
Else
nfilename = "TimeReport_" & Today.ToShortDateString
End If
Dim WFT As New WebFileTools
WFT.DownloadServerFile("ExcelTemplates\" & vfilename, MIMEType.Excel2007, nfilename)
End Sub
End Class
I can’t seem to find the problem has anyone gotten this error or knows how to fix this problem?
I Forgot to add this code thats for generating the Excel file with ExtremMl:
Public Shared Function ExcelOpenPackage(ByVal ServerPath As String, _
ByVal ExcelFileName As String) As ExtremeML.Packaging.SpreadsheetDocumentWrapper
Dim ExcelPackage = SpreadsheetDocumentWrapper.Open(ServerPath & "\" & ExcelFileName & ".xlsx")
Return ExcelPackage
End Function
And this is to fill the info on the file:
Public Shared Sub ExcelWorkBookPopulateDS2Table(ByRef ExcelPackage As ExtremeML.Packaging.SpreadsheetDocumentWrapper, _
ByRef Data As DataSet, _
ByVal DataTableName As String, _
ByVal ExcelTableName As String)
Dim ExcelTable = ExcelPackage.WorkbookPart.GetTablePart(ExcelTableName).Table
ExcelTable.Fill(Data, DataTableName)
End Sub
I don’t know exactly why but the problem was with my Download code previusly it was like this:
Now i changed this line Current.ApplicationInstance.CompleteRequest() for Current.Response.End() and the file gets downloaded correclty
and now i cant call my download code directly:
It’s not suposed to be the correct way of using Response.End, because it always trows an error but I don’t now if this is the correct way of using Current.ApplicationInstance.CompleteRequest() or that Response.End does something that CompleteRequest is not doing in order for it to get the correct MIME Type