Excel 文档可以加载到内存中、填充并以字节形式返回吗?
我之前使用过 Excel Interop 来打开 Excel 文件、向其中写入一些数据并保存它。像这样的事情:
public void WriteAFile()
{
var xlApp = new Excel.ApplicationClass();
var xlWorkbook = xlApp.Workbooks.Add(Missing.Value);
var xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
xlWorksheet.Name = "Some Name";
// Write some contrived data
for (var i = 0; i < 10; i++)
{
xlWorksheet.Cells[i + 1, 1] = i;
xlWorksheet.Cells[i + 1, 2] = i + 1;
//... and so on...
}
//... and so on...
// Save the file
xlWorkbook.SaveAs(
@"",
Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value,
Missing.Value,
false,
false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges,
false,
Missing.Value,
Missing.Value,
Missing.Value
);
xlWorkbook.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
// release other resources, etc.
}
这对于写入本地工作站上的文件的应用程序来说相当有效。但是,现在我正在开发一个网络应用程序,它需要将文件返回给用户。问题是文件系统上的文件无法修改。所以我需要做的是:
- 将一个已知的Excel文件加载到内存中(请注意,该文件是
.xlsm
并且包含宏代码,当用户下载它时,该文件将在用户的工作站上运行)。 - 将已知数据写入文件中的已知位置(为了论证,将一些数值写入第 99 页的前几个单元格)。
- 通过 Web 应用程序从内存中流式传输该文件(流式传输部分很简单,它首先获取字节数组,这对我来说似乎更棘手)。
我用谷歌搜索了一下,但没有找到太多东西。基本上只是说明和示例,它们执行我在上面的代码中已经执行的操作。当使用 COM 互操作时,我上面的代码(相当旧)和我在网上找到的其他代码都有点过时,这也不足为奇。我们使用的是 .NET 4.0,因此任何可用的东西都适合我们。
上面的代码几乎完全展示了我对 COM 的使用经验。如果可能的话,我宁愿根本不使用 COM。但现在我很难起步。有人能指出我正确的方向吗?也许引用一些在这里特别有用的类或方法?谢谢。
PS 另一件令我害怕的事情是 Web 服务器上需要 Excel,以及 Excel 应用程序在 Web 服务器的控制台上打开并挂起用户看不见的错误的可能性某种。因此,非常感谢任何建议。
I've worked with Excel Interop before for the purposes of opening an Excel file, writing some data to it, and saving it. Something like this:
public void WriteAFile()
{
var xlApp = new Excel.ApplicationClass();
var xlWorkbook = xlApp.Workbooks.Add(Missing.Value);
var xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
xlWorksheet.Name = "Some Name";
// Write some contrived data
for (var i = 0; i < 10; i++)
{
xlWorksheet.Cells[i + 1, 1] = i;
xlWorksheet.Cells[i + 1, 2] = i + 1;
//... and so on...
}
//... and so on...
// Save the file
xlWorkbook.SaveAs(
@"",
Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value,
Missing.Value,
false,
false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges,
false,
Missing.Value,
Missing.Value,
Missing.Value
);
xlWorkbook.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
// release other resources, etc.
}
This works reasonably well for an application writing to a file on a local workstation. However, now I'm working on a web application which needs to return a file to the user. The catch is that the file on the file system can't be modified. So what I need to do is:
- Load a known Excel file into memory (note that the file is
.xlsm
and contains macro code, which will be run on the user's workstation when they download it). - Write known data to a known location in the file (for the sake of argument, some numeric values to the first few cells on sheet 99).
- Stream that file from memory via the web application (the streaming part is trivial, it's getting the byte array in the first place that seems trickier to me).
I've Googled around, but haven't found much of anything. Basically just instructions and examples which do what I'm already doing in my code above. It also comes as no surprise when working with COM interop that my code above (which is pretty old) and other code I find online is all a bit dated. We're using .NET 4.0, so anything that's available there is appropriate for us.
The extent of my experience with COM is pretty much fully displayed in the code above. I'd prefer not to use COM at all for this, if that's at all possible. But right now I'm just having trouble getting off the ground. Can anybody point me in the right direction? Maybe reference some classes or methods that would be of particular use here? Thanks.
P.S. Another thing that kind of frightens me is the need for Excel on the web server, as well as the possibility of the Excel application opening on the console of the web server and hanging on a user-unseen error of some kind. So any advice there is very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终使用了 ClosedXML 库。 (OpenXML 本身......笨拙。)作为打开文件、写入一段数据并返回字节数组的简单概念证明:
I ended up using the ClosedXML library. (OpenXML by itself was... unwieldy.) As a simple proof of concept for opening the file, writing a piece of data, and returning a byte array:
使用 c# 4.0 COM 应该更容易一些,因为您可以使用动态。但它仍然是 COM。
我为此使用了第三方产品 放置细胞。我不为他们工作,但我真的很喜欢这个产品(即使它不是免费的)。这让我在处理excel时省去了很多麻烦。
With c# 4.0 COM should be a bit easier since you can use dynamic. But it's then still COM.
I use a thirt party product for this called aspose cells. I'm not working for them, but i realy like this product(even if it's not free). This save me a lot of trouble when dealing with excel.
老派,但是我很久以前就写了这个 即时构建工作簿并将其导出。也许它能让你走上正轨?
Old-school, but I wrote this a long time ago that constructs a workbook on the fly and exports it. Maybe it can get you on the right track?
使用 COM 执行类似操作时,我将生成的 Excel 工作簿保存到临时文件夹中,将其发送出去,然后将其删除。不使用 COM 对我来说并不是一个真正的选择,因为工作簿还需要生成数据透视表和图表。
Doing something similar with COM I resorted to saving the generated Excel workbook to a temporary folder, sending it off and then deleting it. Not using COM wasn't really an option for me as the workbook also needed pivot tables and graphs generated.