MyExcel.cpp
上传用户:jzscgs158
上传日期:2022-05-25
资源大小:8709k
文件大小:14k
源码类别:

百货/超市行业

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyExcel.h"
  3. CMyExcel::CMyExcel()
  4. {
  5. strFilePath=_T("");
  6. }
  7. CMyExcel::~CMyExcel()
  8. {
  9. COleVariant covFalse((short)FALSE);
  10. COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); 
  11. MyRange.ReleaseDispatch();
  12. MySheet.ReleaseDispatch();
  13. MySheets.ReleaseDispatch();
  14. MyBook.Close(covFalse,_variant_t(strFilePath),covOptional);
  15. MyBook.ReleaseDispatch();
  16. MyBooks.Close();
  17. MyBooks.ReleaseDispatch();
  18. MyApp.Quit();
  19. MyApp.ReleaseDispatch();
  20. CoUninitialize();
  21. }
  22. BOOL CMyExcel::Open()
  23. {
  24. LPDISPATCH lpDisp=NULL;
  25. COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);   
  26. CoInitialize(NULL);
  27. if (!MyApp.CreateDispatch("Excel.Application",NULL))
  28. {
  29. AfxMessageBox(_T("EXCEL初始化时出错!"),MB_OK|MB_ICONERROR);
  30. return FALSE;
  31. }
  32. lpDisp=MyApp.GetWorkbooks();
  33. MyBooks.AttachDispatch(lpDisp,TRUE);
  34. lpDisp = MyBooks.Add(covOptional); 
  35. MyBook.AttachDispatch(lpDisp,TRUE);
  36. lpDisp=MyBook.GetWorksheets();
  37. MySheets.AttachDispatch(lpDisp,TRUE);
  38. return TRUE;
  39. }
  40. BOOL CMyExcel::Open(CString strFile)
  41. {
  42. LPDISPATCH   lpDisp=NULL;
  43. CoInitialize(NULL);
  44. if (!MyApp.CreateDispatch("Excel.Application",NULL))
  45. {
  46. AfxMessageBox(_T("EXCEL初始化时出错!"),MB_OK|MB_ICONERROR);
  47. return FALSE;
  48. }
  49. lpDisp=MyApp.GetWorkbooks();
  50. MyBooks.AttachDispatch(lpDisp,TRUE);
  51. lpDisp = MyBooks.Open(strFile, vtMissing, vtMissing, vtMissing, 
  52. vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,vtMissing);
  53. MyBook.AttachDispatch(lpDisp,TRUE);
  54. lpDisp=MyBook.GetWorksheets(); 
  55. MySheets.AttachDispatch(lpDisp,TRUE);
  56. strFilePath=strFile;
  57. return TRUE;
  58. }
  59. BOOL CMyExcel::OpenSheet(CString strSheet)
  60. {
  61. LPDISPATCH  lpDisp=NULL;
  62. long len;
  63. len=MySheets.GetCount();
  64. for(long i=1;i<=len;i++)
  65. {
  66. lpDisp=MySheets.GetItem((_variant_t)(long)i);
  67. MySheet.AttachDispatch(lpDisp,TRUE);
  68. CString str=MySheet.GetName();
  69. if(MySheet.GetName()==strSheet)
  70. {
  71. lpDisp=MySheet.GetCells();
  72. MyRange.AttachDispatch(lpDisp,TRUE);
  73. return TRUE;
  74. }
  75. }
  76. return FALSE;
  77. }
  78. BOOL CMyExcel::SetItemText(long Row,long Col,CString strText)
  79. {
  80. long lRow=0,lCol=0;
  81. lRow=GetRowS();
  82. lCol=GetColS();
  83. if(Row>lRow ||Col>lCol)
  84. {
  85. CString strText;
  86. strText.Format("由于(%d,%d)已经超过了(%d,%d)的范围,所以在(%d,%d)设置失败!"
  87. ,Row,Col,lRow,lCol,Row,Col);
  88. AfxMessageBox(_T(strText),MB_OK|MB_ICONERROR);
  89. return FALSE;
  90. }
  91. MyRange.SetItem(_variant_t(Row), _variant_t(Col), _variant_t(strText));
  92. return TRUE;
  93. }
  94. CString CMyExcel::GetItemText(long Row,long Col)
  95. {
  96. CString strValue=_T("");
  97. long lRow=0,lCol=0;
  98. lRow=GetRowS();
  99. lCol=GetColS();
  100. if(Row>lRow ||Col>lCol)
  101. {
  102. CString strText;
  103. strText.Format("由于(%d,%d)已经超过了(%d,%d)的范围,所以返回空值"
  104. ,Row,Col,lRow,lCol,Row,Col);
  105. AfxMessageBox(_T(strText),MB_OK|MB_ICONERROR);
  106. return strValue;
  107. }
  108. VARIANT lpDisp=MyRange.GetItem(_variant_t(Row), _variant_t(Col));
  109. Range rgRgeValue;
  110. rgRgeValue.AttachDispatch(lpDisp.pdispVal, TRUE);
  111. _variant_t vtVal = rgRgeValue.GetValue();
  112. if (vtVal.vt == VT_EMPTY)
  113. {
  114. rgRgeValue.ReleaseDispatch();
  115. strValue = _T("");
  116. rgRgeValue.ReleaseDispatch();
  117. return strValue;
  118. }
  119. vtVal.ChangeType(VT_BSTR);
  120. strValue= vtVal.bstrVal;
  121. rgRgeValue.ReleaseDispatch();
  122. return strValue;
  123. }
  124. void CMyExcel::SaveAs(CString strPath)
  125. {
  126. if(IsFileExist(strPath,FALSE)==TRUE)
  127. DeleteFile(strPath);
  128. MyBook.SaveAs(_variant_t(strPath),vtMissing,vtMissing,vtMissing,vtMissing,vtMissing
  129. ,0,vtMissing,vtMissing,vtMissing,vtMissing);
  130. strFilePath=strPath;
  131. // AfxMessageBox(_T("Excel保存成功"),MB_OK|MB_ICONINFORMATION);
  132. }
  133. void CMyExcel::Save()
  134. {
  135. MyBook.Save();
  136. // AfxMessageBox(_T("Excel保存成功"),MB_OK|MB_ICONINFORMATION);
  137. }
  138. void CMyExcel::AddSheet(CString strSheet)
  139. {
  140. LPDISPATCH  lpDisp=NULL;
  141. lpDisp=MySheets.Add(vtMissing,vtMissing,vtMissing,vtMissing);
  142. MySheet.AttachDispatch(lpDisp,TRUE);
  143. MySheet.SetName(strSheet);
  144. lpDisp=MySheet.GetCells();
  145. MyRange.AttachDispatch(lpDisp,TRUE);
  146. }
  147. void CMyExcel::GetRange(CString strBegin,CString strEnd)
  148. {
  149. MyRange=MySheet.GetRange(_variant_t(strBegin),_variant_t(strEnd));
  150. }
  151. void CMyExcel::AutoColFit()
  152. {
  153. Range rg=MyRange.GetEntireColumn();
  154. rg.AutoFit();
  155. rg.ReleaseDispatch();
  156. }
  157. void CMyExcel::AutoRowFit()
  158. {
  159. Range rg=MyRange.GetEntireRow();
  160. rg.AutoFit();
  161. rg.ReleaseDispatch();
  162. }
  163. void CMyExcel::SetWrapText(BOOL blnTrue)
  164. {
  165. MyRange.SetWrapText((_variant_t)(short)blnTrue);
  166. }
  167. void CMyExcel::SetVisible(BOOL blnVisible)
  168. {
  169. if(blnVisible==TRUE)
  170. if(strFilePath!="")
  171. {
  172. if(IsFileExist(strFilePath,FALSE))
  173. {
  174. Exit();
  175. ShellExecute(NULL,"open",strFilePath,NULL,NULL,SW_SHOW);
  176. }
  177. else
  178. {
  179. CString strName;
  180. strName="路径"+strFilePath+"错误,不能打开显示!";
  181. AfxMessageBox(strFilePath,MB_OK|MB_ICONINFORMATION);
  182. }
  183. }
  184. else
  185. {
  186. AfxMessageBox("请先存文件后,方可打开!",MB_OK|MB_ICONINFORMATION);
  187. }
  188. }
  189. void CMyExcel::SetFont(MyFont font)
  190. {
  191. Font f=MyRange.GetFont();
  192. f.SetName(_variant_t(font.Name));
  193. f.SetShadow((_variant_t)(short)font.Shadow);
  194. f.SetSize((_variant_t)(short)font.size);
  195. f.SetUnderline((_variant_t)(short)font.Underline);
  196. f.SetBold((_variant_t)(short)font.Bold);
  197. f.SetColor((_variant_t)(long)font.ForeColor);
  198. f.SetItalic((_variant_t)(short)font.Italic);
  199. f.SetStrikethrough((_variant_t)(short)font.Strikethrough);
  200. f.SetSubscript((_variant_t)(short)font.Subscript);
  201. f.SetSuperscript((_variant_t)(short)font.Subscript);
  202. f.ReleaseDispatch();
  203. }
  204. void CMyExcel::SetAlignment(MyAlignment XMyAlignment)
  205. {
  206. MyRange.SetHorizontalAlignment((_variant_t)(short)XMyAlignment.HorizontalAlignment);
  207. MyRange.SetVerticalAlignment((_variant_t)(short)XMyAlignment.VerticalAlignment);
  208. }
  209. void CMyExcel::AutoRange()
  210. {
  211. LPDISPATCH  lpDisp=NULL;
  212. lpDisp=MySheet.GetCells();
  213. MyRange.AttachDispatch(lpDisp,TRUE);
  214. }
  215. void CMyExcel::SetMergeCells(BOOL blnTrue)
  216. {
  217. if(blnTrue==TRUE)
  218. {
  219. int i,j;
  220. long Row=GetRowS();
  221. long Col=GetColS();
  222. for(j=2;j<=Col;j++) SetItemText(1,j,"");
  223. for(i=2;i<=Row;i++)
  224. for(j=1;j<=Col;j++)
  225. SetItemText(i,j,"");
  226. }
  227. MyRange.SetMergeCells((_variant_t)(short)blnTrue);
  228. }
  229. void CMyExcel::SetBackStyle(MyBackStyle BackStyle)
  230. {
  231. LPDISPATCH  lpDisp=NULL;
  232. Interior Itor;
  233. lpDisp=MyRange.GetInterior();
  234. Itor.AttachDispatch(lpDisp,TRUE);
  235. if(BackStyle.transparent==TRUE)
  236. Itor.SetColorIndex((_variant_t)(short)xlNone);
  237. else
  238. {
  239. Itor.SetColor((_variant_t)(long)BackStyle.Color);
  240. Itor.SetPattern((_variant_t)(short)BackStyle.Pattern);
  241. Itor.SetPatternColor((_variant_t)(long)BackStyle.PatternColor);
  242. }
  243. Itor.ReleaseDispatch();
  244. }
  245. void CMyExcel::SetBorderLine(short Xposition,MyBorder XBorder)
  246. {
  247. long Row,Col;
  248. Row=GetRowS();
  249. Col=GetColS();
  250. if(Row==1)
  251. if(Xposition==xlInsideHorizontal) return;
  252. if(Col==1)
  253. if(Xposition==xlInsideVertical) return;
  254. LPDISPATCH  lpDisp=NULL;
  255. lpDisp=MyRange.GetBorders();   
  256. Borders   bds;   
  257. bds.AttachDispatch(lpDisp);   
  258. Border   bd;   
  259. lpDisp=bds.GetItem((long)Xposition);   
  260. bd.AttachDispatch(lpDisp);   
  261. bd.SetLineStyle((_variant_t)(short)XBorder.LineStyle);
  262. bd.SetColor((_variant_t)(long)XBorder.Color);
  263. bd.SetWeight((_variant_t)(short)XBorder.Weight);
  264. bd.ReleaseDispatch();
  265. bds.ReleaseDispatch();
  266. }
  267. long CMyExcel::GetRowS()
  268. {
  269. long len=0;
  270. Range rg=MyRange.GetEntireRow();
  271. len=rg.GetCount();
  272. rg.ReleaseDispatch();
  273. return len;
  274. }
  275. long CMyExcel::GetColS()
  276. {
  277. long len=0;
  278. Range rg=MyRange.GetEntireColumn();
  279. len=rg.GetCount();
  280. rg.ReleaseDispatch();
  281. return len;
  282. }
  283. void CMyExcel::SetNumberFormat(MyNumberFormat XNumberFormat)
  284. {
  285. CString strText=XNumberFormat.strValue;
  286. MyRange.SetNumberFormat(_variant_t(strText));
  287. }
  288. void CMyExcel::SetColumnWidth(int intWidth)
  289. {
  290. double f=intWidth/8.08;
  291. MyRange.SetColumnWidth((_variant_t)(double)f);
  292. }
  293. void CMyExcel::SetRowHeight(int intHeight)
  294. {
  295. double f=intHeight/8.08;
  296. MyRange.SetRowHeight((_variant_t)(double)f);
  297. }
  298. void CMyExcel::InsertPicture(CString strFilePath)
  299. {
  300. LPDISPATCH  lpDisp=NULL;
  301. if(IsFileExist(strFilePath,FALSE))
  302. {
  303. // lpDisp=MySheet.get
  304. }
  305. }
  306. void CMyExcel::SetBackPicture(CString strFilePath)
  307. {
  308. if(IsFileExist(strFilePath,FALSE)==TRUE)
  309. MySheet.SetBackgroundPicture(strFilePath);
  310. }
  311. void CMyExcel::PrintOut(short CopySum)
  312. {
  313. if(CopySum<=0) CopySum=1;
  314. COleVariant covTrue((short)TRUE); 
  315. COleVariant covFalse((short)FALSE); 
  316. COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); 
  317. MySheet.PrintOut(vtMissing,vtMissing,(_variant_t)(short)CopySum,vtMissing
  318. ,vtMissing,vtMissing,covTrue,vtMissing);
  319. }
  320. void CMyExcel::PrePrintOut(BOOL blnEnable)
  321. {
  322. COleVariant covOptional((short)blnEnable);
  323. MySheet.PrintPreview(covOptional);
  324. }
  325. BOOL CMyExcel::IsFileExist(CString strFn, BOOL bDir)
  326. {
  327.     HANDLE h;
  328. LPWIN32_FIND_DATA pFD=new WIN32_FIND_DATA;
  329. BOOL bFound=FALSE;
  330. if(pFD)
  331. {
  332. h=FindFirstFile(strFn,pFD);
  333. bFound=(h!=INVALID_HANDLE_VALUE);
  334. if(bFound)
  335. {
  336. if(bDir)
  337. bFound= (pFD->dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=NULL;
  338. FindClose(h);
  339. }
  340. delete pFD;
  341. }
  342. return bFound;
  343. }
  344. void CMyExcel::Exit()
  345. {
  346. COleVariant covFalse((short)FALSE);
  347. COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); 
  348. MyRange.ReleaseDispatch();
  349. MySheet.ReleaseDispatch();
  350. MySheets.ReleaseDispatch();
  351. MyBook.Close(covFalse,_variant_t(strFilePath),covOptional);
  352. MyBook.ReleaseDispatch();
  353. MyBooks.Close();
  354. MyBooks.ReleaseDispatch();
  355. MyApp.Quit();
  356. MyApp.ReleaseDispatch();
  357. CoUninitialize();
  358. }
  359. MyFont::MyFont()
  360. {
  361. //名字
  362.     Name="Microsoft Sans Serif";
  363. //大小
  364. size=12;
  365. //前景
  366. ForeColor=RGB(0,0,0);
  367. //粗体
  368. Bold=FALSE;
  369. //斜体
  370. Italic=FALSE;
  371. //中间线
  372. Strikethrough=FALSE;
  373. //阴影
  374. Shadow=FALSE;
  375. //下标
  376. Subscript=FALSE;
  377. //上标
  378. Superscricp=FALSE;
  379. //下划线
  380. Underline=xlUnderlineStyleNone;
  381. }
  382. MyBorder::MyBorder()
  383. {
  384. //线条形状
  385. LineStyle=xlContinuous;
  386. //粗线
  387.     Weight=xlThin;
  388. //颜色
  389.     Color=RGB(0,0,0);
  390. }
  391. MyBackStyle::MyBackStyle()
  392. {
  393. //背景颜色
  394. Color=RGB(255,255,255);
  395. //背景图案
  396.     Pattern=xlSolid;
  397. //背景图案颜色
  398.     PatternColor=RGB(255,0,0);
  399. //默认为不透明
  400. transparent=FALSE;
  401. }
  402. MyAlignment::MyAlignment()
  403. {
  404. //普通
  405. HorizontalAlignment=xlGeneral;
  406. //居中对齐
  407. VerticalAlignment=xlCenter;
  408. }
  409. MyNumberFormat::MyNumberFormat()
  410. {
  411. strValue="G/通用格式";
  412. }
  413. CString MyNumberFormat::GetText()
  414. {
  415. strValue="@";
  416. return strValue;
  417. }
  418. CString MyNumberFormat::GetGeneral()
  419. {
  420. strValue="G/通用格式";
  421. return strValue;
  422. }
  423. CString MyNumberFormat::GetNumber(BOOL blnBox,int RightSum)
  424. {
  425. CString str="0";
  426. int i;
  427. if(RightSum<0) RightSum=0;
  428. if(blnBox==TRUE)
  429. {
  430. if(RightSum==0)
  431. {
  432. str="#,##0_ ";
  433. strValue=str;
  434. return strValue;
  435. }
  436. else
  437. {
  438. str="#,##0.";
  439. for(i=0;i<RightSum;i++) str=str+"0";
  440. str=str+"_ ";
  441. strValue=str;
  442. return strValue;
  443. }
  444. }
  445. else
  446. {
  447. if(RightSum==0)
  448. {
  449. str="0_ ";
  450. strValue=str;
  451. return strValue;
  452. }
  453. else
  454. {
  455. str="0.";
  456. for(i=0;i<RightSum;i++) str=str+"0";
  457. str=str+"_ ";
  458. strValue=str;
  459. return strValue;
  460. }
  461. }
  462. }
  463. CString MyNumberFormat::GetDate(BOOL blnChinese)
  464. {
  465. if(blnChinese==TRUE)
  466. strValue="yyyy"年"m"月"d"日";@";
  467. else
  468. strValue="yyyy-m-d;@";
  469. return strValue;
  470. }
  471. CString MyNumberFormat::GetDateTime(BOOL blnChinese)
  472. {
  473. if(blnChinese==TRUE)
  474. strValue="yyyy"年"m"月"d"日" h"时"mm"分"ss"秒";@";
  475. else
  476. strValue="yyyy-m-d h:mm:ss;@";
  477. return strValue;
  478. }
  479. CString MyNumberFormat::GetDBNumber(BOOL blnChinese)
  480. {
  481. if(blnChinese==TRUE)
  482. strValue="[DBNum1][$-804]G/通用格式";
  483. else
  484. strValue="[DBNum2][$-804]G/通用格式";
  485. return strValue;
  486. }
  487. CString MyNumberFormat::GetFractionNumBer(int DownSum,int DownNum)
  488. {
  489. CString str;
  490. int i;
  491. if(DownNum>0 && DownSum>0)
  492. {
  493. AfxMessageBox("分母位数和固定数只能设置一个,默认返回分母位数的设置!"
  494. ,MB_ICONINFORMATION|MB_OK);
  495. }
  496. if(DownSum<=0)
  497. {
  498. if(DownNum<=0)
  499. {
  500. str="# ?/1";
  501. strValue=str;
  502. return strValue;
  503. }
  504. else
  505. {
  506. str.Format("# ?/%d",DownNum);
  507. strValue=str;
  508. return strValue;
  509. }
  510. }
  511. else
  512. {
  513. str="# ?/";
  514. for(i=0;i<DownSum;i++) str=str+"?";
  515. strValue=str;
  516. return strValue;
  517. }
  518. }
  519. CString MyNumberFormat::GetMoney(BOOL blnChinese,int RightSum)
  520. {
  521. CString str;
  522. int i;
  523. if(RightSum<=0) RightSum=0;
  524. if(blnChinese==TRUE)
  525. {
  526. if(RightSum==0)
  527. {
  528. str="#,##0";
  529. strValue="¥"+str+";"+"¥-"+str;
  530. return strValue;
  531. }
  532. else
  533. {
  534. str="#,##0.";
  535. for(i=0;i<RightSum;i++) str=str+"0";
  536. strValue="¥"+str+";"+"¥-"+str;
  537. return strValue;
  538. }
  539. }
  540. else
  541. {
  542. if(RightSum==0)
  543. {
  544. str="#,##0";
  545. strValue="$"+str+";"+"$-"+str;
  546. return strValue;
  547. }
  548. else
  549. {
  550. str="#,##0.";
  551. for(i=0;i<RightSum;i++) str=str+"0";
  552. strValue="$"+str+";"+"$-"+str;
  553. return strValue;
  554. }
  555. }
  556. }
  557. CString MyNumberFormat::GetPercentNumBer(int RightSum)
  558. {
  559. CString str;
  560. int i;
  561. if(RightSum<=0) RightSum=0;
  562. if(RightSum==0)
  563. {
  564. str="0%";
  565. strValue=str;
  566. return strValue;
  567. }
  568. else
  569. {
  570. str="0.";
  571. for(i=0;i<RightSum;i++) str=str+"0";
  572. strValue=str+"%";
  573. return strValue;
  574. }
  575. }
  576. CString MyNumberFormat::GetTechNumBer(int RightSum)
  577. {
  578. CString str;
  579. int i;
  580. if(RightSum<=0) RightSum=0;
  581. str="0.";
  582. for(i=0;i<RightSum;i++) str=str+"0";
  583. strValue=str+"E+00";
  584. return strValue;
  585. }
  586. CString MyNumberFormat::GetTime(BOOL blnChinese)
  587. {
  588. if(blnChinese==TRUE)
  589. {
  590. strValue="h"时"mm"分"ss"秒";@";
  591. return strValue;
  592. }
  593. else
  594. {
  595. strValue="h:mm:ss;@";
  596. return strValue;
  597. }
  598. }
  599. CString MyNumberFormat::GetPost(int Sum)
  600. {
  601. int i;
  602. CString str="";
  603. if(Sum<=0) Sum=1;
  604. for(i=0;i<Sum;i++)
  605. str=str+"0";
  606. strValue=str;
  607. return strValue;
  608. }