c# - 使用 MemoryStream 读取 excel 文件
问题描述
有没有办法MemoryStream
在 C# 中简单地读取为 excel 文件?我目前正在使用本机微软库Microsoft.Office.Interop.Excel
。有没有办法可以用这个库读取 excel 文件?
打开 Excel 文件
using Microsoft.Office.Interop.Excel; // using this namespace
public ExcelHelper(string path)
{
_xlApp = new Application();
_xlWorkbook = _xlApp.Workbooks.Open(path);
}
帮手
public static MemoryStream DownloadFile(FtpHandler handler, FtpDirectoryOrFileDetail detail)
{
var fileByte = handler
.Download
.FileByPath(detail.FilePart.Filename, detail.SubFolderPath + "/", false)?.Data;
if (fileByte is null) return stream;
using (var fileStream = new MemoryStream(fileByte))
return fileStream;
return null;
}
问题是Open
扩展名似乎只使用了一个string
参数而没有MemoryStream
. 有没有人遇到这个问题并且能够解决这个问题?我对任何建议都持开放态度。
解决方案
Below is tested solution which writing Excel workbook to memory stream and returning _objectstream which can be used to write to blob and anywhere else.
using (var fs = new FileStream(fileName, FileMode.Append, FileAccess.Write))
{
IWorkbook workbook = new XSSFWorkbook();
logic goes here......
workbook.Write(obj_stream);
using (MemoryStream tempStream = new MemoryStream())
{
workbook.Write(tempStream);
var byteArray = tempStream.ToArray();
stream = new MemoryStream(byteArray);
obj_stream = stream;
}
}
return obj_stream
推荐阅读
- ansible - Ansible Cron - 定期运行另一个 ansible 任务
- jquery - 引导工具提示卡在跨度单击上
- python-3.x - Django,当点击提交按钮但发生错误时,我想将数据从下拉菜单发送到数据库
- r - R中的嵌套哈希
- php - 在php中使用foreach循环遍历SimpleXML
- android - 行中子项的填充高度
- azure - 如何将电子邮件代码发送到 AzureAD B2C 中的辅助电子邮件 (otherMails)
- vaadin - Vaadin7 视频组件不允许人们拖到视频的后面位置
- opencv - 如何检测视频的 fps=4.5?
- spring-boot - 使用 Spring Boot 时如何防止 HSQLDB 在应用程序运行之间持久化数据?