using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Biff8Excel.Excel { internal class ExcelBinaryWriter : BinaryWriter, IDisposable { //static int tmp ; //用于tmp文件不重覆 string m_FileName; public ExcelBinaryWriter() { //m_FileName = "btw" + tmp.ToString() + ".tmp"; //tmp++; //生成一临时文件 m_FileName = Path.GetTempFileName(); FileInfo myFileInfo = new FileInfo(m_FileName); myFileInfo.Attributes = FileAttributes.Temporary; } public long Length { get { //StreamWriter myWriter = File.AppendText(m_FileName); if (!OutStream.CanRead) OutStream = File.Open(m_FileName, FileMode.Open); return this.OutStream.Length; } } public long Position { get { if (OutStream.CanRead) return OutStream.Position; return 0; } set { if (!OutStream.CanWrite) OutStream = File.Open(m_FileName, FileMode.Open); OutStream.Position = value; } } public byte[] Stream { get { if (OutStream == System.IO.Stream.Null) return null; if ( !OutStream.CanRead ) OutStream = File.Open(m_FileName, FileMode.Open); int rdlen = (int)this.OutStream.Length; byte[] b = new byte[rdlen]; this.OutStream.Seek(0, SeekOrigin.Begin); this.OutStream.Read(b, 0, rdlen); return b; } } public override void Write(byte[] buffer) { if (buffer == null) return; int len = buffer.Length; if (len == 0) return; //if (OutStream == System.IO.Stream.Null) // OutStream = File.Create(fn); if ( OutStream == System.IO.Stream.Null) OutStream = File.Open(m_FileName, FileMode.Truncate); OutStream.Write(buffer, 0, len); //OutStream.Flush(); } internal void clear() { //删除临时文件 OutStream.Close(); if (File.Exists(m_FileName)) File.Delete(m_FileName); m_FileName = string.Empty; } /// /// 删除临时文件 /// ~ExcelBinaryWriter() { clear(); } } }