关于C++调用libxl读写打印Excel表格的问题

你的OPPA 2017-12-12 02:23:31
C++调用libxl库,已经实现了基本的读写excel操作,现在有两个问题:
1.怎么获取单元格中的自动换行属性并且设置自动换行,查了官网上的接口没找到;
2.C++调用libxl打开一张Excel表格,编辑打印设置的属性(比如:横向纵向打印、打印纸为A4纸、是否缩放打印等等);编辑好后保存关闭,然后C++怎么去获取这些信息,然后调用打印机按照这些属性进行打印
...全文
1273 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑色的夢 2021-01-08
  • 打赏
  • 举报
回复
刚遇到了问题1,在libxl的官网找了找,找到了答案。 链接:https://www.libxl.com/faq.html How to add multiple lines of text to a cell ? You should use a line-feed characters \n in a string and add a wrap attribute to the cell's format:
  
  Format* format = book->addFormat();
  format->setWrap(true);

  sheet->writeStr(1, 1, L"line one\nline two\nline three", format);
zgl7903 2017-12-20
  • 打赏
  • 举报
回复
可以打开EXCEL,手动录制宏, 然后参考宏代码修改 页面属性搜 PageSetupPtr

    PageSetupPtr pPageSet = pSheet->PageSetup;
    pPageSet->PutPaperSize( xlPaperA4 ); //A4
    pPageSet->CenterHorizontally = VARIANT_TRUE;  //水平居中
    pPageSet->PutOrientation( xlLandscape ); //横向打印
    pPageSet->PrintArea = "$A$1:$J$22"; //打印区域
    
你的OPPA 2017-12-19
  • 打赏
  • 举报
回复
引用 3 楼 zgl7903 的回复:
import COM 挺方便的, 录制宏,参考VBA修改挺容易的

// Excel12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#import "C:/Program Files/Microsoft Office/OFFICE12/mso.dll" \
  rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
  rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
#import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT.OLB" no_namespace
#import "C:/Program Files/Microsoft Office/OFFICE12/excel.exe" \
  rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
  rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \
  rename("IFont", "IFontXL")

void dump_com_error(_com_error &e)
{
  _tprintf(_T("Oops - hit an error!\n"));
  _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  _bstr_t bstrSource(e.Source());
  _bstr_t bstrDescription(e.Description());
  _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
struct StartOle {
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

int main(int argc, char* argv[])
{ 
  using namespace Office;
  using namespace Excel;
  
  _ApplicationPtr pXL;
  
  try 
  {
    pXL.CreateInstance(L"Excel.Application");
    
    pXL->PutVisible(0, VARIANT_TRUE);
    
    WorkbooksPtr pBooks = pXL->Workbooks;
    _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
    
    _WorksheetPtr pSheet = pXL->ActiveSheet;
    
    try
    {
      // This one will fail
      pSheet->Name = "Market Share?";
    }
    catch (_com_error &e) 
    {
      dump_com_error(e);
    }
    
    pSheet->Name = "Market Share!";

    Excel::ShapesPtr pShapes = pSheet->Shapes;
    RangePtr range = pSheet->Range["H1:O10"];
    float l = range->Left, t = range->Top;
    float w = range->Width, h = range->Height;
    Excel::ShapePtr pShape = pShapes->AddPicture( "C:\\tt.jpg", 
      msoFalse, msoTrue, l, t, w, h);
        
    pSheet->Range["A2"]->Value2 = "Company A";
    pSheet->Range["B2"]->Value2 = "Company B";
    pSheet->Range["C2"]->Value2 = "Company C";
    pSheet->Range["D2"]->Value2 = "Company D";
    
    pSheet->Range["A3"]->Value2 = 75.0;
    pSheet->Range["B3"]->Value2 = 14.0;
    pSheet->Range["C3"]->Value2 = 7.0;
    pSheet->Range["D3"]->Value2 = 4.0;

    pSheet->Range["A4"]->Value2 = 75.0;
    pSheet->Range["B4"]->Value2 = 14.0;
    pSheet->Range["C4"]->Value2 = 7.0;
    pSheet->Range["D4"]->Value2 = 4.0;
    
    Sleep(6000);
    
    pBook->PutSaved(0, VARIANT_TRUE);
    pXL->Quit();
  } 
  catch(_com_error &e) 
  {
    dump_com_error(e);
  }

  return 0;
}


请问用COM组件怎么获取Excel表格中打印页面的属性,就是比如设置的打印纸为A4纸,纵向还是横向这些属性
zgl7903 2017-12-14
  • 打赏
  • 举报
回复
import COM 挺方便的, 录制宏,参考VBA修改挺容易的

// Excel12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#import "C:/Program Files/Microsoft Office/OFFICE12/mso.dll" \
  rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
  rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
#import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT.OLB" no_namespace
#import "C:/Program Files/Microsoft Office/OFFICE12/excel.exe" \
  rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
  rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \
  rename("IFont", "IFontXL")

void dump_com_error(_com_error &e)
{
  _tprintf(_T("Oops - hit an error!\n"));
  _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  _bstr_t bstrSource(e.Source());
  _bstr_t bstrDescription(e.Description());
  _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
struct StartOle {
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

int main(int argc, char* argv[])
{ 
  using namespace Office;
  using namespace Excel;
  
  _ApplicationPtr pXL;
  
  try 
  {
    pXL.CreateInstance(L"Excel.Application");
    
    pXL->PutVisible(0, VARIANT_TRUE);
    
    WorkbooksPtr pBooks = pXL->Workbooks;
    _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
    
    _WorksheetPtr pSheet = pXL->ActiveSheet;
    
    try
    {
      // This one will fail
      pSheet->Name = "Market Share?";
    }
    catch (_com_error &e) 
    {
      dump_com_error(e);
    }
    
    pSheet->Name = "Market Share!";

    Excel::ShapesPtr pShapes = pSheet->Shapes;
    RangePtr range = pSheet->Range["H1:O10"];
    float l = range->Left, t = range->Top;
    float w = range->Width, h = range->Height;
    Excel::ShapePtr pShape = pShapes->AddPicture( "C:\\tt.jpg", 
      msoFalse, msoTrue, l, t, w, h);
        
    pSheet->Range["A2"]->Value2 = "Company A";
    pSheet->Range["B2"]->Value2 = "Company B";
    pSheet->Range["C2"]->Value2 = "Company C";
    pSheet->Range["D2"]->Value2 = "Company D";
    
    pSheet->Range["A3"]->Value2 = 75.0;
    pSheet->Range["B3"]->Value2 = 14.0;
    pSheet->Range["C3"]->Value2 = 7.0;
    pSheet->Range["D3"]->Value2 = 4.0;

    pSheet->Range["A4"]->Value2 = 75.0;
    pSheet->Range["B4"]->Value2 = 14.0;
    pSheet->Range["C4"]->Value2 = 7.0;
    pSheet->Range["D4"]->Value2 = 4.0;
    
    Sleep(6000);
    
    pBook->PutSaved(0, VARIANT_TRUE);
    pXL->Quit();
  } 
  catch(_com_error &e) 
  {
    dump_com_error(e);
  }

  return 0;
}


Uncle_higo 2017-12-14
  • 打赏
  • 举报
回复
c++的库貌似操作excel貌似真的功能有限吧
汪宁宇 2017-12-12
  • 打赏
  • 举报
回复
利用模板文件填充表格吧,LIBX不带打印的,需要调ShellExecute调用外部表格软件

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧