如何设置excel中的页边距?

特招 2004-10-28 11:15:35
AnsiString fileName = PubPrintForm->edtSaveDir->Text;
if (FileExists(fileName))
{
if (MessageDlg("文件已经存在,是否删除?",mtWarning,TMsgDlgButtons()<<mbOK<<mbCancel,0)==mrOk)
{
DeleteFile(fileName);
}
else
{
ShowMessage("放弃操作");
return;
}
}
//导出到Excel开始
Variant excelApp,workBook,aft,sheetA,range,bef;
//检查机器是否安装了Office
try{
excelApp = CreateOleObject("Excel.Application"); //打开excel应用程序
}
catch(...)
{
ShowMessage("运行Excel出错,请检查本机是否安装了Office!");
return;
}
excelApp.OPS("Visible",true); //使应用程序可见
//建立一个新的excel文件
workBook = excelApp.OPG("WorkBooks").OFN("Add");
//workBook.Exec(PR("Add"));
workBook = excelApp.OPG("ActiveWorkBook");//打开工作簿
//显示存在的sheet的数目
int count = workBook.OPG("sheets").OPG("count");
aft = workBook.OPG("sheets",count);
workBook.OPG("sheets").OPR("Add",bef.NoParam(),aft);
sheetA = workBook.OPG("ActiveSheet");
sheetA.OPS("name","表一");
//设置表一为横向页面打印 横向用"xlLandscape",纵向用"xlPortrait"
sheetA.OPG("PageSetup").OPS("Orientation",2);
/*************************如何设置页边距*************************/

//关闭警告提示
workBook.OPG("Application").OPS("DisplayAlerts",false);
//另存为
workBook.OPR("SaveAs",fileName.c_str());
//关闭工作簿
workBook.OPR("Close");
//关闭excel程序
excelApp.OFN("Quit");
//结束,如果没有如下代码,Excel线程直到应用程序退出才结束
excelApp=Unassigned;
workBook=Unassigned;
aft=Unassigned;
sheetA = Unassigned;
range = Unassigned;
ShowMessage(title+"导出成功!");
...全文
1962 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
特招 2004-10-29
  • 打赏
  • 举报
回复
我试验过了,一般在Excel中设置的相应边距值,需要乘以28.5
我试验的OFFICE版本是XP,BCB6
// 在Excel中设置相应的边距值,在BCB中写入时乘以28.5
sheetA.OPG("PageSetup").OPS("LeftMargin", 3 * 28.5); // 左边距3
sheetA.OPG("PageSetup").OPS("RightMargin", 4 * 28.5); // 右边距4
sheetA.OPG("PageSetup").OPS("TopMargin", 1 * 28.5); // 上边距1
sheetA.OPG("PageSetup").OPS("BottomMargin", 2 * 28.5); // 下边距2
sheetA.OPG("PageSetup").OPS("HeaderMargin", 1.1 * 28.5); // 页眉1.1
sheetA.OPG("PageSetup").OPS("FooterMargin", 1.2 * 28.5); // 页脚1.2

多谢妖哥
特招 2004-10-28
  • 打赏
  • 举报
回复
自己up
hailin10 2004-10-28
  • 打赏
  • 举报
回复
学习
milkwayhong 2004-10-28
  • 打赏
  • 举报
回复
up
ccrun.com 2004-10-28
  • 打赏
  • 举报
回复
录了一段宏,改成BCB的。
没有太多时间,所以没有测试,自个试试吧。

sheetA.OPG("PageSetup").OPS("LeftHeader","");
sheetA.OPG("PageSetup").OPS("CenterHeader","");
sheetA.OPG("PageSetup").OPS("RightHeader","");
sheetA.OPG("PageSetup").OPS("LeftFooter","");
sheetA.OPG("PageSetup").OPS("CenterFooter","");
sheetA.OPG("PageSetup").OPS("RightFooter","");

// 在Excel中设置的相应边距值,在BCB中写入时乘以0.39
sheetA.OPG("PageSetup").OPS("LeftMargin", 3 * 0.39); // 左边距3
sheetA.OPG("PageSetup").OPS("RightMargin", 4 * 0.39); // 右边距4
sheetA.OPG("PageSetup").OPS("TopMargin", 1 * 0.39); // 上边距1
sheetA.OPG("PageSetup").OPS("BottomMargin", 2 * 0.39); // 下边距2
sheetA.OPG("PageSetup").OPS("HeaderMargin", 1.1 * 0.39); // 页眉1.1
sheetA.OPG("PageSetup").OPS("FooterMargin", 1.2 * 0.39); // 页脚1.2

sheetA.OPG("PageSetup").OPS("PrintHeadings", false);
sheetA.OPG("PageSetup").OPS("PrintGridlines", false);
sheetA.OPG("PageSetup").OPS("PrintComments", -4142); //xlPrintNoComments
sheetA.OPG("PageSetup").OPS("PrintQuality", 600);

sheetA.OPG("PageSetup").OPS("CenterHorizontally", true);
sheetA.OPG("PageSetup").OPS("CenterVertically", false);
sheetA.OPG("PageSetup").OPS("Orientation", 1); //xlPortrait
sheetA.OPG("PageSetup").OPS("Draft", false);

sheetA.OPG("PageSetup").OPS("PaperSize", 9); // xlPaperA4
sheetA.OPG("PageSetup").OPS("FirstPageNumber", -4105); //xlAutomatic
sheetA.OPG("PageSetup").OPS("Order", 1); //xlDownThenOver
sheetA.OPG("PageSetup").OPS("BlackAndWhite", false);
sheetA.OPG("PageSetup").OPS("Zoom", 100);
sheetA.OPG("PageSetup").OPS("PrintErrors", 0); // xlPrintErrorsDisplayed
特招 2004-10-28
  • 打赏
  • 举报
回复
高手帮忙解决啊

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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