导出HTML文件与图像到Excel单XLS文件? [英] export HTML files with images to Excel single XLS file?

查看:538
本文介绍了导出HTML文件与图像到Excel单XLS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用C ++ OLE自动化将包含图片的HTML文件导出到Excel。



代码示例:

  #import\\alpha \sdk\mso\office12\mso.dllrename(RGB,MSORGB)rename(DocumentProperties ,MSODocumentProperties)rename(SearchPath,MSOSearchPath)
using namespace Office;
#import\\alpha\sdk\mso\office12\VBE6EXT.OLBrename(RGB,MSORGB)rename(EOF,EndOfFile)
使用命名空间VBIDE;
#import\\alpha\sdk\mso\office12\excel.exerename(DialogBox,ExcelDialogBox)rename(RGB,ExcelRGB)rename重命名(ReplaceText,ExcelReplaceText)no_auto_exclude
#import\\alpha \sdk\mso\office12\msword.olbrename ,WordExitWindows)rename(FindText)重命名(CopyFile,WordReplaceText)重命名( ,WordFindText)no_auto_exclude

//创建Excle应用程序OLE obj ...
Excel :: _ ApplicationPtr pApplication;
HRESULT hRes = pApplication.CreateInstance(_T(Excel.Application));
if(hRes == S_OK){
pApplication-> PutDisplayAlerts(MAKELCID(MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),SORT_DEFAULT),VARIANT_FALSE);
pApplication-> PutCopyObjectsWithCells(MAKELCID(MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),SORT_DEFAULT),VARIANT_TRUE);

//打开prepeared用于导出HTML文件
Excel :: _ WorkbookPtr pBook = pApplication-> Workbooks-> Open(_bstr_t(szHTMLPath),_variant_t(long)Excel :: xlUpdateLinksAlways ),vtMissing,vtMissing / * _variant_t((long)Excel :: xlHtml)* /);
if(pBook!= NULL){
//将打开的HTML文件保存为XSL文件
hRes = pBook-> SaveAs(_bstr_t(szXLSPath),_variant_t xlWorkbookNormal),vtMissing,vtMissing,vtMissing,vtMissing,Excel :: xlNoChange,_variant_t(long)Excel :: xlLocalSessionChanges),_variant_t(false));
if(hRes == S_OK){
// All is ok
}
pBook-> Close();
}
pApplication-> Quit();不幸的是,来自html代码的img图片不会转换为 strong>嵌入图片,但在外部链接图片中。

解决方案

简短讨论在Microsoft论坛允许形成更多或更少的工作代码。

  Excel :: ShapesPtr pShapes = Excel :: _ WorksheetPtr(pBook-> Worksheets-> Item [1]) - >形状; 
for(long nIndex = 1; nIndex< = pShapes-> GetCount(); nIndex ++)
{
Excel :: ShapePtr pShape = pShapes-> Item(nIndex);

const Office :: MsoShapeType nType = pShape-> Type;
switch(nType)
{
case Office :: msoLinkedPicture:
{
const float rLeft = pShape-> Left;
const float rTop = pShape-> Top;
const float rWidth = pShape-> Width;
const float rHeight = pShape-> Height;
const _bstr_t szPath = pShape-> AlternativeText;

Excel :: ShapePtr pNewShape = pShapes-> AddPicture(szPath,Office :: msoFalse,Office :: msoTrue,rLeft,rTop,rWidth,rHeight);
if(pNewShape)
{
pShape-> Delete();
nIndex = 0;
}
}
break;
}
}


I'm trying to export HTML file with pictures to Excel using C++ OLE Automation.

Sample of code:

#import "\\alpha\sdk\mso\office12\mso.dll" rename( "RGB", "MSORGB" ) rename("DocumentProperties", "MSODocumentProperties") rename("SearchPath", "MSOSearchPath")
using namespace Office;
#import "\\alpha\sdk\mso\office12\VBE6EXT.OLB" rename( "RGB", "MSORGB" ) rename("EOF", "EndOfFile")
using namespace VBIDE;
#import "\\alpha\sdk\mso\office12\excel.exe" rename( "DialogBox", "ExcelDialogBox" ) rename( "RGB", "ExcelRGB" ) rename( "CopyFile", "ExcelCopyFile" ) rename( "ReplaceText", "ExcelReplaceText" ) no_auto_exclude
#import "\\alpha\sdk\mso\office12\msword.olb" rename( "DialogBox", "WordDialogBox" ) rename( "RGB", "WordRGB" ) rename( "CopyFile", "WordCopyFile" ) rename( "ReplaceText", "WordReplaceText" ) rename( "ExitWindows", "WordExitWindows" ) rename( "FindText", "WordFindText" ) no_auto_exclude

// Create Excle application OLE obj...
Excel::_ApplicationPtr pApplication;
HRESULT hRes = pApplication.CreateInstance(_T("Excel.Application"));
if (hRes==S_OK) {
    pApplication->PutDisplayAlerts(MAKELCID(MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL), SORT_DEFAULT), VARIANT_FALSE);
    pApplication->PutCopyObjectsWithCells(MAKELCID(MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL), SORT_DEFAULT), VARIANT_TRUE);

    // Open prepeared for export HTML file
    Excel::_WorkbookPtr pBook = pApplication->Workbooks->Open( _bstr_t(szHTMLPath), _variant_t((long)Excel::xlUpdateLinksAlways), vtMissing, vtMissing/*_variant_t((long)Excel::xlHtml)*/ );
    if (pBook!=NULL) {
        //Save opened HTML file as XSL file
        hRes = pBook->SaveAs(_bstr_t(szXLSPath), _variant_t((long)Excel::xlWorkbookNormal), vtMissing, vtMissing, vtMissing, vtMissing, Excel::xlNoChange, _variant_t((long)Excel::xlLocalSessionChanges), _variant_t(false));
        if (hRes==S_OK) {
            // All is ok
        }
        pBook->Close();
    }
    pApplication->Quit();
}

Unfortunately, the img pictures from of the html code is not converted into an embedded image, but in the external linked images. How do I convert images to store them internally in the XLS file as an embedded object?

解决方案

Brief talk on a Microsoft forum allowed to form a more or less working code.

Excel::ShapesPtr pShapes = Excel::_WorksheetPtr(pBook->Worksheets->Item[1])->Shapes;
for (long nIndex=1; nIndex<=pShapes->GetCount(); nIndex++)
{
    Excel::ShapePtr pShape = pShapes->Item(nIndex);

    const Office::MsoShapeType nType = pShape->Type;
    switch (nType)
    {
    case Office::msoLinkedPicture:      
        {
            const float rLeft       = pShape->Left;
            const float rTop        = pShape->Top;
            const float rWidth      = pShape->Width;
            const float rHeight     = pShape->Height;
            const _bstr_t szPath    = pShape->AlternativeText;

            Excel::ShapePtr pNewShape = pShapes->AddPicture(szPath, Office::msoFalse, Office::msoTrue, rLeft, rTop, rWidth, rHeight);
            if (pNewShape)
            {
                pShape->Delete(); 
                nIndex = 0;
            }
        }
        break;
    }
}

这篇关于导出HTML文件与图像到Excel单XLS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆