我正在与一个SQDB合作,把Excel文档(连同其他档案类型,如PDF)作为双向数据储存起来。 我使用以下代码将这些档案输入档案系统。
The Problem: PDF files come out just fine. But for Excel, the files get created and when I try to open them, they crash or just give me garbage text.
我使用了前gu的这个法典,他撰写了这份复述文件。 该守则使用开放式光盘,因为我无法找到有用的在线文件。
//execution starts here
public override void SaveToDisk()
{
byte[] keys = { (byte)0xd0, (byte)0xcf };
//Searches through m_RawOleObject for combination of keys
int offset = Utils.SearchBytes(m_RawOleObject, keys); //returns 60 in case of Excel and 66 in case of Pdf
//m_RawOleOjbect contains the data from the sqlDataReader (the binary data from the column.)
m_RawOleObject = strip(m_RawOleObject, offset);
MemoryStream ms = new MemoryStream(m_RawOleObject);
CompoundFile cf = new CompoundFile(ms);
GetStorageByName(cf.RootStorage, m_StorageName);
if (Storage != null)
{
if (Storage is CFStream)
{
m_RawOleObject = (Storage as CFStream).GetData();
}
m_filename = System.IO.Path.Combine(STOREPATH, Utils.CombineFilenameWithExtension(Filename, m_extension));
WriteToFile(m_filename, m_RawOleObject);
}
}
protected void WriteToFile(string fn, byte[] obj)
{
fn = GetNextAvailableFilename(fn, 0);
FileStream fs = new FileStream(fn, FileMode.Create);
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(obj);
writer.Close();
fs.Close();
fs.Dispose();
}
protected void GetStorageByName(CFStorage cfs, string name)
{
VisitedEntryAction va = delegate(CFItem target)
{
if (target is CFStorage)
{
GetStorageByName((CFStorage)target, name);
}
else
{
if (target.Name == name)
Storage = target;
}
};
//Visit NON-recursively (first level only)
cfs.VisitEntries(va, false);
}
Any ideas what s happening here? why are the excel corrupted? I couldn t find a lot online despite hours of search!
任何想法、建议或解决办法都将受到赞赏。
Thanks