604
社区成员
发帖
与我相关
我的任务
分享
#ifndef SheetH
#define SheetH
class CWorkSheet
{
public:
CWorkSheet();
~CWorkSheet();
Variant *pSheet;
Variant __fastcall GetCells(int i, int j);
void __fastcall SetCells(int i, int j, int Value);
void __fastcall SetCells(int i, int j, WideString Value);
void __fastcall SetCells(int i, int j, WideString Value, int Width);
int iRows;
int iColumns;
WideString __fastcall GetName();
void __fastcall SetName(WideString Name);
__property WideString Name = {read = GetName, write = SetName};
};
// ---------------------------------------------------------------------------
#endif
Sheet.cpp
#include <vcl.h>
#pragma hdrstop
#include <utilcls.h>
#include "Sheet.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
CWorkSheet::CWorkSheet()
{
}
CWorkSheet::~CWorkSheet()
{
}
Variant __fastcall CWorkSheet::GetCells(int i, int j)
{
return pSheet->OlePropertyGet("Cells", i, j).OlePropertyGet("value");
}
void __fastcall CWorkSheet::SetCells(int i, int j, int Value)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
}
void __fastcall CWorkSheet::SetCells(int i, int j, WideString Value)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
}
// ---------------------------------------------------------------------
void __fastcall CWorkSheet::SetCells(int i, int j, WideString Value, int Width)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
pSheet->OlePropertyGet("Columns",j).OlePropertySet("ColumnWidth", Width);
}
// -----------------------------------------------------------------------
WideString __fastcall CWorkSheet::GetName()
{
return VarToWideStr(pSheet->OlePropertyGet("name"));
}
void __fastcall CWorkSheet::SetName(WideString Name)
{
pSheet->OlePropertySet("name", Name);
}
Excel.h
// ---------------------------------------------------------------------------
#ifndef ExcelH
#define ExcelH
// ---------------------------------------------------------------------------
#include "Sheet.h"
class CExcel
{
private:
bool __fastcall OpenExcel();
bool __fastcall NewExcel();
int iCurrSheet;
WideString _FileName;
Variant vEx;
Variant vWb;
Variant vSh;
CWorkSheet *pWorkSheet;
public:
CExcel();
CExcel(WideString FileName);
~CExcel();
bool __fastcall SaveExcel();
bool __fastcall SaveAsExcel(WideString Name);
void __fastcall CloseExcel();
int iSheetCount;
CWorkSheet* __fastcall GetSheet(int i);
};
#endif
excel.cpp
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Excel.h"
#include <utilcls.h>
#include <ComObj.hpp>
// ---------------------------------------------------------------------------
#pragma package(smart_init)
CExcel::CExcel()
{
iCurrSheet = 0;
pWorkSheet = new CWorkSheet();
NewExcel();
}
CExcel::CExcel(WideString FileName)
{
_FileName = FileName;
pWorkSheet = new CWorkSheet();
OpenExcel();
}
CExcel::~CExcel()
{
CloseExcel();
}
bool __fastcall CExcel::OpenExcel()
{
try
{
CoInitialize(NULL);
vEx = CreateOleObject("Excel.Application");
vEx.OlePropertySet("Visible", false);
vEx.OlePropertySet("DisplayAlerts", false);
vEx.OlePropertyGet("WorkBooks").OleProcedure("Open",_FileName.c_bstr());
vWb = vEx.OlePropertyGet("ActiveWorkBook");
vSh = vWb.OlePropertyGet("ActiveSheet");
pWorkSheet->pSheet = &vSh;
iSheetCount = vWb.OlePropertyGet("sheets").OlePropertyGet("count");
return true;
}
catch (...)
{
return false;
}
}
bool __fastcall CExcel::NewExcel()
{
try
{
CoInitialize(NULL);
vEx = Variant::CreateObject("Excel.Application");
vEx.OlePropertySet("Visible", false);
vEx.OlePropertyGet("WorkBooks").OleFunction("Add");
vWb = vEx.OlePropertyGet("ActiveWorkBook");
vSh = vWb.OlePropertyGet("ActiveSheet");
pWorkSheet->pSheet = &vSh;
iSheetCount = vWb.OlePropertyGet("sheets").OlePropertyGet("count");
return true;
}catch (...)
{
return false;
}
}
// ---------------------------------------------------------------------
bool __fastcall CExcel::SaveExcel()
{
try
{
vWb.OleFunction("save");
return true;
}
catch (...)
{
return false;
}
}
bool __fastcall CExcel::SaveAsExcel(WideString Name)
{
AnsiString sFileName;
sFileName = Name;
if (FileExists(Name))
DeleteFile(sFileName);
try
{
vWb.OleFunction("SaveAs", Name.c_bstr());
_FileName = Name;
return true;
}catch (...)
{
return false;
}
}
void __fastcall CExcel::CloseExcel()
{
if( NULL != pWorkSheet )
{
delete pWorkSheet;
pWorkSheet = NULL ;
}
vWb.OleFunction("close");
vEx.OleFunction("quit");
vWb = Unassigned;
vEx = Unassigned;
CoUninitialize();
}
// ---------------------------------------------------------------------
CWorkSheet *__fastcall CExcel::GetSheet(int i)
{
if (i == iCurrSheet)
{
return pWorkSheet;
}
else if (i < 1 || i > iSheetCount)
{
return NULL;
}
else
{
iCurrSheet = i;
}
vSh = vWb.OlePropertyGet("Sheets", i);
pWorkSheet->pSheet = &vSh;
pWorkSheet->iRows = vSh.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("count");
pWorkSheet->iColumns = vSh.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("count");
return pWorkSheet;
}
没注释,请将就着看吧//---------------------------------------------------------------------------
//功能: 导出EXCEL ,保存路径 ,是否预览
//---------------------------------------------------------------------------
void __fastcall TfrmMain::DBGToExcel(TADOQuery * ADOQuery, TDBGrid * DBGrid, String XlsFile, bool PreView)
{
ADOQuery->First();
if (ADOQuery->RecordCount == 0)
{
MessageBox(NULL, "无导出内容,操作取消!", "提示", MB_OK | MB_ICONINFORMATION);
return;
}
TFileStream *fs=new TFileStream(XlsFile,fmCreate);
String lf = "\r\n"; // 换行符
String tab = char(9); // 换列符
String v;
//导出字段名
for (int i = 0; i < DBGrid->Columns->Count; i++)
{
if (DBGrid->Columns->Items[i]->Visible == true)
{
v = DBGrid->Columns->Items[i]->Title->Caption;// FieldName;
v = v + tab; // 下一列
fs->Write((void *)v.c_str(),v.Length());
}
}
//导出字段内容
fs->Write((void *)lf.c_str(),lf.Length()); //下一行
for (int j = 0;j < ADOQuery->RecordCount; j++)
{
for (int i = 0;i < DBGrid->Columns->Count; i++)
{
if (DBGrid->Columns->Items[i]->Visible == true)
{
v = ADOQuery->Fields->Fields[i]->AsString.Trim();
v = v + tab; // 下一列
fs->Write((void *)v.c_str(),v.Length());
}
}
fs->Write((void *)lf.c_str(),lf.Length()); //下一行
ADOQuery->Next();
}
delete fs;
if (PreView)
ShellExecute(NULL,"open",XlsFile.c_str(),NULL,NULL,SW_SHOW);
else
MessageBox(NULL, "导出完毕!", "提示", MB_OK | MB_ICONINFORMATION);
}