#include "stdafx.h"
#include "BasicExcelVC6.h"
namespace YCompoundFiles
{
/********************************** Start of Class Block *************************************/
// PURPOSE: Manage a file by treating it as blocks of data of a certain size.
Block::Block() :
blockSize_(512), fileSize_(0), indexEnd_(0),
filename_(0) {}
bool Block::Create(const wchar_t* filename)
// PURPOSE: Create a new block file and open it.
// PURPOSE: If file is present, truncate it and then open it.
// PROMISE: Return true if file is successfully created and opened, false if otherwise.
{
// Create new file
#ifdef NON_ENGLISH_PATH_FILENAME_SUPPORT
size_t filenameLength = wcslen(filename) * 2;
#else
size_t filenameLength = wcslen(filename);
#endif
char* name = new char[filenameLength+1];
wcstombs(name, filename, filenameLength);
name[filenameLength] = 0;
file_.open(name, ios_base::out | ios_base::trunc);
file_.close();
file_.clear();
// Open the file
bool ret = this->Open(filename);
delete[] name;
return ret;
}
bool Block::Open(const wchar_t* filename, ios_base::openmode mode)
// PURPOSE: Open an existing block file.
// PROMISE: Return true if file is successfully opened, false if otherwise.
{
// Open existing file for reading or writing or both
#ifdef NON_ENGLISH_PATH_FILENAME_SUPPORT
size_t filenameLength = wcslen(filename) * 2;
#else
size_t filenameLength = wcslen(filename);
#endif
filename_.resize(filenameLength+1, 0);
wcstombs(&*(filename_.begin()), filename, filenameLength);
file_.open(&*(filename_.begin()), mode | ios_base::binary);
if (!file_.is_open()) return false;
mode_ = mode;
// Calculate filesize
if (mode & ios_base::in)
{
file_.seekg(0, ios_base::end);
fileSize_ = file_.tellg();
}
else if (mode & ios_base::out)
{
file_.seekp(0, ios_base::end);
fileSize_ = file_.tellp();
}
else
{
this->Close();
return false;
}
// Calculate last index + 1
indexEnd_ = fileSize_/blockSize_ + (fileSize_ % blockSize_ ? 1 : 0);
return true;
}
bool Block::Close()
// PURPOSE: Close the opened block file.
// PROMISE: Return true if file is successfully closed, false if otherwise.
{
file_.close();
file_.clear();
filename_.clear();
fileSize_ = 0;
indexEnd_ = 0;
blockSize_ = 512;
return !file_.is_open();
}
bool Block::IsOpen()
// PURPOSE: Check if the block file is still opened.
// PROMISE: Return true if file is still opened, false if otherwise.
{
return file_.is_open();
}
bool Block::Read(size_t index, char* block)
// PURPOSE: Read a block of data from the opened file at the index position.
// EXPLAIN: index is from [0..].
// PROMISE: Return true if data are successfully read, false if otherwise.
{
if (!(mode_ & ios_base::in)) return false;
if (index < indexEnd_)
{
file_.seekg(index * blockSize_);
file_.read(block, blockSize_);
return !file_.fail();
}
else return false;
}
bool Block::Write(size_t index, const char* block)
// PURPOSE: Write a block of data to the opened file at the index position.
// EXPLAIN: index is from [0..].
// PROMISE: Return true if data are successfully written, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
file_.seekp(index * blockSize_);
file_.write(block, blockSize_);
if (indexEnd_ <= index)
{
indexEnd_ = index + 1;
fileSize_ += blockSize_;
}
file_.close();
file_.clear();
file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
return file_.is_open();
}
bool Block::Swap(size_t index1, size_t index2)
// PURPOSE: Swap two blocks of data in the opened file at the index positions.
// EXPLAIN: index1 and index2 are from [0..].
// PROMISE: Return true if data are successfully swapped, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
if (index1 < indexEnd_ && index2 < indexEnd_)
{
if (index1 == index2) return true;
char* block1 = new char[blockSize_];
if (!this->Read(index1, block1)) return false;
char* block2 = new char[blockSize_];
if (!this->Read(index2, block2)) return false;
if (!this->Write(index1, block2)) return false;
if (!this->Write(index2, block1)) return false;
delete[] block1;
delete[] block2;
return true;
}
else return false;
}
bool Block::Move(size_t from, size_t to)
// PURPOSE: Move a block of data in the opened file from an index position to another index position.
// EXPLAIN: from and to are from [0..].
// PROMISE: Return true if data are successfully moved, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
if (from < indexEnd_ && to < indexEnd_)
{
if (to > from)
{
for (size_t i=from; i!=to; ++i)
{
if (!this->Swap(i, i+1)) return false;
}
}
else
{
for (size_t i=from; i!=to; --i)
{
if (!this->Swap(i, i-1)) return false;
}
}
return true;
}
else return false;
}
bool Block::Insert(size_t index, const char* block)
// PURPOSE: Insert a new block of data in the opened file at the index position.
// EXPLAIN: index is from [0..].
// PROMISE: Return true if data are successfully inserted, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
if (index <= indexEnd_)
{
// Write block to end of file
if (!this->Write(indexEnd_, block)) return false;
// Move block to index if necessary
if (index < indexEnd_-1) return this->Move(indexEnd_-1, index);
else return true;
}
else
{
// Write block to index after end of file
return this->Write(index, block);
}
}
bool Block::Erase(size_t index)
// PURPOSE: Erase a block of data in the opened file at the index position.
// EXPLAIN: index is from [0..].
// PROMISE: Return true if data are successfully erased, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
if (index < indexEnd_)
{
fileSize_ -= blockSize_;
indexEnd_ -= 1;
// Read entire file except the block to be deleted into memory.
char* buffer = new char[fileSize_];
for (size_t i=0, j=0; i!=indexEnd_+1; ++i)
{
file_.seekg(i*blockSize_);
if (i != index)
{
file_.read(buffer+j*blockSize_, blockSize_);
++j;
}
}
file_.close();
file_.open(&*(filename_.begin()), ios_base::out | ios_base::trunc | ios_base::binary);
file_.write(buffer, fileSize_); // Write the new file.
file_.close();
file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
delete[] buffer;
return true;
}
else return false;
}
bool Block::Erase(vector<size_t>& indices)
// PURPOSE: Erase blocks of data in the opened file at the index positions.
// EXPLAIN: Each index in indices is from [0..].
// PROMISE: Return true if data are successfully erased, false if otherwise.
{
if (!(mode_ & ios_base::out)) return false;
// Read entire file except the blocks to be deleted into memory.
size_t maxIndices = indices.size();
fileSize_ -= maxIndices*blockSize_;
char* buffer = new char[fileSize_];
for (size_t i=0, k=0; i!=indexEnd_; ++i)
{
file_.seekg(i*blockSize_);
bool toDelete = false;
for (size_t j=0; j<maxIndices; ++j)
{
if (i == indices[j])
{
toDelete = true;
break;
}
}
if (!toDelete)
{
file_.read(buffer+k*blockSize_, blockSize_);
++k;
}
}
indexEnd_ -= maxIndices;
file_.close();
file_.open(&*(filename_.begin()), ios_base::out | ios_base::trunc | ios_base::binary);
file_.write(buffer, fileSize_); // Write the new file.
file_.close();
file_.open(&*(filename_.begin()), mode_ | ios_base::binary);
delete[] buffer;
return true;
}
/********************************** End of Class Block ***************************************/
/********************************** Start of Class Header ************************************/
// PURPOSE: Read and write data to a compound file header.
CompoundFile::Header::Header() :
fileType_(0xE11AB1A
没有合适的资源?快使用搜索试试~ 我知道了~
BasicExcelSample.zip
共24个文件
h:6个
cpp:5个
xls:3个
5星 · 超过95%的资源 需积分: 17 505 下载量 129 浏览量
2011-04-06
10:37:57
上传
评论 7
收藏 336KB ZIP 举报
温馨提示
支持中文的EXCEL处理,有好多的人在找这段代码,所有先放这让大家共享下载吧!
资源推荐
资源详情
资源评论
收起资源包目录
BasicExcelSample.zip (24个子文件)
ExcelTabDlg.h 2KB
BasicExcelVC6.h 46KB
ExcelTabDlg_vc90.vcproj 22KB
StdAfx.cpp 210B
ExcelTabDlg.rc 5KB
resource.h 772B
ExcelTab.h 1KB
ExcelTabDlg_vc90.sln 1KB
UnicodeRelease
vc60
example.xls 22KB
ExcelTabDlgU.exe 244KB
vc90
example.xls 22KB
ExcelTabDlgU.exe 191KB
BasicExcel.cpp 180KB
ExcelTabDlg_vc60.dsp 8KB
ExcelTabDlg_vc60.dsw 547B
example.xls 22KB
StdAfx.h 2KB
BasicExcelVC6.cpp 180KB
ExcelTab.cpp 2KB
ExcelTabDlg.cpp 11KB
BasicExcel.h 46KB
res
ExcelTabDlg.ico-backup 66KB
ExcelTabDlg.ico 29KB
ExcelTabDlg.rc2 400B
共 24 条
- 1
动起手来实现白日梦
- 粉丝: 144
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
- 123321123323211
- yolov8实战第八天-pyqt5-yolov8实现车牌识别系统(论文(约7000字)+数据集+完整部署代码+代码使用说明)
- 三相桥式全桥整流电路MATALB Simulink仿真文件
- ABB机器人操作培训文档
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页