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;
VARIANT Result;
Result = oApp.Run(COleVariant("htmlconvert"), // The "macro" name
COleVariant(ObjToConvert), // An array of Variants
covFalse, //UseExistingFile
covFalse, //UseFrontPageForExistingFile
covFalse, //AddToFrontPageWeb
COleVariant("1252"), //CodePage (1252 U.S./Western Europe)
COleVariant(strToName), //HTMLFilePath
COleVariant(ReportName), //TitleFullPage
covTrue, //LineBeforeTableFullPage
COleVariant(""), //NameFullPage
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional // Run() takes 31 parameters!!
);
oHTML.SetSaved(TRUE);
oBook.SetSaved(TRUE);
//oAddIns.ReleaseDispatch();
//oAddIn.ReleaseDispatch();
oHTML.ReleaseDispatch();
oBook.ReleaseDispatch();
oApp.Quit();
oApp.ReleaseDispatch();
/* ReadHtmlINI(m_HtmlFileNum);
CString sFile, sHtmlName;
for(int i=0; i<m_HtmlFileNum; i++)
{
bool bFlg = GetHtmlFiletName(m_HtmlSourcePath,sHtmlName);
if(bFlg)
{
sFile.Format("%s\\%s",m_pHtmlFilePath[i].file,sHtmlName);
CopyFile(m_HtmlSourcePath,sFile,FALSE);
}
}
if(m_pHtmlFilePath!=NULL)
{
delete [] m_pHtmlFilePath;
m_pHtmlFilePath = NULL;
}*/
return true;