如何从流中打开.xls
文件?
using (MemoryStream ms = new MemoryStream())
using (CryptoStream sc = new CryptoStream(ms, new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces), CryptoStreamMode.Write))
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(base64String);
sw.Flush;
}
我正在尝试使用Microsoft.Office.Interop
,但看起来它的Excel.Application().Workbooks.Open
方法只是启动了Excel
应用程序。
答案 0 :(得分:1)
要从流中读取,可以使用ExcelDataReader软件包:
using ExcelDataReader;
using (MemoryStream ms = new MemoryStream())
using (CryptoStream sc = new CryptoStream(ms, new
FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces), CryptoStreamMode.Write))
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(base64String);
sw.Flush;
using (IExcelDataReader er = ExcelReaderFactory.CreateBinaryReader(ms))
{
while(er.Read())
{
for (int i = 0; i < er.FieldCount; i++)
Console.Write(er.GetValue(i) + " ");
Console.WriteLine();
}
}
}
然后,您可能需要另外安装ExcelDataReader.DataSet软件包。
答案 1 :(得分:0)
通过名为EPPlus的库,您可以通过以下方式实现此目标:
using (var package = new ExcelPackage(stream))
{
var currentSheet = package.Workbook.Worksheets;
var workSheet = currentSheet.First();
workSheet.Cells[1,1].Value = "Text in first row and first column"; // EPPlus index of columns and rows are from 1 not 0
using(var memStream = new MemoryStream())
{
package.SaveAs(memStream);
using(var fileStream = File.Create("path"))
{
byte[] buffer = new byte[1024];
int bytesRead;
do
{
bytesRead = memStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}