admin 管理员组文章数量: 887021
VC
我们制作应用软件的时候,经常要把结果以报表的形式输出,当前使用较为广泛的当然是Excel我们要编程输出一个如下表格。
格式如下:
1
2
3
在BOOL CVCExcelApp::InitInstance()
if(CoInitialize(NULL)==0)//
{
AfxMessageBox("
exit(1);
}
在return FALSE;
为了能调用Excel
#include "VCExcel.h"
#include "comdef.h"
这样一来我们的程序就可以自由调用EXCEL
下面先在CVCExcelDlg
Range m_ExlRge;
_Worksheet m_ExlSheet;
Worksheets m_ExlSheets;
_Workbook m_ExlBook;
Workbooks m_ExlBooks;
_Application m_ExlApp;
我们利用加载Excel
void CVCExcelDlg::OnRunexcel()
{
// TODO: Add your control notification handler code here
//
if(!m_ExlApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("
return;
}
//
m_ExlApp.SetVisible(TRUE);
///
CString theAppPath,theAppName;
char Path[MAX_PATH];
GetModuleFileName(NULL,Path,MAX_PATH);//
theAppPath=(CString)Path;
theAppName=AfxGetApp()->m_pszAppName;
theAppName+=".exe";
//
int length1,length2;
length1=theAppPath.GetLength();
length2=theAppName.GetLength();
theAppPath.Delete(length1-length2,length2);
CString TempPath="";
TempPath=theAppPath+"Template.xls";//EXCEL
m_ExlBooks.AttachDispatch(m_ExlApp.GetWorkbooks(),TRUE);
m_ExlBook.AttachDispatch(m_ExlBooks.Add((_variant_t)TempPath),TRUE);//
m_ExlSheets.AttachDispatch(m_ExlBook.GetSheets(),TRUE);//
//
m_ExlSheets.Add(vtMissing,vtMissing,_variant_t((long)1),vtMissing);
//
m_ExlSheet.AttachDispatch(m_ExlSheets.GetItem(_variant_t((long)2)),TRUE);
m_ExlSheet.Delete();
//
m_ExlSheet.AttachDispatch(m_ExlSheets.GetItem(_variant_t((long)1)),TRUE);
m_ExlSheet.SetName("TestSheet");
///
//
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("D1")),TRUE);
m_ExlRge.Merge(_variant_t((long)0));
m_ExlRge.AttachDispatch(m_ExlSheet.GetCells(),TRUE);//
m_ExlRge.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("
m_ExlRge.SetItem(_variant_t((long)2),_variant_t((long)1),_variant_t("
m_ExlRge.SetItem(_variant_t((long)2),_variant_t((long)2),_variant_t("
m_ExlRge.SetItem(_variant_t((long)2),_variant_t((long)3),_variant_t("
m_ExlRge.SetItem(_variant_t((long)2),_variant_t((long)4),_variant_t("
m_ExlRge.SetItem(_variant_t((long)3),_variant_t((long)1),_variant_t("
m_ExlRge.SetItem(_variant_t((long)3),_variant_t((long)2),_variant_t("60"));
m_ExlRge.SetItem(_variant_t((long)3),_variant_t((long)3),_variant_t("
m_ExlRge.SetItem(_variant_t((long)3),_variant_t((long)4),_variant_t("
m_ExlRge.SetItem(_variant_t((long)4),_variant_t((long)1),_variant_t("
m_ExlRge.SetItem(_variant_t((long)4),_variant_t((long)2),_variant_t("40"));
m_ExlRge.SetItem(_variant_t((long)4),_variant_t((long)3),_variant_t("
m_ExlRge.SetItem(_variant_t((long)4),_variant_t((long)4),_variant_t("
m_ExlRge.SetItem(_variant_t((long)5),_variant_t((long)1),_variant_t("
m_ExlRge.SetItem(_variant_t((long)5),_variant_t((long)2),_variant_t("60"));
m_ExlRge.SetItem(_variant_t((long)5),_variant_t((long)3),_variant_t("
m_ExlRge.SetItem(_variant_t((long)5),_variant_t((long)4),_variant_t("
m_ExlRge.AttachDispatch(m_ExlSheet.GetUsedRange());//
m_ExlRge.SetWrapText(_variant_t((long)1));//
//
//
//
m_ExlRge.SetHorizontalAlignment(_variant_t((long)-4108));
m_ExlRge.SetVerticalAlignment(_variant_t((long)-4108));
///
Font ft;
ft.AttachDispatch(m_ExlRge.GetFont());
ft.SetName(_variant_t("
ft.SetColorIndex(_variant_t((long)11));//
ft.SetSize(_variant_t((long)12));//
///
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("D1")));
ft.AttachDispatch(m_ExlRge.GetFont());
ft.SetBold(_variant_t((long)1));//
ft.SetSize(_variant_t((long)13));
ft.SetColorIndex(_variant_t((long)2));
CellFormat cf;
cf.AttachDispatch(m_ExlRge.GetCells());
//
Interior it;
it.AttachDispatch(m_ExlRge.GetInterior());
it.SetColorIndex(_variant_t((long)11));//
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A2"),_variant_t("D5")));
it.AttachDispatch(m_ExlRge.GetInterior());
it.SetColorIndex(_variant_t((long)15));
//
Range UnitRge;
CString CellName;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
CellName.Format("%c%d",j+64,i);//
UnitRge.AttachDispatch(m_ExlRge.GetRange(_variant_t(CellName),_variant_t(CellName)));//
//LineStyle=
UnitRge.BorderAround(_variant_t((long)1),_variant_t((long)2),_variant_t((long)-4105),vtMissing);//
}
}
//
m_ExlRge.ReleaseDispatch();
m_ExlSheet.ReleaseDispatch();
m_ExlSheets.ReleaseDispatch();
m_ExlBook.ReleaseDispatch();
m_ExlBooks.ReleaseDispatch();
//m_ExlApp
m_ExlApp.ReleaseDispatch();
//
}
小结:一年前就想写这个东西了,因为网上讲VC
本文标签: VC
版权声明:本文标题:VC 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1698703034h314761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论