就是有個一個word模板,從資料庫中獲取一些資料,替換word模板中幾個地方,批量生成word檔案?
~妖哥幫忙一下
uj5u.com熱心網友回復:
僅供參考
void __fastcall TfmSearch::btExportWordClick(TObject *Sender)
{
try
{
if(edTollID->Text!=""&&edTollDate->Text!="")
{
AnsiString asDir;
AnsiString asSFile = fmMain->g_asWorkPath + "\\doc\\lt.doc";
AnsiString asDFile = "d:\\" + edTollID->Text + ".doc";
CopyFile(asSFile.c_str(),asDFile.c_str(),false);
//保存電子檔案
Variant vWordApp;
Variant vDoc, vTable,vCell;
DWORD ErrCode;
HWND hPrevApp = FindWindow(NULL,"Microsoft Word");
if(!hPrevApp)
{ //if word not started
try
{
vWordApp=CreateOleObject("Word.Application");
}
catch(...)
{
//MessageBox(Handle, "啟動Word出錯!", Application->Title.c_str(), MB_OK | MB_ICONERROR);
return;
}
}
else
vWordApp=GetActiveOleObject("Word.Application");
vWordApp.OlePropertySet("Visible", true);
WideString strDocName = asDFile;
vDoc = vWordApp.OlePropertyGet("Documents").OleFunction("Open", strDocName);
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);
vCell=vTable.OleFunction("Cell",1,2);
AnsiString Content=edTollID->Text;//FormatDateTime("yyyymmdd",fmMain->dtDOCDate);
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",1,4);
Content=edTollDate->Text;//fmMain->asDOCVehType;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",1,6);
Content=edLicPlate->Text;//fmMain->asDOCLic;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",1,8);
Content=edVehType->Text;//fmMain->asDOCOpTicket;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",2,2);
Content=edEPLAZAName->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",2,4);
Content=edOperatorName->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",2,6);
Content=edTollPay->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",2,8);
Content=edCarWeight->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",3,2);
Content=edOverloadPer->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",3,4);
Content=edGoodsName->Text;//fmMain->asDOCGoodsName;
if(edMixGoodsName->Text.Trim()!="")
Content+="+" +edMixGoodsName->Text.Trim();
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",3,6);
Content=edDensity->Text;
vCell.OlePropertySet("Range",Content.c_str());
vCell=vTable.OleFunction("Cell",3,8);
Content=edCheckName->Text;
vCell.OlePropertySet("Range",Content.c_str());
AnsiString strPicFile;
asDir="d:";
String picFileName;
try
{
picFileName= asDir+"\\前部照片.jpeg";
imgFront->Picture->SaveToFile(picFileName);
picFileName= asDir+"\\頂部照片.jpeg";
imgTop->Picture->SaveToFile(picFileName);
picFileName= asDir+"\\全景照片.jpeg";
imgTotal->Picture->SaveToFile(picFileName);
picFileName= asDir+"\\左后側照片.jpeg";
imgLeft->Picture->SaveToFile(picFileName);
picFileName= asDir+"\\右后側照片.jpeg";
imgRight->Picture->SaveToFile(picFileName);
picFileName= asDir+"\\貨物照片.jpeg";
imgCheck->Picture->SaveToFile(picFileName);
ExportDVfile();//匯出錄像
}
catch(...)
{
//
}
try
{
strPicFile = asDir + "\\前部照片.jpeg";
Variant vShape;
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",1,1);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
strPicFile = asDir + "\\頂部照片.jpeg";
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",1,2);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
strPicFile = asDir + "\\全景照片.jpeg";
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",1,3);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
strPicFile = asDir + "\\左后側照片.jpeg";
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",3,1);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
strPicFile = asDir + "\\右后側照片.jpeg";
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",3,2);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
strPicFile = asDir + "\\貨物照片.jpeg";
vTable=vWordApp.OlePropertyGet("ActiveDocument").OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 2);
vCell=vTable.OleFunction("Cell",3,3);
vCell.OleFunction("select");
vShape =vWordApp.OlePropertyGet("Selection")
.OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile.c_str(),false,true);
vWordApp.OlePropertyGet("Documents").OleFunction("Save");
// vWordApp.OlePropertyGet("Documents").OleFunction("Close");
// vWordApp.OleFunction("Quit");
}
catch(...)
{
//
}
// fmMain->g_bCancle = true;
/*
if(!fmMain->g_bDOCPhotoErr)
{
//洗掉照片
AnsiString usPhotoName;
usPhotoName = asDir + "\\車輛全貌.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
usPhotoName = asDir + "\\前部照片.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
usPhotoName = asDir + "\\上方貨物.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
usPhotoName = asDir + "\\左側貨物.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
usPhotoName = asDir + "\\右側貨物.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
usPhotoName = asDir + "\\后方貨物.jpeg";
if(FileExists(usPhotoName)) DeleteFile(usPhotoName);
}*/
}
else
{
ShowMessage("請選擇單個車輛!");
}
}
catch(...)
{
ShowMessage("匯出資料失敗!");
}
}
uj5u.com熱心網友回復:
lt.doc是模板轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/146103.html
上一篇:我在撰寫一個管理程式,遇到了些問題希望大家能幫助我.......我想在DBGrid中增加一個復選框,從而控制被選中資料
