【发布时间】:2010-04-06 20:59:22
【问题描述】:
我正在使用 BinaryReader 从使用 OWA 的 Exchange 邮箱读取 Excel 2007 文件,然后使用 BinaryWriter 将文件写入磁盘。我的问题是当作者完成时这两个文件不匹配。更糟糕的是 Excel 2007 不会打开已写入的文件。
以前 Excel 2003 对下面的解决方案没有任何问题。如果文件是 Excel 2003 格式的文件,Excel 2007 没有问题,只要文件格式是 Excel 2007 (*.xlsx)。
二进制读取器:
using(System.IO.Stream stream = resource.GetInputStream(attachedFiles[k].Address))
{
using(System.IO.BinaryReader br = new System.IO.BinaryReader(stream))
{
attachment.Data = new byte[attachedFiles[k].Size];
int bufPosn=0, len=0;
while ((len = br.Read( attachment.Data, bufPosn, attachment.Data.Length-bufPosn )) > 0)
{
bufPosn += len;
}
br.Close();
}
}
BinaryWriter:
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter binWriter = new BinaryWriter(fs);
binWriter.Write( content, 0, content.Length );
binWriter.Close();
fs.Close();
很高兴收到建议。
【问题讨论】:
-
content变量是什么? xlsx 的格式不同,所以如何创建content很重要。 -
只是猜测,也许你的 .xlsx 是 Unicode,你需要用 BinaryWriter(Stream, Encoding) 构造函数来构造你的 BinaryWriter。
-
xlsx 本质上是一个压缩的 xml 文件。
标签: c# excel-2007 binary-data binaryreader binarywriter