寫一個doc文件
先寫一句話
畫一個2列5行的表格,第一列數據加粗
再寫一句話
再畫一個5列4行的表格,第一行數據加粗,第5列數據有顏色
參考
MFC下使用C++操作Word文檔
1.從MSWORD.OLB導入要使用的類
項目->類向導->添加類(向下的小三角)類型庫中的MFC類->文件->選擇MSWORD.OLB
MSWORD.OLB的位置與安裝office的版本和安裝位置有關
可以從這里參考查查自己電腦MSWORD.OLB文件的位置 https://www.mypcrun.com/file-info-page/msword-olb/
我電腦是office2016 位置是 C:\Program Files (x86)\Microsoft Office\root\Office16\MSWORD.OLB
選擇自己需要的類,導入到工程中
2.導入的所有的.h文件開頭第一句都有一行代碼,把這個注釋掉,就能編譯了
#import "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\MSWORD.OLB" no_namespace
3.include 這些.h文件,我這里用到這些
#include "CApplication.h"
#include "CDocument0.h"
#include "CDocuments.h"
#include "CBorders.h"
#include "CFont0.h"
#include "CParagraphs.h"
#include "CSelection.h"
#include "CTable0.h"
#include "CTables0.h"
#include "CCell.h"
寫一個例子
1 void CMFCApplication1Dlg::OnBnClickedOk() 2 { 3 // TODO: 在此添加控件通知處理程序代碼 4 //1.開啟word 5 CApplication word_app; 6 if (!word_app.CreateDispatch(_T("Word.Application"), NULL)) 7 //if (!word_app.CreateDispatch(_T("KWPS.Application"), NULL)) 8 { 9 AfxMessageBox(_T("本機沒有安裝word產品!")); 10 return; 11 } 12 word_app.put_Visible(FALSE);//設置word可見 13 14 //2.新建文檔 15 COleVariant vTrue((short)TRUE), vFalse((short)FALSE), VOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 16 CDocuments docs; 17 CDocument0 doc; 18 docs = word_app.get_Documents(); 19 doc = docs.Add(new CComVariant(_T("")), new CComVariant(false), new CComVariant(0), new CComVariant()); 20 21 //3.開始向word寫入數據 22 CSelection sel = word_app.get_Selection(); 23 24 CParagraphs wordParagraphs = sel.get_ParagraphFormat(); 25 wordParagraphs.put_Alignment(0);//0 左 1劇中 2右 26 27 CFont0 font = sel.get_Font(); 28 font.put_Bold(1); 29 font.put_Size(22); 30 font.put_Name(_T("宋體"));//設置字體屬性 31 sel.TypeText(TEXT("hello MSWORD.OLB\n\n")); 32 33 CTables0 tables = doc.get_Tables(); 34 CTable0 table1 = tables.Add(sel.get_Range(), 5, 2, new CComVariant(), new CComVariant());//創建5行2列的表格 35 CBorders borders1 = table1.get_Borders(); 36 borders1.put_InsideLineStyle(1); 37 borders1.put_OutsideLineStyle(1);//設置表格邊框,默認沒有邊框 38 39 //設置單元格寬度 40 CCell CellTemp; 41 for (int i = 0; i < 5; i++) 42 { 43 CellTemp = table1.Cell(i + 1, 1); 44 CellTemp.put_Width(100);//寬度 45 } 46 for (int i = 0; i < 5; i++) 47 { 48 CellTemp = table1.Cell(i + 1, 2); 49 CellTemp.put_Width(315); 50 } 51 //寫單元格內容 52 CellTemp = table1.Cell(1, 1); 53 CellTemp.Select(); 54 wordParagraphs.put_Alignment(1);//設置文字居中 55 CFont0 fontTemp = sel.get_Font(); 56 fontTemp.put_Bold(1); 57 fontTemp.put_Size(10); 58 sel.TypeText(_T("1111")); 59 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 60 wordParagraphs.put_Alignment(1);//設置文字居中 61 fontTemp.put_Bold(1); 62 fontTemp.put_Size(10); 63 sel.TypeText(_T("2222")); 64 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 65 wordParagraphs.put_Alignment(1);//設置文字居中 66 fontTemp.put_Bold(1); 67 fontTemp.put_Size(10); 68 sel.TypeText(_T("3333")); 69 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 70 wordParagraphs.put_Alignment(1);//設置文字居中 71 fontTemp.put_Bold(1); 72 fontTemp.put_Size(10); 73 sel.TypeText(_T("4444")); 74 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 75 wordParagraphs.put_Alignment(1);//設置文字居中 76 fontTemp.put_Bold(1); 77 fontTemp.put_Size(10); 78 sel.TypeText(_T("5555")); 79 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 80 81 CellTemp = table1.Cell(1, 2); 82 CellTemp.Select(); 83 wordParagraphs.put_Alignment(0);//設置文字居中 84 fontTemp = sel.get_Font(); 85 fontTemp.put_Bold(0); 86 fontTemp.put_Size(10); 87 sel.TypeText(_T("一一一")); 88 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 89 wordParagraphs.put_Alignment(0);//設置文字居中 90 fontTemp.put_Bold(0); 91 fontTemp.put_Size(10); 92 sel.TypeText(_T("二二二")); 93 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 94 wordParagraphs.put_Alignment(0);//設置文字居中 95 fontTemp.put_Bold(0); 96 fontTemp.put_Size(10); 97 sel.TypeText(_T("三三三")); 98 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 99 wordParagraphs.put_Alignment(0);//設置文字居中 100 fontTemp.put_Bold(0); 101 fontTemp.put_Size(10); 102 sel.TypeText(_T("一一\n二二\n三三")); 103 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 104 wordParagraphs.put_Alignment(0);//設置文字居中 105 fontTemp.put_Bold(0); 106 fontTemp.put_Size(10); 107 sel.TypeText(_T("五五五")); 108 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 109 110 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 111 fontTemp.put_Size(10); 112 sel.TypeText(TEXT("不知道寫啥就hello world吧\n")); 113 //sel.TypeParagraph(); // 新起一段 114 115 CTable0 table2 = tables.Add(sel.get_Range(), 4, 5, new CComVariant(), new CComVariant());//創建4行2列的表格 116 CBorders borders2 = table2.get_Borders(); 117 borders2.put_InsideLineStyle(1); 118 borders2.put_OutsideLineStyle(1);//設置表格邊框,默認沒有邊框 119 120 //設置單元格寬度 121 for (int i = 0; i < 4; i++) 122 { 123 for (int j = 0; j < 2; j++) 124 { 125 CellTemp = table2.Cell(i + 1, j + 1); 126 if (j == 0) 127 { 128 CellTemp.put_Width(140); 129 } 130 else if (j == 1) 131 { 132 CellTemp.put_Width(80); 133 } 134 else if (j == 2) 135 { 136 CellTemp.put_Width(70); 137 } 138 else if (j == 3) 139 { 140 CellTemp.put_Width(60); 141 } 142 else if (j == 4) 143 { 144 CellTemp.put_Width(60); 145 } 146 } 147 } 148 //第一行標題 149 CellTemp = table2.Cell(1, 1); 150 CellTemp.Select(); 151 CString strTitleTemp; 152 strTitleTemp = _T("AAA"); 153 wordParagraphs.put_Alignment(0);//設置文字居左 154 fontTemp.put_Bold(1); 155 fontTemp.put_Size(10); 156 sel.TypeText(strTitleTemp); 157 CellTemp = table2.Cell(1, 2); 158 CellTemp.Select(); 159 strTitleTemp = _T("BBB"); 160 sel.TypeText(strTitleTemp); 161 CellTemp = table2.Cell(1, 3); 162 CellTemp.Select(); 163 strTitleTemp = _T("CCC"); 164 sel.TypeText(strTitleTemp); 165 CellTemp = table2.Cell(1, 4); 166 CellTemp.Select(); 167 strTitleTemp = _T("DDD"); 168 sel.TypeText(strTitleTemp); 169 CellTemp = table2.Cell(1, 5); 170 CellTemp.Select(); 171 strTitleTemp = _T("EEE"); 172 sel.TypeText(strTitleTemp); 173 174 //第一列數據 175 CString strTemp; 176 CellTemp = table2.Cell(2, 1); 177 CellTemp.Select(); 178 for (int i = 0; i < 4 - 1; i++) 179 { 180 wordParagraphs.put_Alignment(0);//設置文字居左 181 fontTemp.put_Bold(0); 182 fontTemp.put_Size(10); 183 strTemp.Format(_T("第一列%d"), i+1); 184 sel.TypeText(strTemp); 185 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 186 } 187 //第二列數據 188 CellTemp = table2.Cell(2, 2); 189 CellTemp.Select(); 190 for (int i = 0; i < 4 - 1; i++) 191 { 192 wordParagraphs.put_Alignment(0);//設置文字居左 193 fontTemp.put_Bold(0); 194 fontTemp.put_Size(10); 195 strTemp.Format(_T("第二列%d"), i + 1); 196 sel.TypeText(strTemp); 197 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 198 } 199 //第三列數據 200 CellTemp = table2.Cell(2, 3); 201 CellTemp.Select(); 202 for (int i = 0; i < 4 - 1; i++) 203 { 204 wordParagraphs.put_Alignment(0);//設置文字居左 205 fontTemp.put_Bold(0); 206 fontTemp.put_Size(10); 207 strTemp.Format(_T("第三列%d"), i + 1); 208 sel.TypeText(strTemp); 209 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 210 } 211 //第四列數據 212 CellTemp = table2.Cell(2, 4); 213 CellTemp.Select(); 214 for (int i = 0; i < 4 - 1; i++) 215 { 216 wordParagraphs.put_Alignment(0);//設置文字居左 217 fontTemp.put_Bold(0); 218 fontTemp.put_Size(10); 219 strTemp.Format(_T("第四列%d"), i + 1); 220 sel.TypeText(strTemp); 221 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 222 } 223 //第五列數據 224 CellTemp = table2.Cell(2, 5); 225 CellTemp.Select(); 226 for (int i = 0; i < 4 - 1; i++) 227 { 228 wordParagraphs.put_Alignment(0);//設置文字居左 229 fontTemp.put_Bold(0); 230 fontTemp.put_Size(10); 231 strTemp.Format(_T("第五列%d"), i + 1); 232 if (i%2) 233 { 234 fontTemp.put_Color(65280); 235 } 236 else 237 { 238 fontTemp.put_Color(255); 239 } 240 sel.TypeText(strTemp); 241 sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0))); 242 } 243 244 //4.保存並退出 245 doc.SaveAs(COleVariant(_T("E:\\123.doc")), VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, 246 VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt); 247 doc.Close(vFalse, VOpt, VOpt); 248 word_app.Quit(VOpt, VOpt, VOpt); 249 }
寫完成的doc文件內容如圖