【问题标题】:export HTML files with images to Excel single XLS file?将带有图像的 HTML 文件导出到 Excel 单个 XLS 文件?
【发布时间】:2012-09-26 09:43:26
【问题描述】:

我正在尝试使用 C++ OLE 自动化将带有图片的 HTML 文件导出到 Excel。

代码示例:

#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();
}

很遗憾,html代码中的img图片并没有转换成内嵌图片,而是外链图片。如何转换图像以将它们作为嵌入对象存储在 XLS 文件内部?

【问题讨论】:

    标签: c++ excel automation ole


    【解决方案1】:

    简短的谈话on a Microsoft forum 允许形成或多或少的工作代码。

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2014-05-16
      • 1970-01-01
      相关资源
      最近更新 更多