Reputation: 1460
I have an input file "inputfile.txt" from where I am reading input.
inputfile.txt is :-
|I|line|number|1
|P|line|number|2
|I|line|number|3
|P|line|number|4
|I|line|number|5
|P|line|number|6
Now I am processing this file according to the condition that if second char is I then it will write into "Iout.xls" and if it is 'P' then write to "Pout.xls"
Iout.xls :-
|I|line|number|1
|I|line|number|3
|I|line|number|5
Pout.xls:-
|P|line|number|2
|P|line|number|4
|P|line|number|6
both are excel files seperated by pipe.
here is my code what I am doing:-
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main() {
ifstream in;
ofstream Iout,Pout;
string str;
string::iterator it;
in.open("inputfile.txt");
Iout.open("Iout.xls",fstream::app);
Pout.open("Pout.xls",fstream::app);
if(in.is_open()){
while(in.good()) {
getline(in,str);
it=str.begin()+1;
if(*(it)=='I')
Iout<<str<<endl;
if(*(it)=='P')
Pout<<str<<endl;
}
}
else cout<<"can't open file";
Iout.close();
Pout.close();
in.close();
return 0;
}
I have two queries:-
1.I am inserting values as a text into excel (for e.g., "|P|line|number|2" is inserted as a text into Pout.xls) and there I am doing text to column manually, Is there any way to automate this using c++.
i.e., |P|line|number|2 will be inserted as:- space in first column,P in second column,line in 3rd column,number in 4th column and 2 in 5th column.
my second query:- I am writing two different files Iout.xls and Pout.xls and then after converting from text to column in both the files,I copy both files into a single file FinalOut.xls with two different sheets Iout and Pout containing the data of Iout.xls and Pout.xls. How can I open different sheets of an excel file with c++ and write corresponind data directly to the sheets.
i.e., sheet1 will be Iout and sheet2 will bePout.
I am using devc++ for my code.
Thank You
Upvotes: 2
Views: 4018
Reputation: 70369
Excel is capable of importing HTML-based files - checkout the official documentation at http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx
Since HTML is "text-based" you should be able to write is rather easily...
Another option is to use CSV (also text-based) which Excel does understand BUT there is no support in CSV for different sheets.
XLS is a binary file format defined by Microsoft which is rather complex... one option is a commercial library like LibXL - it can read and write the "genuine Excel format (XLS etc.)" including support for different sheets etc.
Upvotes: 1