703
社区成员
发帖
与我相关
我的任务
分享
Variant vExcelApp;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBoxW(0, L"启动 Excel 出错, 可能是没有安装Excel.",
L"Error", MB_OK | MB_ICONERROR);
vExcelApp = Unassigned;
return;
}
vExcelApp.OlePropertySet("Visible", true);
String strXlsFile = L"D:\\ccrun\\123.xls";
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", WideString(strXlsFile));
Variant vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);
// 在第二行第三列的格子里插入超链接
vSheet.OlePropertyGet("Cells", 2, 3).OleProcedure("Select");
String strLinkFile = L"D:\\ccrun\\123.bmp";
String strScreenTip = L"点击打开图片";
String strTextToDisplay = L"链接图片";
vSheet.OlePropertyGet("Hyperlinks").OleFunction("Add",
vExcelApp.OlePropertyGet("Selection"), // Anchor
WideString(strLinkFile), // Address
WideString(strLinkFile), // SubAddress
WideString(strScreenTip), // ScreenTip
WideString(strTextToDisplay) // TextToDisplay
);
...
void __fastcall TfmMain::ADOQueryExcel(TADOQuery *dbg, String strXlsFile)
{
///修改妖哥(ccrun)的 DBGRID转EXCEL,改为ADOQUERY 转EXCEL
if(!dbg->Active) // 数据集没有打开就返回
{ return;}
Variant vExcelApp, vSheet;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, L"启动 Excel 出错, 可能是没有安装Excel.",
L"DBGrid2Excel", MB_OK);
return;
}
// 隐藏Excel界面
vExcelApp.OlePropertySet((WideString)L"Visible", false);
// 新建一个工作表
vExcelApp.OlePropertyGet((WideString)L"Workbooks").OleFunction((WideString)L"Add"); // 工作表
// 操作这个工作表
vSheet = vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
.OlePropertyGet((WideString)L"Sheets", 1);
//设置Excel文档的字体
vSheet.OleProcedure((WideString)L"Select");
vSheet.OlePropertyGet((WideString)L"Cells").OleProcedure((WideString)L"Select");
vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
.OlePropertySet((WideString)L"Size", 12);
vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
.OlePropertySet((WideString)L"Name", L"新宋体");
vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
.OlePropertySet((WideString)L"FontStyle", L"常规");
vSheet.OlePropertyGet((WideString)L"Cells", 1, 1).OleProcedure((WideString)L"Select");
// 表格的行数
int nRowCount=dbg->RecordCount+1; // 表格的列数
int nColCount=dbg->FieldCount; //表格的行数
nColCount = nColCount < 1? 1: nColCount;
//设置单元格的宽度
for(int i=0; i<nColCount; i++)
{
//int nColWidth = dbg->Columns->Items[i]->Width;
vSheet.OlePropertyGet((WideString)L"Columns",i+1).OlePropertySet((WideString)L"ColumnWidth",10);
}
// 先将列名写入Excel表格
for(int j=0; j<nColCount; j++)
{
// 标题行的行高
vExcelApp.OlePropertyGet((WideString)L"Rows", 1).OlePropertySet((WideString)L"RowHeight", 20);
//WideString tem=
vSheet.OlePropertyGet((WideString)L"Cells", 1, j + 1)
.OlePropertySet((WideString)L"Value",
(WideString)dbg->Fields->Fields[j]->FieldName);
// 设置列名单元格的背景色
Variant vInter = vSheet.OlePropertyGet(
(WideString)L"Cells", 1, j + 1).OlePropertyGet((WideString)L"Interior");
vInter.OlePropertySet((WideString)L"ColorIndex", 15); // 灰色
vInter.OlePropertySet((WideString)L"Pattern", 1); // xlSolid
vInter.OlePropertySet((WideString)L"PatternColorIndex", -4105); // xlAutomatic
}
// 将ADOQuery中的数据写入Excel表格
dbg->First();
for(int i=0; i<nRowCount-1; i++)
{
// 普通数据行的行高16
vExcelApp.OlePropertyGet((WideString)L"Rows", i + 2).OlePropertySet((WideString)L"RowHeight", 16);
// 63 63 72 75 6E 2E 63 6F 6D
for(int j=0; j<dbg->FieldCount; j++)
{
if((dbg->Fields->Fields[j]->FieldName=="图片1")||||(dbg->Fields->Fields[j]->FieldName=="图片2")||(dbg->Fields->Fields[j]->FieldName=="图片3"))
{
WideString data1=dbg->FieldByName(dbg->Fields->Fields[j]->FieldName)->AsString;
WideString data2=ExtractFileName(data1);
vSheet.OlePropertyGet((WideString)L"Cells", i + 2, j + 1).OleProcedure("Select");
String strLinkFile = L".\\"+data2;
String strScreenTip = L"点击打开图片";
String strTextToDisplay = L"链接图片";
vSheet.OlePropertyGet("Hyperlinks").OleFunction("Add",
vExcelApp.OlePropertyGet("Selection"), // Anchor
WideString(strLinkFile), // Address
WideString(strLinkFile), // SubAddress
WideString(strScreenTip), // ScreenTip
WideString(strTextToDisplay) // TextToDisplay
);
}
else
{
Variant vCell = vSheet.OlePropertyGet((WideString)L"Cells", i + 2, j + 1);
WideString data="'"+dbg->FieldByName(dbg->Fields->Fields[j]->FieldName)->AsString;
vCell.OlePropertySet((WideString)L"Value",data);
}
}
dbg->Next();
}
dbg->First();
// 保存Excel文档并退出
WideString str=strXlsFile;
vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
.OleFunction((WideString)L"SaveAs",str);
vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
.OleFunction((WideString)L"Close");
vExcelApp.OleFunction((WideString)L"Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
// 工作结束
MessageBox(0, L"生成excel表格成功!",
L"消息", MB_OK);
}
//---------------------------------------------------------------------------