使用Open XML和ExtremML创建Excel文件 - 提供“......不可读的内容......”

时间:2011-09-13 17:07:15

标签: excel openxml

大家好我正在使用Open XML和ExtremML在服务器上生成Excel文件并下载它,但是当我尝试打开它时它会给我一个错误“EXCEL FOUND RECREADABLE CONTENT。你想要恢复吗?这本练习册的内容? 如果您信任本工作簿的来源,请点击“,因此检查内部文件,发现它添加了一个名为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

1 个答案:

答案 0 :(得分:0)

我不确切知道为什么,但问题出在我的下载代码上,这就像是这样:

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.Response.End()更改了此行Current.ApplicationInstance.CompleteRequest(),并将文件下载为correclty

现在我无法直接调用我的下载代码:

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类型