Excel.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #include "Excel\CApplication.h"
  3. #include "Excel\CRange.h"
  4. #include "Excel\CWorkbook.h"
  5. #include "Excel\CWorkbooks.h"
  6. #include "Excel\CWorksheet.h"
  7. #include "Excel\CWorksheets.h"
  8. #include "Excel\CWindow0.h"
  9. #include "Excel\Cnterior.h"
  10. #include "Excel\CFont0.h"
  11. #include "Excel\CBorder.h"
  12. #include "Excel\CBorders.h"
  13. #include "Excel\CChart.h"
  14. #include "Excel\CChartObject.h"
  15. #include "Excel\CChartObjects.h"
  16. #include "Excel\CChartTitle.h"
  17. #include "Excel\CAxisTitle.h"
  18. #include "Excel\CAxis.h"
  19. #include "Excel\CTickLabels.h"
  20. #include "Excel\CSeries.h"
  21. #include "Excel\CLegend.h"
  22. // 水平对齐方式
  23. #define xlLeft -4131
  24. #define xlCenter -4108
  25. #define xlRight -4152
  26. // 边框
  27. #define xlDiagonalDown 5
  28. #define xlDiagonalUp 6
  29. #define xlEdgeLeft 7
  30. #define xlEdgeTop 8
  31. #define xlEdgeBottom 9
  32. #define xlEdgeRight 10
  33. #define xlNone -4142
  34. #define xlContinuous 1
  35. #define xlAutomatic -4105
  36. // LineType
  37. #define xlThin 2 // 细线
  38. #define xlMedium -4138 // 粗线
  39. #define xlThick 4 //粗粗线
  40. #define xlInsideVertical 11
  41. #define xlInsideHorizontal 12
  42. // 图表
  43. #define xlColumns 2
  44. #define xlValue 2
  45. #define xlPrimary 1
  46. #define xlCategory 1
  47. // 图表类型
  48. #define xlColumnClustered 51
  49. // 图例放在右上角
  50. #define xlCorner 2
  51. class CExport2Excel
  52. {
  53. public:
  54. CExport2Excel(void);
  55. ~CExport2Excel(void);
  56. private:
  57. CWorksheet m_wsExcelSingle;
  58. CWorksheets m_wsExcels;
  59. CWorkbook m_wbExcelSingle;
  60. CWorkbooks m_wbExcels;
  61. CApplication m_appExcel;
  62. CRange m_rangeExcel;
  63. CBorder m_borderExcel;
  64. CBorders m_bordersExcel;
  65. COleVariant covOptional;
  66. CString sExcelFile;
  67. int iSheetIndex;
  68. private:
  69. bool bSheet;
  70. bool bCreate;
  71. public:
  72. //创建Excel文件
  73. bool CreateExcel(CString sExcelFile);
  74. //打开Excel文件
  75. bool OpenExcel(CString sExcelFile);
  76. //创建表
  77. void CreateSheet(CString sSheetName);
  78. long GetSheetsNumber();
  79. // 设置是否显示 “是否保存对文件的修改”的对话框
  80. void SetShowAlert(BOOL bShow);
  81. //设置指定列的列宽
  82. void SetColWidth(long iColumn, float iWidth);
  83. void SetRowHeight(long iRow, float iHeight);
  84. void SetAutoFilter(long iRow);
  85. //设置自动适应列宽
  86. void SetAudoColumn();
  87. //设置当前活动表
  88. void SetSheet(long iSheetIndex);
  89. //首行冻结
  90. void SetFirstLineFreezePanes();
  91. //首行颜色
  92. void SetFirstLineColor(COLORREF color = RGB(100, 100, 100));
  93. void SetLineColor(COLORREF color, long iRow, LPCTSTR pszFontName = NULL, int iFontSize = 12, bool bBold = true);
  94. void SetColor(COLORREF color, long iRow, long iColumn);
  95. //写Title
  96. void WriteTitle(LPCTSTR pszText);
  97. //写表的表头
  98. void WriteHeader(long iColumn, LPCTSTR pszText);
  99. //在指定的行和列显示文本
  100. void WriteData(long iRow, long iColumn, LPCTSTR pszText);
  101. //合并Title列
  102. void MergeTitleCells(long iRow, long iStartColumn, long iEndColumn);
  103. //合并单元格
  104. void MergeCells(long iRow, long iStartColumn, long iEndColumn);
  105. void MergeCells(long iStartRow, long iEndRow, long iStartColumn, long iEndColumn);
  106. // 获取合并单元格信息
  107. void GetMergeAreaInfo(long iRow, long iColumn, long& MergeRowCount, long& MergeColumnCount, long& MergeStartRow, long& MergeStartColumn);
  108. CString GetText(long iRow, long iColumn);
  109. // 公式读写操作
  110. CString GetCalcText(long iRow, long iColumn);
  111. void WriteCalcText(long iRow, long iColumn, LPCTSTR pszText);
  112. void InsertRow(long iRow);
  113. long GetRowCount();
  114. long GetColCount();
  115. int GetSheetIndex();
  116. void SetWrapText(long iRow, long iColumn);
  117. // 设置边框
  118. void SetBorders(long iStartRow, long iStartColumn, long iEndRow, long iEndColumn, // 指定的单元格
  119. bool bLeft, bool bTop, bool bRight, bool bBottom, // 指定的四个边
  120. bool bInsideVertical, bool bInsideHorizontal, // 内部边框
  121. long Left_LineType, long Top_LineType, long Right_LineType, long Bottom_LineType, // 四个边的线类型
  122. long InsideVertical_LineType, long InsideHorizontal_LineType);
  123. // 设置单元格“文本”格式
  124. void SetNumberFormatLocal(long iRow, long iColumn, LPCTSTR pszText);
  125. // 设置对齐方式
  126. void SetHorizontalAlignment(long iRow, long iColumn, long HAType);
  127. // 设置字体
  128. void SetFont(long iRow, LPCTSTR pszFontName, int iFontSize, bool bBold);
  129. void SetFont(long iRow, long iColumn, LPCTSTR pszFontName, int iFontSize, bool bBold);
  130. //保存Excel文件
  131. void Save();
  132. void SaveAs(CString strExcelFile);
  133. //关闭Excel操作
  134. void Close();
  135. // 创建图表
  136. void CreatePicutre(double dLeft, double dTop, double dWidth, double dHeight, // 图标的位置和大小
  137. const char* strDataSourceRange,
  138. const char* strTitle,
  139. const char* strX,
  140. const char* strY);
  141. };