楼主会用自定义函数吗?会的话可以参考下面的步骤哦:
按alt+F11,点击插入-模块,将下方代码贴入窗口中
Function Getformula(rng As Range, Optional flag As Boolean = False)If flag Then
Getformula = rng.Formula
Else
mystr = rng.Formula
With CreateObject("vbscript.regexp")
.Pattern = "[\w$]+\d+"
.Global = True
.MultiLine = False
.ignorecase = True
Set mymh = .Execute(mystr)
For Each mhi In mymh
.Pattern = "([^$A-Z]|^)" & Application.WorksheetFunction.Substitute(mhi, "$", "\$") & "(?=[^0-9]|$)"
mystr = .Replace(mystr, "$1" & IIf(Range(mhi).Value = "", 0, Range(mhi).Value))
Next
End With
Getformula = mystr
End If
End Function
然后回到表格中,假设你公式在A1单元格
在B1单元格输入如下公式:
=Getformula(A1,0)
应该就可以得到你要的结果
//Excel文档必须要有内容才可以预览//打印功能是直接打印要进行更多的参数设置参照VBA帮助
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Variants.hpp>
#include <Comobj.hpp>
#include <Utilcls.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1
Variant excel_app,excel_book,excel_sheet,my_worksheet
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
excel_app=Variant::CreateObject("excel.application")//创建app对象
excel_book=excel_app.OlePropertyGet("workbooks")//创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1)
excel_sheet=excel_book.OleFunction("add") //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet")
excel_app.OlePropertySet("Visible",(Variant)true) //可见
}
catch (Exception &exception)
{
Application->MessageBox("调用EXCEL失败!可能你的电脑里没有EXCEL!!","提示信息",MB_OK)
return
}
//声明OLE调用
PropertyGet Range("Range")
Variant my_range
PropertySet Setvalue("value")
PropertySet SetFormula("Formula")
PropertyGet Getvalue("value")
PropertyGet GetFormula("Formula")
Function SaveAs("SaveAs")
AnsiString mytmp
int total=0
for (int i=1i<5++i)
{
Application->ProcessMessages()
for(int j=1j<5++j)
{
Range.ClearArgs()
Setvalue.ClearArgs()
mytmp=String(char(64+i))+IntToStr(j)
Range.ClearArgs()
Setvalue.ClearArgs()
my_range = my_worksheet.Exec(Range << mytmp )
my_range.Exec(Setvalue << "Hello!")
Range.ClearArgs()
Application->ProcessMessages()
}
}
try
{
excel_app.OlePropertyGet("ActiveWorkBook").OlePropertyGet("WorkSheets",1).OleFunction("Activate") //激活工作表
}
catch (Exception &exception)
{
MessageBox(Handle,"保存失败,请检查!!","错误",MB_OK)
return
}
excel_app.OlePropertyGet("ActiveWindow").OlePropertyGet("SelectedSheets").OleFunction("PrintPreview")//打印预览
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
excel_app.OlePropertyGet("ActiveWorkBook").OleFunction("Close")
excel_app.OleFunction("Quit")
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
excel_app.OlePropertyGet("ActiveWindow").OlePropertyGet("SelectedSheets").OleFunction("PrintOut")//打印
}
//---------------------------------------------------------------------------
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)