An easy way to read and write Excel files in C + +

Source: Internet
Author: User

For file reading and writing, of course, text file is best read, best written, no document structure to consider.

For Windows Excel files such as the operation is not so easy, we can search, basically are all complex ways to read and write.

Of course, this is not my original, in some csdn corner also have this article similar expression, we do not have to be serious.


The essential:

CSV format file, is a text file, can be used in C + + file flow simple read and write. However, text files of this format can be supported by default Excel, so it is Excel to open Excel files.


The code is as follows;

#include <fstream> 
#include <string>
#include <iostream>
#include <streambuf> 
using namespace std; 

int main () 
{         
	//definition file output stream 
	ofstream ofile; 

	Open the file you want to output 
	ofile.open ("Scoresheet.csv", Ios::out | ios::trunc);    This makes it easy to output a required Excel file
	ofile << "name <<", "<<" Age "<<", "<<" Class "<<", "< < "class Teacher" << Endl; 
	Ofile << "John" << "<<" "<<", "<<" 1 "<<", "<<" JIM "<< Endl; 
	Ofile << "Dick" << "<<" "<<", "<<" 3 "<<", "<<" TOM "<< Endl; 

	Ofile.close (); 
    
         
	Open the file you want to output 
	ifstream ifile ("Scoresheet.csv");
	String   readstr (std::istreambuf_iterator<char> (ifile)),  std::istreambuf_iterator<char> () ); 
	 cout <<  readstr.c_str ();

	  return 0;
}  



The effect is as follows:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.