| |

VerySource

 Forgot password?
 Register
Search
View: 825|Reply: 6

读取Excel表格、Xlsx文件的C++代码源码

[Copy link]

4

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2023-9-18 11:48:16
| Show all posts |Read mode
本篇文章属于《518抽奖软件开发日志》系列文章的一部分。
我在开发《518抽奖软件》(www.518cj.net)的时候,需要支持从excel表格里面导入数据。找到几个读取表格的开源库,但是有的收费有的不好用,最终用的xlnt这个只能读xlsx的开源库,代码如下。
  1. #include <xlnt/xlnt.hpp>
  2. using namespace xlnt;
  3. #define MAX_MSG  2048

  4. BOOL load_xlsx(const WCHAR* file, int maxRow, vector<vector<string>>& rows, WCHAR* err)
  5. {
  6.         CHAR fileA[MAX_PATH * 3] = { 0 };
  7.         WideCharToMultiByte(CP_UTF8, 0, file, -1, fileA, sizeof(fileA) - 1, NULL, NULL);

  8.         try
  9.         {
  10.                 workbook wb;
  11.                 try { wb.load(fileA); }
  12.                 catch (xlnt::exception e)
  13.                 {
  14.                         if (err) MultiByteToWideChar(CP_UTF8, 0, e.what(), -1, err, MAX_MSG - 1);
  15.                         return FALSE;
  16.                 }
  17.                 worksheet ws = wb[0];

  18.                 /* 列不存在:一般读取空字符串,不会失败 */
  19.                 BOOL bFirstRow = TRUE;
  20.                 for (auto row : ws.rows(true))
  21.                 {
  22.                         vector<string> one;
  23.                         if (bFirstRow)
  24.                         {
  25.                                 bFirstRow = FALSE;
  26.                                 continue;
  27.                         }
  28.                         for (auto cell : row)
  29.                         {
  30.                                 one.push_back(cell.to_string());
  31.                                 if (one.size() >= 7)
  32.                                         break;
  33.                         }
  34.                         while (one.size() < 7)
  35.                                 one.push_back("");

  36.                         rows.push_back(one);
  37.                         if (maxRow != 0 && rows.size() >= maxRow)
  38.                                 break;
  39.                 }
  40.         }
  41.         catch (...)
  42.         {
  43.                  if (err) wcscpy(err, L"unknown error");
  44.                  rows.clear();
  45.                  return FALSE;
  46.         }
  47.         return TRUE;
  48. }

Copy the Code


Reply

Use magic Report

0

Threads

3

Posts

1497.00

Credits

Credits
1497.00

 China

Post time: 2023-9-19 09:47:09
| Show all posts
如果可以的话,分享一下这个库
Reply

Use magic Report

0

Threads

5

Posts

66.00

Credits

Member

Rank: 2

Credits
66.00

 China

Post time: 2024-4-24 13:22:07
| Show all posts
Why 发表于 2023-9-19 09:47
如果可以的话,分享一下这个库

https://github.com/tfussell/xlnt.git
Reply

Use magic Report

0

Threads

3

Posts

1497.00

Credits

Credits
1497.00

 China

Post time: 2024-4-25 20:41:15
| Show all posts
che_dan365 发表于 2024-4-24 13:22
https://github.com/tfussell/xlnt.git

这个其实并不是很好用,libxl更好用
Reply

Use magic Report

0

Threads

5

Posts

66.00

Credits

Member

Rank: 2

Credits
66.00

 China

Post time: 2024-4-26 10:25:21
| Show all posts
Why 发表于 2024-4-25 20:41
这个其实并不是很好用,libxl更好用

那我得去看看
Reply

Use magic Report

0

Threads

5

Posts

66.00

Credits

Member

Rank: 2

Credits
66.00

 China

Post time: 2024-4-26 10:26:09
| Show all posts
Why 发表于 2024-4-25 20:41
这个其实并不是很好用,libxl更好用

大哥,你说的这个收费的额
Reply

Use magic Report

0

Threads

3

Posts

1497.00

Credits

Credits
1497.00

 China

Post time: 2024-4-26 20:29:51
| Show all posts
che_dan365 发表于 2024-4-26 10:26
大哥,你说的这个收费的额

excel自带的也可以的
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list