C++ Excel文件读写之简便方法

 对于 文件的 读写, 当然是 文本文件 最好读,最好写,没有 什么文件结构 需要考虑。

 对于windows 下的 excel 等文件 进行操作时就不是那么容易了,大家可以搜搜 ,基本上都是都复杂的方式才能读写

关键:

      CSV 格式的文件,是一种文本文件,可以通过 C++ 的文件流简单的读写。  但是这种格式的文本文件,却是可以有 excel 默认支持的,所以用 excel 打开就是 excel文件。

代码如下;

[cpp]  view plain  copy
 
  1. #include    
  2. #include   
  3. #include   
  4. #include    
  5. using namespace std;   
  6.   
  7. int main()   
  8. {           
  9.     //定义文件输出流   
  10.     ofstream oFile;   
  11.   
  12.     //打开要输出的文件   
  13.     oFile.open("scoresheet.csv", ios::out | ios::trunc);    // 这样就很容易的输出一个需要的excel 文件  
  14.     oFile << "姓名" << "," << "年龄" << "," << "班级" << "," << "班主任" << endl;   
  15.     oFile << "张三" << "," << "22" << "," << "1" << "," << "JIM" << endl;   
  16.     oFile << "李四" << "," << "23" << "," << "3" << "," << "TOM" << endl;   
  17.   
  18.     oFile.close();   
  19.       
  20.            
  21.     //打开要输出的文件   
  22.     ifstream iFile("scoresheet.csv");  
  23.     string   readStr((std::istreambuf_iterator<char>(iFile)),  std::istreambuf_iterator<char>());   
  24.      cout <<  readStr.c_str();  
  25.   
  26.       return 0;  

 

 

你可能感兴趣的:(C++ Excel文件读写之简便方法)