YD_ExcelReader.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. // Excel 读取库
  3. #if defined(_MSC_VER) || defined(WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32)
  4. #ifdef _OCC_P_EXCLETOOL_SDK
  5. #define _OCC_P_EXCLETOOL_API __declspec(dllexport)
  6. #else
  7. #define _OCC_P_EXCLETOOL_API __declspec(dllimport)
  8. #endif
  9. #else //linux
  10. #ifdef _OCC_P_EXCLETOOL_SDK
  11. #define _OCC_P_EXCLETOOL_API __attribute__ ((visibility("default")))
  12. #else
  13. #define _OCC_P_EXCLETOOL_API __attribute__ ((visibility("default")))
  14. #endif
  15. #endif // WIN64||WIN32
  16. #include <string>
  17. #include <vector>
  18. class xhExcelReader;
  19. class xhExcelWriter;
  20. class _OCC_P_EXCLETOOL_API YD_ExcelReader
  21. {
  22. public:
  23. YD_ExcelReader(void);
  24. ~YD_ExcelReader(void);
  25. enum ex_ret_code
  26. {
  27. cd_gbk,
  28. cd_utf8,
  29. cd_auto //windows 下返回GBK ,linux 下返回utf8
  30. };
  31. public:
  32. int GetSheelNames(const std::string& strFileName, std::vector<std::string>& vrSheetList);
  33. //加载 Excel 使用表格名称
  34. int LoadSheet(const std::string& strFileName,const std::string & strSheetName);
  35. //加载 Excel 使用表格编号,编号从0开始
  36. int LoadSheet(const std::string& strFileName, int nSheetIndex);
  37. //获取列名 列名为Excel第一行
  38. int GetColNameList(std::vector<std::string> & vrStringList);
  39. //获取列数
  40. int GetColCount();
  41. //获取行数
  42. int GetRowCount();
  43. //获取Excel单元格 根据列名获取
  44. std::string FieldByName_AsString(const std::string& strFieldName,int nRow);
  45. int FieldByName_AsInt(const std::string& strFieldName, int nRow);
  46. double FieldByName_AsFloat(const std::string& strFieldName, int nRow);
  47. //获取Excel单元格 根据列编号获取
  48. std::string Cell_AsString(int nRow,int nCol);
  49. int Cell_AsInt(int nRow, int nCol);
  50. double Cell_AsFloat(int nRow, int nCol);
  51. void SetReturnCode(ex_ret_code code);
  52. ex_ret_code GetReturnCode();
  53. private:
  54. xhExcelReader * m_preader;
  55. };
  56. class _OCC_P_EXCLETOOL_API YD_ExcelWriter
  57. {
  58. public:
  59. YD_ExcelWriter(void);
  60. ~YD_ExcelWriter(void);
  61. public:
  62. //打开Excel文件 不存在就创建
  63. int OpenFile(const std::string& strFileName,bool isCreateNew = false);
  64. //打开Sheet 不存在就创建
  65. int OpenSheet(const std::string& strSheetName);
  66. //写入值
  67. int WriteStr(int nRow, int nCol,const std::string & strValue);
  68. int writeNum(int nRow, int nCol, double dValue);
  69. //关闭
  70. int Close();
  71. private:
  72. xhExcelWriter* m_pWriter;
  73. };