VC控制Excel

 

关于Excel的操作,代码来源自网络。

 1 None.gif void  CControlExcelDoc::OnControlExcel() 
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    // TODO: Add your command handler code here
 4InBlock.gif    COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
 5InBlock.gif        VT_ERROR),vtTrue((short)TRUE),vtFalse((short)FALSE);
 6InBlock.gif    _Application *ExcelApp = new _Application;
 7InBlock.gif
 8InBlock.gif    ExcelApp->CreateDispatch("excel.application");
 9InBlock.gif
10InBlock.gif    //设置为显示
11InBlock.gif    ExcelApp->SetVisible(TRUE);
12InBlock.gif    //得到WorkBooks
13InBlock.gif    Workbooks  ExcelBook=ExcelApp->GetWorkbooks();
14InBlock.gif    Sheets     ExcelSheet=ExcelBook.Add(vtOptional);
15InBlock.gif    _Workbook  workBook;
16InBlock.gif    _Worksheet workSheet;
17InBlock.gif    Range    range;
18InBlock.gif    workBook.AttachDispatch(ExcelApp->GetApplication());
19InBlock.gif    ExcelSheet=workBook.GetSheets();
20InBlock.gif
21InBlock.gif    
22InBlock.gif    workSheet=ExcelSheet.GetItem(COleVariant((short)1));
23InBlock.gif    workSheet.Activate();
24InBlock.gif
25InBlock.gif
26InBlock.gif    Range myallrange=workSheet.GetRange(COleVariant("A1"),COleVariant("D1"));
27InBlock.gif    //myallrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
28InBlock.gif    myallrange.SetHorizontalAlignment(COleVariant((short)3));
29InBlock.gif    myallrange.Merge(COleVariant((short)1));
30InBlock.gif    myallrange.SetValue(COleVariant("个人信息表"));
31InBlock.gif
32InBlock.gif    Range myrange=workSheet.GetRange(COleVariant("A2"),COleVariant("A2"));
33InBlock.gif    myrange.SetValue(COleVariant("姓名"));
34InBlock.gif    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
35InBlock.gif    myrange.SetHorizontalAlignment(COleVariant((short)3));
36InBlock.gif    myrange.SetVerticalAlignment(COleVariant((short)2));
37InBlock.gif    myrange.SetColumnWidth(COleVariant((short)15));
38InBlock.gif
39InBlock.gif    myrange=workSheet.GetRange(COleVariant("B2"),COleVariant("B2"));
40InBlock.gif    myrange.SetValue(COleVariant("性别"));
41InBlock.gif    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
42InBlock.gif    myrange.SetHorizontalAlignment(COleVariant((short)3));
43InBlock.gif    myrange.SetVerticalAlignment(COleVariant((short)2));
44InBlock.gif    myrange.SetColumnWidth(COleVariant((short)15));
45InBlock.gif
46InBlock.gif    myrange=workSheet.GetRange(COleVariant("C2"),COleVariant("C2"));
47InBlock.gif    myrange.SetValue(COleVariant("QQ"));
48InBlock.gif    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
49InBlock.gif    myrange.SetHorizontalAlignment(COleVariant((short)3));
50InBlock.gif    myrange.SetVerticalAlignment(COleVariant((short)2));
51InBlock.gif    myrange.SetColumnWidth(COleVariant((short)15));
52InBlock.gif
53InBlock.gif    myrange=workSheet.GetRange(COleVariant("D2"),COleVariant("D2"));
54InBlock.gif    myrange.SetValue(COleVariant("Email"));
55InBlock.gif    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
56InBlock.gif    myrange.SetHorizontalAlignment(COleVariant((short)3));
57InBlock.gif    myrange.SetVerticalAlignment(COleVariant((short)2));
58InBlock.gif    myrange.SetColumnWidth(COleVariant((short)17));
59InBlock.gif
60InBlock.gif    CString str;
61InBlock.gif    for(int i=3;i<=500;i++)
62ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
63InBlock.gif        str.Format("A%d",i);
64InBlock.gif        Range myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
65InBlock.gif        myrange.SetValue(COleVariant("名字"));
66InBlock.gif        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
67InBlock.gif        myrange.SetHorizontalAlignment(COleVariant((short)3));
68InBlock.gif        myrange.SetVerticalAlignment(COleVariant((short)2));
69InBlock.gif
70InBlock.gif        str.Format("B%d",i);
71InBlock.gif        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
72InBlock.gif        myrange.SetValue(COleVariant(""));
73InBlock.gif        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
74InBlock.gif        myrange.SetHorizontalAlignment(COleVariant((short)3));
75InBlock.gif        myrange.SetVerticalAlignment(COleVariant((short)2));
76InBlock.gif
77InBlock.gif        str.Format("C%d",i);
78InBlock.gif        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
79InBlock.gif        myrange.SetValue(COleVariant("12345678"));
80InBlock.gif        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
81InBlock.gif        myrange.SetHorizontalAlignment(COleVariant((short)3));
82InBlock.gif        myrange.SetVerticalAlignment(COleVariant((short)2));
83InBlock.gif
84InBlock.gif        str.Format("D%d",i);
85InBlock.gif        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
86InBlock.gif        myrange.SetValue(COleVariant("superxsc@126.com"));
87InBlock.gif        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
88InBlock.gif        myrange.SetHorizontalAlignment(COleVariant((short)3));
89InBlock.gif        myrange.SetVerticalAlignment(COleVariant((short)2));
90ExpandedSubBlockEnd.gif    }

91InBlock.gif
92InBlock.gif    delete ExcelApp;
93InBlock.gif
94ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/hone/archive/2007/01/12/618297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值