xlsxio_read.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*****************************************************************************
  2. Copyright (C) 2016 Brecht Sanders All Rights Reserved
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. *****************************************************************************/
  19. /**
  20. * @file xlsxio_read.h
  21. * @brief XLSX I/O header file for reading .xlsx files.
  22. * @author Brecht Sanders
  23. * @date 2016
  24. * @copyright MIT
  25. *
  26. * Include this header file to use XLSX I/O for reading .xlsx files and
  27. * link with -lxlsxio_read.
  28. * This header provides both advanced methods using callback functions and
  29. * simple methods for iterating through data.
  30. */
  31. #ifndef INCLUDED_XLSXIO_READ_H
  32. #define INCLUDED_XLSXIO_READ_H
  33. #include <stdlib.h>
  34. #if defined(_MSC_VER) && _MSC_VER < 1600
  35. typedef signed __int64 int64_t;
  36. typedef unsigned __int64 uint64_t;
  37. #else
  38. #include <stdint.h>
  39. #endif
  40. #include <time.h>
  41. /*! \brief character type used (usually char, but when XML_UNICODE is defined wchar_t) */
  42. #ifndef XLSXIOCHAR
  43. #if defined(XML_UNICODE_WCHAR_T)
  44. #warning Building with XML_UNICODE_WCHAR_T and -fshort-wchar is not supported unless all other linked libraries and programs are also compiled with -fshort-wchar
  45. #elif !defined(XML_UNICODE)
  46. #define XLSXIOCHAR char
  47. #else
  48. #include <wchar.h>
  49. #define XLSXIOCHAR wchar_t
  50. #endif
  51. #endif
  52. /*! \cond PRIVATE */
  53. #ifndef DLL_EXPORT_XLSXIO
  54. #ifdef _WIN32
  55. #if defined(BUILD_XLSXIO_DLL) || defined(BUILD_XLSXIO_SHARED) || defined(xlsxio_write_SHARED_EXPORTS)
  56. #define DLL_EXPORT_XLSXIO __declspec(dllexport)
  57. #elif !defined(STATIC) && !defined(BUILD_XLSXIO_STATIC) && !defined(BUILD_XLSXIO)
  58. #define DLL_EXPORT_XLSXIO __declspec(dllimport)
  59. #else
  60. #define DLL_EXPORT_XLSXIO
  61. #endif
  62. #else
  63. #define DLL_EXPORT_XLSXIO
  64. #endif
  65. #endif
  66. /*! \endcond */
  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif
  70. /*! \brief get xlsxio_write version
  71. * \param pmajor pointer to integer that will receive major version number
  72. * \param pminor pointer to integer that will receive minor version number
  73. * \param pmicro pointer to integer that will receive micro version number
  74. * \sa xlsxiowrite_get_version_string()
  75. */
  76. DLL_EXPORT_XLSXIO void xlsxioread_get_version (int* pmajor, int* pminor, int* pmicro);
  77. /*! \brief get xlsxio_write version string
  78. * \return version string
  79. * \sa xlsxiowrite_get_version()
  80. */
  81. DLL_EXPORT_XLSXIO const XLSXIOCHAR* xlsxioread_get_version_string ();
  82. /*! \brief read handle for .xlsx object */
  83. typedef struct xlsxio_read_struct* xlsxioreader;
  84. /*! \brief open .xlsx file
  85. * \param filename path of .xlsx file to open
  86. * \return read handle for .xlsx object or NULL on error
  87. * \sa xlsxioread_close()
  88. */
  89. DLL_EXPORT_XLSXIO xlsxioreader xlsxioread_open (const char* filename);
  90. /*! \brief open .xlsx file
  91. * \param filehandle file handle of .xlsx file opened with read access in binary mode
  92. * \return read handle for .xlsx object or NULL on error
  93. * \sa xlsxioread_close()
  94. */
  95. DLL_EXPORT_XLSXIO xlsxioreader xlsxioread_open_filehandle (int filehandle);
  96. /*! \brief open .xlsx from memory buffer
  97. * \param data memory buffer containing .xlsx file (data must remain valid as long as any xlsxioread_ functions are called)
  98. * \param datalen size of memory buffer containing .xlsx file
  99. * \param freedata if non-zero data will be freed by xlsxioread_close()
  100. * \return read handle for .xlsx object or NULL on error
  101. * \sa xlsxioread_close()
  102. */
  103. DLL_EXPORT_XLSXIO xlsxioreader xlsxioread_open_memory (void* data, uint64_t datalen, int freedata);
  104. /*! \brief close .xlsx file
  105. * \param handle read handle for .xlsx object
  106. * \sa xlsxioread_open()
  107. */
  108. DLL_EXPORT_XLSXIO void xlsxioread_close (xlsxioreader handle);
  109. /*! \brief type of pointer to callback function for listing worksheets
  110. * \param name name of worksheet
  111. * \param callbackdata callback data passed to xlsxioread_list_sheets
  112. * \return zero to continue, non-zero to abort
  113. * \sa xlsxioread_list_sheets()
  114. */
  115. typedef int (*xlsxioread_list_sheets_callback_fn)(const XLSXIOCHAR* name, void* callbackdata);
  116. /*! \brief list worksheets in .xlsx file
  117. * \param handle read handle for .xlsx object
  118. * \param callback callback function called for each worksheet
  119. * \param callbackdata custom data as passed to quickmail_add_body_custom/quickmail_add_attachment_custom
  120. * \sa xlsxioread_list_sheets_callback_fn
  121. */
  122. DLL_EXPORT_XLSXIO void xlsxioread_list_sheets (xlsxioreader handle, xlsxioread_list_sheets_callback_fn callback, void* callbackdata);
  123. /*! \brief possible values for the flags parameter of xlsxioread_process()
  124. * \sa xlsxioread_process()
  125. * \name XLSXIOREAD_SKIP_*
  126. * \{
  127. */
  128. /*! \brief don't skip any rows or cells \hideinitializer */
  129. #define XLSXIOREAD_SKIP_NONE 0
  130. /*! \brief skip empty rows (note: cells may appear empty while they actually contain data) \hideinitializer */
  131. #define XLSXIOREAD_SKIP_EMPTY_ROWS 0x01
  132. /*! \brief skip empty cells \hideinitializer */
  133. #define XLSXIOREAD_SKIP_EMPTY_CELLS 0x02
  134. /*! \brief skip empty rows and cells \hideinitializer */
  135. #define XLSXIOREAD_SKIP_ALL_EMPTY (XLSXIOREAD_SKIP_EMPTY_ROWS | XLSXIOREAD_SKIP_EMPTY_CELLS)
  136. /*! \brief skip extra cells to the right of the rightmost header cell \hideinitializer */
  137. #define XLSXIOREAD_SKIP_EXTRA_CELLS 0x04
  138. /*! \brief skip hidden rows \hideinitializer */
  139. #define XLSXIOREAD_SKIP_HIDDEN_ROWS 0x08
  140. /*! @} */
  141. /*! \brief type of pointer to callback function for processing a worksheet cell value
  142. * \param row row number (first row is 1)
  143. * \param col column number (first column is 1)
  144. * \param value value of cell (note: formulas are not calculated)
  145. * \param callbackdata callback data passed to xlsxioread_process
  146. * \return zero to continue, non-zero to abort
  147. * \sa xlsxioread_process()
  148. * \sa xlsxioread_process_row_callback_fn
  149. */
  150. typedef int (*xlsxioread_process_cell_callback_fn)(size_t row, size_t col, const XLSXIOCHAR* value, void* callbackdata);
  151. /*! \brief type of pointer to callback function for processing the end of a worksheet row
  152. * \param row row number (first row is 1)
  153. * \param maxcol maximum column number on this row (first column is 1)
  154. * \param callbackdata callback data passed to xlsxioread_process
  155. * \return zero to continue, non-zero to abort
  156. * \sa xlsxioread_process()
  157. * \sa xlsxioread_process_cell_callback_fn
  158. */
  159. typedef int (*xlsxioread_process_row_callback_fn)(size_t row, size_t maxcol, void* callbackdata);
  160. /*! \brief process all rows and columns of a worksheet in an .xlsx file
  161. * \param handle read handle for .xlsx object
  162. * \param sheetname worksheet name (NULL for first sheet)
  163. * \param flags XLSXIOREAD_SKIP_ flag(s) to determine how data is processed
  164. * \param cell_callback callback function called for each cell
  165. * \param row_callback callback function called after each row
  166. * \param callbackdata callback data passed to xlsxioread_process
  167. * \return zero on success, non-zero on error
  168. * \sa xlsxioread_process_row_callback_fn
  169. * \sa xlsxioread_process_cell_callback_fn
  170. */
  171. DLL_EXPORT_XLSXIO int xlsxioread_process (xlsxioreader handle, const XLSXIOCHAR* sheetname, unsigned int flags, xlsxioread_process_cell_callback_fn cell_callback, xlsxioread_process_row_callback_fn row_callback, void* callbackdata);
  172. /*! \brief read handle for list of worksheet names */
  173. typedef struct xlsxio_read_sheetlist_struct* xlsxioreadersheetlist;
  174. /*! \brief open list of worksheet names
  175. * \param handle read handle for .xlsx object
  176. * \sa xlsxioread_sheetlist_close()
  177. * \sa xlsxioread_open()
  178. */
  179. DLL_EXPORT_XLSXIO xlsxioreadersheetlist xlsxioread_sheetlist_open (xlsxioreader handle);
  180. /*! \brief close worksheet
  181. * \param sheetlisthandle read handle for worksheet object
  182. * \sa xlsxioread_sheetlist_open()
  183. */
  184. DLL_EXPORT_XLSXIO void xlsxioread_sheetlist_close (xlsxioreadersheetlist sheetlisthandle);
  185. /*! \brief get next worksheet name
  186. * \param sheetlisthandle read handle for worksheet object
  187. * \return name of worksheet or NULL if no more worksheets are available
  188. * \sa xlsxioread_sheetlist_open()
  189. */
  190. DLL_EXPORT_XLSXIO const XLSXIOCHAR* xlsxioread_sheetlist_next (xlsxioreadersheetlist sheetlisthandle);
  191. /*! \brief read handle for worksheet object */
  192. typedef struct xlsxio_read_sheet_struct* xlsxioreadersheet;
  193. /*! \brief get index of last row read from worksheet (returns 0 if no row was read yet)
  194. * \param sheethandle read handle for worksheet object
  195. * \sa xlsxioread_sheet_open()
  196. */
  197. DLL_EXPORT_XLSXIO size_t xlsxioread_sheet_last_row_index (xlsxioreadersheet sheethandle);
  198. /*! \brief get index of last column read from current row in worksheet (returns 0 if no column was read yet)
  199. * \param sheethandle read handle for worksheet object
  200. * \sa xlsxioread_sheet_open()
  201. */
  202. DLL_EXPORT_XLSXIO size_t xlsxioread_sheet_last_column_index (xlsxioreadersheet sheethandle);
  203. /*! \brief get flags used to open worksheet
  204. * \param sheethandle read handle for worksheet object
  205. * \sa xlsxioread_sheet_open()
  206. */
  207. DLL_EXPORT_XLSXIO unsigned int xlsxioread_sheet_flags (xlsxioreadersheet sheethandle);
  208. /*! \brief open worksheet
  209. * \param handle read handle for .xlsx object
  210. * \param sheetname worksheet name (NULL for first sheet)
  211. * \param flags XLSXIOREAD_SKIP_ flag(s) to determine how data is processed
  212. * \return read handle for worksheet object or NULL in case of error
  213. * \sa xlsxioread_sheet_close()
  214. * \sa xlsxioread_open()
  215. */
  216. DLL_EXPORT_XLSXIO xlsxioreadersheet xlsxioread_sheet_open (xlsxioreader handle, const XLSXIOCHAR* sheetname, unsigned int flags);
  217. /*! \brief close worksheet
  218. * \param sheethandle read handle for worksheet object
  219. * \sa xlsxioread_sheet_open()
  220. */
  221. DLL_EXPORT_XLSXIO void xlsxioread_sheet_close (xlsxioreadersheet sheethandle);
  222. /*! \brief get next row from worksheet (to be called before each row)
  223. * \param sheethandle read handle for worksheet object
  224. * \return non-zero if a new row is available
  225. * \sa xlsxioread_sheet_open()
  226. */
  227. DLL_EXPORT_XLSXIO int xlsxioread_sheet_next_row (xlsxioreadersheet sheethandle);
  228. /*! \brief get next cell from worksheet
  229. * \param sheethandle read handle for worksheet object
  230. * \return value (caller must free the result) or NULL if no more cells are available in the current row
  231. * \sa xlsxioread_sheet_open()
  232. */
  233. DLL_EXPORT_XLSXIO XLSXIOCHAR* xlsxioread_sheet_next_cell (xlsxioreadersheet sheethandle);
  234. /*! \brief get next cell from worksheet as a string
  235. * \param sheethandle read handle for worksheet object
  236. * \param pvalue pointer where string will be stored if data is available (caller must free the result)
  237. * \return non-zero if a new cell was available in the current row
  238. * \sa xlsxioread_sheet_open()
  239. * \sa xlsxioread_sheet_next_cell()
  240. */
  241. DLL_EXPORT_XLSXIO int xlsxioread_sheet_next_cell_string (xlsxioreadersheet sheethandle, XLSXIOCHAR** pvalue);
  242. /*! \brief get next cell from worksheet as an integer
  243. * \param sheethandle read handle for worksheet object
  244. * \param pvalue pointer where integer will be stored if data is available
  245. * \return non-zero if a new cell was available in the current row
  246. * \sa xlsxioread_sheet_open()
  247. * \sa xlsxioread_sheet_next_cell()
  248. */
  249. DLL_EXPORT_XLSXIO int xlsxioread_sheet_next_cell_int (xlsxioreadersheet sheethandle, int64_t* pvalue);
  250. /*! \brief get next cell from worksheet as a floating point value
  251. * \param sheethandle read handle for worksheet object
  252. * \param pvalue pointer where floating point value will be stored if data is available
  253. * \return non-zero if a new cell was available in the current row
  254. * \sa xlsxioread_sheet_open()
  255. * \sa xlsxioread_sheet_next_cell()
  256. */
  257. DLL_EXPORT_XLSXIO int xlsxioread_sheet_next_cell_float (xlsxioreadersheet sheethandle, double* pvalue);
  258. /*! \brief get next cell from worksheet as date and time data
  259. * \param sheethandle read handle for worksheet object
  260. * \param pvalue pointer where date and time data will be stored if data is available
  261. * \return non-zero if a new cell was available in the current row
  262. * \sa xlsxioread_sheet_open()
  263. * \sa xlsxioread_sheet_next_cell()
  264. */
  265. DLL_EXPORT_XLSXIO int xlsxioread_sheet_next_cell_datetime (xlsxioreadersheet sheethandle, time_t* pvalue);
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif