怎么用vs2010读取excel里的内容,用的c++

如题所述

你好,

你指的是excel 2013中的表格吗?如果是单纯的表格的话,你可以将excel文件另存为".csv" (comma-sperated-version, 中文为逗号分隔)格式,每个逗号对应的就是表格的一个单元。例如

1
2
3

// Example.csv
Name, Age, ID
David, 23, 0

就是一个2乘2的表格用.csv的形式来表示的,用C++读入这样的csv文件就可以了,代码如下:

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

void readCSV(const char* fileName, vector<vector<string>>& csvVector)
{
ifstream file(fileName);

while (file)
{
string s;
if (!getline(file, s)) break;

istringstream ss(s);
vector <string> record;

while (ss)
{
string s;
if (!getline(ss, s, ',')) break;
record.push_back(s);
}

csvVector.push_back(record);
}
if (!file.eof())
{
cerr << "Fooey!\n";
}
}

参数中的csvVector是一个二维的vector, 分别代表行和列,每次按行读入。追问

我是新手,您能说的详细些吗😊?

温馨提示:答案为网友推荐,仅供参考
相似回答