Excel转HTML显示Excel格子
我如下代码不能显示Excel格子。
sFilePath:为Excel文件路径。
bool CModuleProperty::WriteReportToHtml(CString sFilePath)
{
CString strHtmSavePath;
CString ReportName=GetReportName(sFilePath);
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application oApp;
_Workbook oBook;
Workbooks oBooks;
_Workbook oHTML;
Worksheets oSheets;
_Worksheet oSheet;
Range oRange;
AddIns oAddIns;
AddIn oAddIn;
VARIANT ObjToConvert[1];
ObjToConvert[0].vt = VT_DISPATCH;
ObjToConvert[0].pdispVal = oRange; //We'll set this value later.
//Launch Excel and make it visible.
oApp.CreateDispatch("Excel.Application");
oApp.SetVisible(FALSE);
//Get the Workbooks collection.
oBooks = oApp.GetWorkbooks();
// Excel 2000 takes 13 arguments
//Open the test workbook.
oBook = oBooks.Open(sFilePath, // This is a test workbook.
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional); // Open for Excel 2000 has 13 parameters
oSheets = oBook.GetWorksheets();
//Get the worksheet 1.
oSheet = oSheets.GetItem(COleVariant((short)1)); // Worksheet 1
//Get the AddIns collection.
oAddIns = oApp.GetAddIns();
//Search for the HTML.XLA AddIn.
long lCount = oAddIns.GetCount();
for (long l = 1; l<=lCount; l++)
{
oAddIn = oAddIns.GetItem(COleVariant((long)l));
if(oAddIn.GetName() == "HTML.XLA")
{
break; // AddIn is checked in the list
}
}
//Make sure the HTML.XLA AddIn is installed.
if(!oAddIn.GetInstalled())
{
AfxMessageBox("Installing AddIn");
oAddIn.SetInstalled(TRUE);
}
oHTML = oBooks.Open(oAddIn.GetFullName(), // Returns a _Workbook
// object reference.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional
);
//Get the Range we want to convert to HTML.
char cMaxCol[5];
if(m_pReportModule.MaxCol<=25)
{
cMaxCol[0]='A'+ m_pReportModule.MaxCol;
cMaxCol[1]='\0';
}
else
{
cMaxCol[0]='A'+ m_pReportModule.MaxCol/26-1;
cMaxCol[1]='A'+ m_pReportModule.MaxCol%26+1;
cMaxCol[2]='\0';
}
CString strMaxSize;
strMaxSize.Format("%s%d",cMaxCol,m_pReportModule.MaxRow+1);
oRange = oApp.GetRange(COleVariant("A1"), COleVariant(strMaxSize));
ObjToConvert[0].pdispVal = oRange;
CString strToName ;
strToName=ReportName+".htm";
m_HtmlSourcePath = strToName;