You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.4 KiB
C++

#include "StdAfx.h"
#include "ExcelMgr.h"
#include "MsgBox.h"
#include "LogMgr.h"
//һ<>㰲װ<E3B0B2><D7B0>WPS2019 <20>Ϳ<EFBFBD><CDBF><EFBFBD>ʹ<EFBFBD><CAB9>
CExcelMgr::CExcelMgr(void)
{
}
CExcelMgr::~CExcelMgr(void)
{
m_bCreateOk = false;//<2F>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Excel <20>ļ<EFBFBD>
}
bool CExcelMgr::CreatExcelFile()
{
/* COleVariant<6E><74>ΪVARIANT<4E><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵İ<CDB5>װ<EFBFBD><D7B0>
<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ARIANT<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>COleVariant<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>˵ġ<EFBFBD>
*/
//covOptional <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>VARIANT<4E><54><EFBFBD><EFBFBD>
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!m_app.CreateDispatch("Excel.Application") )
{
//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>WPS <20><><EFBFBD><EFBFBD>ǻᱨ<C7BB><E1B1A8>,<2C><><EFBFBD>Ժ<EFBFBD><D4BA>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CMsgBox MsgBox;
MsgBox.Show("<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ExcellentӦ<EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>°<EFBFBD>װExcel<65><6C>");//
return false;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_books=m_app.GetWorkbooks();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_book=m_books.Add(covOptional);
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_sheets=m_book.GetSheets();
//<2F><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_sheet=m_sheets.GetItem(COleVariant((short)1));
m_bCreateOk = true;//<2F>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Excel <20>ļ<EFBFBD>
return m_bCreateOk;
}
//<2F><>ʾExcel <20><><EFBFBD><EFBFBD>
void CExcelMgr::ShowExcelFile()
{
if(!m_bCreateOk)
return;
//<2F><>ʾExcel<65><6C><EFBFBD>񣬲<EFBFBD><F1A3ACB2><EFBFBD><EFBFBD><EFBFBD>״̬Ϊ<CCAC>û<EFBFBD><C3BB>ɿ<EFBFBD><C9BF><EFBFBD>
m_app.SetVisible(TRUE);
m_app.SetUserControl(TRUE);
}
//<2F><>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>A1,D4<44><34>)
CString CExcelMgr::GetCellIdxStr(int Row,int Col)
{
Col -= 1;
CString IdxStr;
int num = 65;// A<><41>Unicode<64><65>
CString colName = "";
do
{
if(colName.GetLength() > 0)
{
Col--;
}
int remainder = Col % 26;
colName = ((char) (remainder + num)) + colName;
Col = (int) ((Col - remainder) / 26);
} while (Col > 0);
IdxStr.Format(_T("%d"),Row);
IdxStr = colName+IdxStr;
//gLogMgr->WriteDebugLog(IdxStr);
return IdxStr;
}
//<2F><><EFBFBD>õ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>(<28><>Row <20>е<EFBFBD>Col <20><>)(bBold <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>)
void CExcelMgr::SetCellTxt(int Row,int Col,CString txt,bool bBold)
{
if(!m_bCreateOk)
return;
Range range; //<2F><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MyFont font;
Range cols;
//ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Row<6F>е<EFBFBD>Col<6F>еĵ<D0B5>Ԫ<EFBFBD><D4AA>
CString IdxStr = GetCellIdxStr(Row,Col);
range=m_sheet.GetRange(COleVariant(IdxStr),COleVariant(IdxStr));
//<2F><><EFBFBD><EFBFBD>A1="HELLO EXCEL"
range.SetValue2(COleVariant(txt)); //range.SetValue()<29><>Ϊrange.SetValue2()
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>
if(bBold)
{
font=range.GetFont();
font.SetBold(COleVariant((short)TRUE));
}
//ѡ<><D1A1>A<EFBFBD><41>A<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>Ϊ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Ӧ
cols=range.GetEntireColumn();
cols.AutoFit();
}
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ͼƬ(Row1,Col1,Row2,Col2 ָ<><D6B8>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Χ)FilePath <20><>ͼƬ<CDBC><C6AC><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
void CExcelMgr::InsertPicture(int Row1,int Col1,int Row2,int Col2,CString FilePath)
{
//<2F><>Sheet<65><74><EFBFBD><EFBFBD><EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD>һ<EFBFBD><D2BB>Shapes
Range range; //<2F><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Shapes shapes=m_sheet.GetShapes();
//<2F><><EFBFBD><EFBFBD>Range<67><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬ
range=m_sheet.GetRange(COleVariant(GetCellIdxStr(Row1,Col1)),COleVariant(GetCellIdxStr(Row2,Col2)));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬ<CDBC>ijߴ<C4B3>
//<2F>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ͼƬ
shapes.AddPicture(FilePath,false,true,(float)range.GetLeft().dblVal,(float)range.GetTop().dblVal,(float)range.GetWidth().dblVal,(float)range.GetHeight().dblVal);
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>
ShapeRange sRange=shapes.GetRange(_variant_t(long(1)));
sRange.SetHeight(float(400));
sRange.SetWidth(float(550));
}