-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (71 loc) · 2.14 KB
/
Copy pathmain.cpp
File metadata and controls
72 lines (71 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <xlnt/xlnt.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include "cmdline/cmdline.h"
int main(int argc, char **argv)
{
cmdline::parser cmdline_parser;
cmdline_parser.add<std::string>("filepath", 'f', "xlsx file path", true);
cmdline_parser.add<std::string>("sheet_name", 's', "the convert sheet name", false, "");
cmdline_parser.add<std::size_t>("sheet_index", 'i', "the convert sheet index", false, 0);
cmdline_parser.add<std::string>("out_file", 'o', "the output file path", false, "output.csv");
cmdline_parser.parse_check(argc, argv);
std::string filename = cmdline_parser.get<std::string>("filepath");
std::string sheet_name = cmdline_parser.get<std::string>("sheet_name");
std::size_t sheet_index = cmdline_parser.get<std::size_t>("sheet_index");
std::string output_file = cmdline_parser.get<std::string>("out_file");
std::ifstream fin(filename);
if(!fin)
{
std::clog << "Please input the xlsx file is exist" << std::endl;
return -1;
}
fin.close();
auto wb = xlnt::workbook();
wb.load(filename);
xlnt::worksheet ws;
if("" == sheet_name)
{
ws = wb.active_sheet();
}
else if("" != sheet_name)
{
if (!wb.contains(sheet_name))
{
std::clog << "Please input a exist sheet name" << std::endl;
return -1;
}
ws = wb.sheet_by_title(sheet_name);
}
else if(0 != sheet_index)
{
if(wb.sheet_count() >= sheet_index)
{
std::clog << "sheet_index is error, more than the number of sheet" << std::endl;
return -1;
}
ws = wb.sheet_by_id(sheet_index);
}
int maxlen = 0;
std::ofstream fout(output_file);
std::stringstream temp_str;
for (auto row : ws.rows(false))
{
temp_str.str("");
int count = 0;
for (auto cell : row)
{
if(count >= 0)
{
count++;
temp_str << ",";
}
temp_str << cell.to_string();
}
fout << temp_str.str() << '\n';
}
return 0;
}