【问题标题】:Creating Excel File with Open XML and ExtremML- gives “…unreadable content…”使用 Open XML 和 ExtremML 创建 Excel 文件 - 给出“……不可读的内容……”
【发布时间】:2011-09-15 00:10:22
【问题描述】:

大家好,我正在使用 Open XML 和 ExtremML 在服务器上生成 Excel 文件并下载它,但是当您尝试打开它时,它给了我错误“EXCEL FOUND UNREADABLE CONTENT. DO YOU WANT TO RECOVER THE本工作手册的内容? 如果您信任此工作簿的来源,请单击“是”,因此检查 y 内的文件发现它添加了一个名为 company 的标签,当我删除它时,它会纠正 Excel 给出的错误,它在 VB .NET 中,这是我的代码:

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

页面DownloadPage.aspx有这个代码:

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

我似乎找不到问题有没有人得到这个错误或知道如何解决这个问题?

我忘了添加这段代码,用于使用 ExtremMl 生成 Excel 文件:

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

这是填写文件上的信息:

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

【问题讨论】:

  • 查看我的代码更多,我了解到问题出在下载文件时,因为当我将模板复制到新文件然后填充文件时,我可以毫无问题地打开它,但是一旦下载给客户端添加了标签我不知道这是否与 mimetype 有关
  • 好吧,我已经检查过我的 IIS 是否正确添加了 MIMEtypes,所以我仍然不知道原因

标签: excel openxml


【解决方案1】:

我不知道具体原因,但问题出在我的下载代码之前,它是这样的:

Public Sub DownloadServerFile(ByVal FileToDownload As String, ByVal MIMETYPE As MIMEType,   Optional ByVal NewNameOfFileToDownload As String = "")
  Dim FileExt As String = ""
  Dim F2DArr As String = Path.GetFileName(FileToDownload)

  Current.Response.ClearContent()
  Current.Response.ClearHeaders()

  Select Case MIMETYPE
     Case WebFileTools.MIMEType.Excel2007
        Current.Response.ContentType = _
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        FileExt = ".xlsx"
     Case WebFileTools.MIMEType.Word2007
        Current.Response.ContentType = _
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        FileExt = ".docx"
     Case WebFileTools.MIMEType.PowerPoint2007
        Current.Response.ContentType = _
                "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        FileExt = ".pptx"
  End Select

  If NewNameOfFileToDownload.Trim.Length > 0 Then
     Current.Response.AppendHeader("content-disposition", "inline;attachment;filename=" _
                           & NewNameOfFileToDownload & FileExt)
  Else
     Current.Response.AppendHeader("content-disposition", "inline;attachment;filename=" & F2DArr)
  End If

  Current.Response.Clear()
  Current.Response.TransmitFile(Server.MapPath(".") & "\" & FileToDownload)
  Current.Response.Flush()
  If File.Exists(Server.MapPath(".") & "\" & FileToDownload) = True Then
     File.Delete(Server.MapPath(".") & "\" & FileToDownload)
  End If
  Current.ApplicationInstance.CompleteRequest()
End Sub

现在我将这一行 Current.ApplicationInstance.CompleteRequest() 更改为 Current.Response.End() 并且文件被正确下载

现在我不能直接调用我的下载代码:

Server.ClearError()
        WFT.DownloadServerFile("ExcelTemplates\" & vFilename, MIMEType.Excel2007, nfilename)
        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

它不应该是使用 Response.End 的正确方法,因为它总是会引发错误,但如果这是使用 Current.ApplicationInstance.CompleteRequest() 或 Response.End 的正确方法,我现在不会CompleteRequest 为了获得正确的 MIME 类型而没有做的事情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多