如何从ASP.Net Server打开.xlsx文件?

时间:2015-07-28 17:34:38

标签: asp.net vb.net excel xls xlsx

我正在尝试打开从我们的服务器生成的.xlsx文件,因为我被要求将文件从.xls更改为.xlsx。旧版本没有问题,他们只想升级。我已经尝试了,我搜索了许多提示和网站,人们在问同样的问题,但没有人给他们一个正确的答案,我发现最新的是2012年左右。这很可能是主要问题。

Public Sub GenerateExcelToBrowser(ByRef dtDetailRows As DataTable, _
                                          ByVal strReportname As String, _
                                                Optional ByRef dtHeaderRow As DataTable = Nothing, _
                                                Optional ByVal blnMissingPartsOverride As Boolean = False, _
                                                Optional ByVal Scheduler As String = "N")
            Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
            Try
                Dim sbHTML As New StringBuilder

                sbHTML = GenerateExcel(dtDetailRows, strReportname, dtHeaderRow, blnMissingPartsOverride)

                Response.Clear()
                Response.Charset = String.Empty
                Response.Buffer = True
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", strReportname)) 'set the response mime type for excel
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                Response.Write(sbHTML.ToString())            
                Response.Flush()
            Catch MemEx As OutOfMemoryException
                Dim oLog4Net As clsLog4Net = New clsLog4Net
                oLog4Net.LogError(MemEx.Message.ToString() & " - " & MemEx.Source.ToString() & " - " & MemEx.StackTrace.ToString())
                Response.Write("The report you are trying to create contains more information than can be sent. Please narrow the report criteria and try again.")
                Response.Flush()
            Catch ex As Exception
                Dim oLog4Net As clsLog4Net = New clsLog4Net
                If UCase(Scheduler) = "Y" Then
                    oLog4Net.LogError(ex.Message.ToString() & " - " & ex.Source.ToString() & " - " & ex.StackTrace.ToString())
                    Response.Write("Error on a page. For assistance please call CSA Today Administrator at 877-422-4225.")
                Else
                    oLog4Net.LogError(ex.Message.ToString() & " - " & ex.Source.ToString() & " - " & ex.StackTrace.ToString())
                End If
                oLog4Net = Nothing
                Response.Flush()
            End Try
        End Sub

简单地重命名文件是不够的。 Excel 2013将说文件格式/扩展名无效并检查它是否已损坏。我比较了新旧类型之间的属性。在“详细信息”下,新类型缺少名称,类型和文件夹路径。我一直在为此而奋斗,我不知道为什么这么简单的声音任务最终会如此复杂。

任何信息将不胜感激!

2 个答案:

答案 0 :(得分:0)

Excel 2007及更高版本使用名为" OpenXML"的文件格式。您需要更新GenerateExcel方法以生成该格式的文件。您可以查看几个不同的库来执行此操作。 ClosedXML是我过去使用的Asposehttp://hostname/blogs/xx/也很受欢迎。如果你四处搜寻,还有其他人。

此外,您无法从服务器"打开文件。您只能将文件推送到浏览器,此时它将成为浏览器的责任,用户可以选择打开,保存或丢弃它。

答案 1 :(得分:0)

您似乎想将XLS文件转换为XLSX格式。您可以使用Aspose.Cells,使用简单的代码行,如下所示

string filePath = @"c:\source.xls";
Workbook workbook = new Workbook(filePath);
workbook.Save(@"c:\output.xlsx", SaveFormat.Xlsx);

您无需从xls文件导入数据,然后将其导出到xlsx文件。只需将xls文件加载到工作簿对象中并以xlsx格式保存即可。

此外,您可以在服务器端的ASP.NET中使用此解决方案。您还可以在内存流中以不同格式加载和保存工作簿。

注意:我在Aspose

担任开发人员传播者
相关问题