C++ 语言实现读写.csv文件.xls文件
C++ 语言实现读.csv文件.xls文件
VNAM1_24100078.csv 文件内容:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include <streambuf>
#include <stdio.h>using namespace std;int readExcel()
{//定义文件输出流ofstream ofile;ifstream infile;infile.open("..\\..\\..\\test5\\example\\VNAM1_24100078.csv");//_2if (infile.is_open() == false){cerr << "文件打开失败" << endl;getchar();exit(0);}string s;int counter = 0;vector<vector<string>>all_array;vector<string> arrays; //申明字符串变量string array;while (getline(infile, s)){istringstream sin(s);while (getline(sin, array, ',')){arrays.push_back(array);}all_array.push_back(arrays);arrays.clear();counter++;if (counter > 150){break;} }//打印输入进来的excel数据int i = 0, j = 0;for (i = 0; i < 16; i++){for (j = 0; j < 4; j++){cout << all_array[i][j] << " ";}cout << endl;}return 0;
}int main()
{readExcel();system("pause");return 0;
}
程序运行结果:
C++ 语言实现将数据写入 .csv 文件
//将数据写入 .csv 文件
void writeCSV()
{vector<vector<string>>all_array;for (int i = atoi(X_0); i <= atoi(X_1); i++){std::string filename = "VNAM1" + SN + ".csv";ofstream myfile(filename, ofstream::app);myfile << "X" << i << ",";for (int j = atoi(Y_0); j <= atoi(Y_1); j++){Value = MyMeasure(vnaAddress1, BandWidth, j, i, &GetBandWidth)*1e-6;value = floor(Value);myfile << value << ",";all_array[i][j].push_back(value);}myfile << "\n";myfile.close();}
}