简体   繁体   English

使用c#asp.net / MVC从Web浏览器上的巨大excel文件打开单个工作表(单个选项卡)

[英]Open a single worksheet (single tab) from a huge excel file on a web browser using c# asp.net / MVC

I have huge excel files that I have to open from web browser. 我有大量的excel文件,我必须从网络浏览器打开。 It takes several minutes to load huge file. 加载大文件需要几分钟。 Is it possible to open a single worksheet (single tab) at a time from excel file that contains many worksheets? 是否可以从包含许多工作表的excel文件一次打开一个工作表(单个选项卡)? I have to do this using C# / asp.net MVC 我必须使用C#/ asp.net MVC来做这件事

I'm assuming you have the excel workbook on the server and just want to send a single worksheet to the client. 我假设你在服务器上有excel工作簿,只想将一个工作表发送给客户端。 Does the user then edit the worksheet? 然后用户是否编辑工作表? Will they be uploading it back? 他们会上传吗?

Assuming this is just a report then why not use the OpenXML sdk to read the workbook, extrac the sheet in question and send it back to the client? 假设这只是一个报告,那么为什么不使用OpenXML sdk来读取工作簿,提取相关工作表并将其发送回客户端? This is what @Jim in the comments was suggesting. 这就是@Jim在评论中提出的建议。 You can get the SDK here: Open XML SDK 2.0 for Microsoft Office . 您可以在此处获取SDK: 打开适用于Microsoft Office的XML SDK 2.0 However, I'm not sure if it will work with the 'old' excel format. 但是,我不确定它是否适用于'旧'excel格式。 I assume you'll need to save the template workbook in the new Office formats (xslx). 我假设您需要以新的Office格式(xslx)保存模板工作簿。

Your question is slightly unclear as to where the spreadsheet is stored. 您的问题有点不清楚电子表格的存储位置。

If it's on a server you control, process it, extracting sheets you need, and create other sheets which are smaller in size. 如果它在您控制的服务器上,处理它,提取您需要的纸张,并创建其他尺寸较小的纸张。 (Or possibly save them in a different format.). (或者可能以不同的格式保存它们。)

If they're not on a server you control, download the file using C#, then go through a similiar process of extracting the sheet before opening it. 如果它们不在您控制的服务器上,请使用C#下载该文件,然后在打开之前通过一个类似的提取工作表的过程。

Having said that, I've dealt with some largish spreadsheets (20MB or so), and haven't really had a problem processing the entire spreadsheet as a whole. 话虽如此,我已经处理了一些较大的电子表格(20MB左右),并且在处理整个电子表格时并没有遇到任何问题。

So where is the bottleneck? 那么瓶颈在哪里? Your network or possibly the machine you're running? 您的网络或您正在运行的机器?

Use third party components. 使用第三方组件。
We are fighting with server side Excel generation for years and has been defeated. 我们正在与服务器端Excel生成多年的战斗,并已被击败。
We bought third party components and all problems gone. 我们买了第三方组件,所有问题都没了。

Take a look at this question in StackOverflow: 在StackOverflow中查看这个问题:

Create Excel (.XLS and .XLSX) file from C# 从C#创建Excel(.XLS和.XLSX)文件

I think you can open your workbook on the server (inside your ASP.NET MVC application) and process only the specific worksheet you want. 我认为您可以在服务器上(在ASP.NET MVC应用程序内)打开工作簿,并只处理您想要的特定工作表。 You can then send such worksheet to the user using NPOI. 然后,您可以使用NPOI将此类工作表发送给用户。

The following post shows you how to do that using an ASP.NET MVC application: 以下文章向您展示了如何使用ASP.NET MVC应用程序执行此操作:

Creating Excel spreadsheets .XLS and .XLSX in C# 在C#中创建Excel电子表格.XLS和.XLSX

From your question, it seems you want to improve load time by using (opening) the data from one worksheet instead of the whole workbook. 从您的问题来看,似乎您希望通过使用(打开)来自一个工作表而不是整个工作簿的数据来改善加载时间。 If this is the case and you only want the data, then access the workbook using ADO.NET with OLEDB provider. 如果是这种情况并且您只需要数据,则使用带有OLEDB提供程序的ADO.NET访问工作簿。 (You can use threading to load each worksheet to improve load performance. For instance, loading three large data sets in three worksheets took 17 seconds. Loading each worksheet on a separate thread, loaded same data sets in 5 seconds.) (您可以使用线程加载每个工作表以提高加载性能。例如,在三个工作表中加载三个大型数据集需要17秒。将每个工作表加载到一个单独的线程上,在5秒内加载相同的数据集。)

From experience, performance starts to really suffer with workbooks of 40MB or more. 根据经验,性能开始真正受到40MB或更多工作簿的影响。 Especially, if workbooks contain many formulas. 特别是,如果工作簿包含许多公式。 My largest workbook of 120MB takes several minutes to load. 我最大的120MB工作簿需要几分钟才能加载。 Using OLEDB access, I can load, access, and process the same data in a few seconds. 使用OLEDB访问,我可以在几秒钟内加载,访问和处理相同的数据。

If you want the client to open data in Excel, gather data via ADO.NET/OLEDB, get XML and transform into XMLSS using Xslt. 如果您希望客户端在Excel中打开数据,请通过ADO.NET/OLEDB收集数据,获取XML并使用Xslt转换为XMLSS。 Which is easy and there is much documentation and samples. 这很简单,有很多文档和样本。

If you just want to present the data, gather data via ADO.NET/OLEDB, get XML and transform into HTML using Xslt. 如果您只想呈现数据,请通过ADO.NET/OLEDB收集数据,获取XML并使用Xslt转换为HTML。 Which is easy and there is much documentation and samples. 这很简单,有很多文档和样本。

Be aware that the browser and computer become non-responsive with large data sets. 请注意,浏览器和计算机对大型数据集无响应。 I had to set limit upper limit. 我必须设置限制上限。 If limit was reaced, I notified user of truncated results, otherwise, user thought computer was "locked". 如果限制被重新定义,我通知用户截断结果,否则,用户认为计算机被“锁定”。

You can't "say" to Excel, even via Interop that you only want a single worksheet. 您不能“说”Excel,即使通过Interop,您只需要一个工作表。 There are a lot of explanations, like formulas, references and links between them, which makes the task impossible. 有很多解释,比如公式,引用和它们之间的链接,这使得任务变得不可能。

If you only want to read the data from the worksheet, maybe OLEDB Data Provider is the best option for you. 如果您只想从工作表中读取数据,那么OLEDB数据提供程序可能是您的最佳选择。 Here is a full example: Reading excel file using OLEDB Data Provider 这是一个完整的示例: 使用OLEDB数据提供程序读取excel文件

Otherwise, you will need to load the entire workbook in memory before do anything with it. 否则,您需要在执行任何操作之前将整个工作簿加载到内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM