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