這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在C++中使用COM接口操作PPT,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
楊浦網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),楊浦網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為楊浦上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的楊浦做網(wǎng)站的公司定做!
1.1 進(jìn)入類(lèi)向?qū)?/strong>

1.2 添加PowerPoint COM接口


1.3 添加Excel COM接口

選中所有的COM接口,生成接口文件。
三、 定義PPT文件基礎(chǔ)操作函數(shù)(頭文件略)
3.1 定義PPT應(yīng)用基礎(chǔ)對(duì)象
class CPPTObject
{
public:
CApplication m_PPTApp;
CSlides m_Slides;
CSlide m_curSlide;
CPresentation m_Presentaion;
};3.2 啟動(dòng)PowerPoint軟件,調(diào)用COM接口需要安裝Office
// 創(chuàng)建PPT應(yīng)用,啟動(dòng)powerpoint程序。
bool CPPTUtil::CreatePPTApplication()
{
COleException exception;
LPCSTR str = "Powerpoint.Application";
if(!m_pPPTObject->m_PPTApp.CreateDispatch(str, &exception))
{
AfxMessageBox(exception.m_sc, MB_SETFOREGROUND);
return false;
}
m_pPPTObject->m_PPTApp.put_Visible(true);
return true;
}3.3 打開(kāi)PPT模板文件。修改PPT內(nèi)容前,先打開(kāi)PPT。
// 打開(kāi)模板ppt。
bool CPPTUtil::OpenPPT(const std::string& pptPath)
{
CPresentations presentations = m_pPPTObject->m_PPTApp.get_Presentations();
m_pPPTObject->m_Presentaion = presentations.Open(CString(pptPath.c_str()), 0, 0, 1);
m_pPPTObject->m_Slides = m_pPPTObject->m_Presentaion.get_Slides();
return true;
}3.4 保存PPT文件內(nèi)容,關(guān)閉文件,退出PowerPoint程序。
// 關(guān)閉PPT,保存數(shù)據(jù)關(guān)閉。
bool CPPTUtil::ClosePPT()
{
m_pPPTObject->m_Presentaion.Save();
m_pPPTObject->m_Presentaion.Close();
m_pPPTObject->m_PPTApp.Quit();
return true;
}3.5 選中具體的PPT幻燈片。
// 選中PPT指定索引的幻燈片。
bool CPPTUtil::SelectSlide(long slideIndex)
{
if (slideIndex > m_pPPTObject->m_Slides.get_Count())
{
return false;
}
m_pPPTObject->m_curSlide = m_pPPTObject->m_Slides.Range(COleVariant(slideIndex));
return true;
}四、 修改文本編輯框函數(shù)
// 修改文本框
bool CPPTUtil::ModifyTextBox(const std::string& boxName, const std::string& strValue)
{
CShapes shapes = m_pPPTObject->m_curSlide.get_Shapes();
for(long i = 1; i <= shapes.get_Count(); ++i)
{
CShape shape(shapes.Item(COleVariant(i)));
CString name = shape.get_Name();
if(shape.get_Type() == (long)Office::msoTextBox
&& name.Compare(CString(boxName.c_str())) == 0)
{
CTextFrame textFrame = shape.get_TextFrame();
CTextRange textRange = textFrame.get_TextRange();
CString txt = textRange.get_Text();
textRange.put_Text(strValue.c_str());
}
}
return true;
}boxName對(duì)應(yīng)于PPT中的Shape Name。這個(gè)Shape Name貌似在PowerPoint中沒(méi)有地方能看到,也沒(méi)有辦法修改。只能在調(diào)試時(shí)記錄下來(lái)。
五、 修改PPT中的圖表函數(shù)。先在PPT中定義圖表模板,通過(guò)COM接口修改圖表數(shù)據(jù)。
5.1 定義圖表數(shù)據(jù)結(jié)構(gòu)。圖表的數(shù)據(jù)都是用Excel存儲(chǔ)的。
5.1.1 定義單元格數(shù)據(jù)結(jié)構(gòu)
CCellDataCom::CCellDataCom(const CellValueType valueType, const std::string& strValue,
const int iRow, const int iCol)
{
m_ValueType = valueType;
m_strValue = strValue;
m_strPos = indexToString(iRow, iCol);
}
// 獲取單元格值類(lèi)型
CellValueType CCellDataCom::getValueType()
{
return m_ValueType;
}
// 獲取字符串類(lèi)型值
const std::string& CCellDataCom::getStringValue()
{
return m_strValue;
}
// 獲取整型值
long CCellDataCom::getLongValue()
{
return atol(m_strValue.c_str());
}
// 獲取浮點(diǎn)類(lèi)型值
double CCellDataCom::getDoubleValue()
{
return atof(m_strValue.c_str());
}
// 獲取單元格位置名稱(chēng)
const std::string& CCellDataCom::getPos()
{
return m_strPos;
}
// 將單元格坐標(biāo)轉(zhuǎn)換名稱(chēng)字符串
CString CCellDataCom::indexToString( int row, int col )
{
CString strResult;
if( col > 26 )
{
strResult.Format(_T("%c%c%d"),'A' + (col-1)/26-1,'A' + (col-1)%26,row);
}
else
{
strResult.Format(_T("%c%d"), 'A' + (col-1)%26,row);
}
return strResult;
}5.1.2 定義圖表數(shù)據(jù)結(jié)構(gòu)
// 插入一行記錄
void CChartDataCom::insertRowData(const std::list<CCellDataCom>& lstRowData)
{
m_lstValue.push_back(lstRowData);
}
// 獲取圖表數(shù)據(jù)
const std::list<std::list<CCellDataCom> >& CChartDataCom::getValue()const
{
return m_lstValue;
}5.2 修改圖表數(shù)據(jù)函數(shù)
// 修改圖表
bool CPPTUtil::ModifyChart(const std::string& chartName, const CChartDataCom& chartData)
{
CShapes shapes = m_pPPTObject->m_curSlide.get_Shapes();
for(long i = 1; i <= shapes.get_Count(); ++i)
{
CShape shape(shapes.Item(COleVariant(i)));
if(shape.get_Type() != (long)Office::msoChart
|| chartName != std::string(shape.get_Name().GetBuffer()))
{
continue;
}
// 修改圖表數(shù)據(jù)
return ModifyChartData(shape.get_Chart(), chartData);
}
return false;
}
// 修改圖表數(shù)據(jù)
bool CPPTUtil::ModifyChartData(CChart chart, const CChartDataCom& chartData)
{
// 激活圖表組件的excel數(shù)據(jù)表格,打開(kāi)內(nèi)嵌的excel.
CChartData chartDataModel = chart.get_ChartData();
chartDataModel.Activate();
CWorkbook workBook = chartDataModel.get_Workbook();
CWorksheets sheets = workBook.get_Worksheets();
if(sheets.get_Count() == 0)
{
return false;
}
// 獲取第一個(gè)sheet, 圖表組件的數(shù)據(jù)都在內(nèi)嵌excel的第一個(gè)sheet頁(yè)。
VARIANT vaSheetIndex;
vaSheetIndex.vt = VT_I4;
vaSheetIndex.lVal = 1;
CWorksheet sheet = sheets.get_Item(vaSheetIndex);
bool bRet = true;
// 循環(huán)修改單元格的數(shù)據(jù)
const std::list<std::list<CCellDataCom> >& lstValue = chartData.getValue();
std::list<std::list<CCellDataCom> >::const_iterator iterAllData = lstValue.begin();
for(; iterAllData != lstValue.end(); ++iterAllData)
{
std::list<CCellDataCom>::const_iterator iterRowData = iterAllData->begin();
for(; iterRowData != iterAllData->end(); ++iterRowData)
{
bRet = ModifyCellData(sheet, *iterRowData);
if(bRet == false)
{
break;
}
}
if(bRet == false)
{
break;
}
}
// 關(guān)閉Excel
CApplication0 app0 = workBook.get_Application();
app0.Quit();
Sleep(2000);
return bRet;
}
// 修改單元格數(shù)據(jù)
bool CPPTUtil::ModifyCellData(CWorksheet sheet, CCellDataCom cellData)
{
const std::string& cellPos = cellData.getPos();
CRange range = sheet.get_Range(COleVariant(cellPos.c_str()), COleVariant(cellPos.c_str()));
COleVariant* pOleVar = NULL;
if(cellData.getValueType() == CELL_STRING_TYPE)
{
pOleVar = new COleVariant(CString(cellData.getStringValue().c_str()));
}
else if(cellData.getValueType() == CELL_LONG_TYPE)
{
pOleVar = new COleVariant(cellData.getLongValue());
}
else if(cellData.getValueType() == CELL_DOUBLE_TYPE)
{
pOleVar = new COleVariant(cellData.getDoubleValue());
}
else
{
return false;
}
range.put_Value2(*pOleVar);
delete pOleVar;
return true;
}六、 合并多個(gè)PPT文件函數(shù)
// 合并PPT
bool CPPTUtil::MergePPT(const std::string& outputPPTPath, const std::list<std::string>& lstMergePPTPath)
{
CApplication pptApp;
COleException exception;
// 打開(kāi)PowerPoint程序
LPCSTR str = "Powerpoint.Application";
if(!pptApp.CreateDispatch(str, &exception))
{
AfxMessageBox(exception.m_sc, MB_SETFOREGROUND);
return false;
}
pptApp.put_Visible(true);
// 打開(kāi)輸出文件
CPresentations presentations = pptApp.get_Presentations();
CPresentation outPresention = presentations.Open(CString(outputPPTPath.c_str()), 0, 0, 1);
// 循環(huán)打開(kāi)合并文件插入PPT頁(yè)面
std::list<std::string>::const_iterator iterMergeFile = lstMergePPTPath.begin();
for(; iterMergeFile != lstMergePPTPath.end(); ++iterMergeFile)
{
CPresentation mergePresention = presentations.Open(CString(iterMergeFile->c_str()), 0, 0, 1);
CSlides mergeSlides = mergePresention.get_Slides();
int pageNum = mergeSlides.get_Count();
mergePresention.Close();
// 合并PPT頁(yè)簽
CSlides outSlides = outPresention.get_Slides();
outSlides.InsertFromFile(CString(iterMergeFile->c_str()), outSlides.get_Count(), 1, pageNum);
}
outPresention.Save();
outPresention.Close();
pptApp.Quit();
return true;
}上述就是小編為大家分享的怎么在C++中使用COM接口操作PPT了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在C++中使用COM接口操作PPT
標(biāo)題路徑:http://chinadenli.net/article40/ihhcho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、做網(wǎng)站、用戶(hù)體驗(yàn)、商城網(wǎng)站、ChatGPT、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)