doubt in CFile
-
hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.
Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.
-
hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.
Rakesh5 wrote:
CFile::modeCreate
You are recreating the file each time with new content using that flag.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns Help humanity, join the CodeProject grid computing team here
-
hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.
After you open the file, use the SeekToEnd function:
myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL);
myfile.SeekToEnd();
myfile.Write(sUserID,sUserID.GetLength());I can't say that I've ever seen anyone use CFile to write to an excel file, so I'm not sure how you're accomplishing that. Most people would write the data to a .csv file, which can then be opened in excel. Each cell data on the same line would be separated by a comma, with a CR/LF pair at the end of that line. I don't think you're actually creating a true .xls file - it's most likely just a text file, but with the .xls extension, excel may be able to open it. Be aware that just because you name a file with .xls as an extension, it's not really an Excel file. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.
CFile is not the right object to work with excel. Presently what you are doing is, creating a text file with name "example.xls" and writing/overwriting its data. Maybe using "modeAppend" may help you to get away with the overwriting issue. But still may not serve your purpose. To modify an "real" excel file, or creating a new excel you need to use Excel . Microsoft provides a way to start excel.exe invisible through your program and then open an existing/create_new excel file and make changes to it. This is called Excel Automation/Office Automation. http://support.microsoft.com/kb/196776[^]
Suhredayan
-
hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.
In addition to the Excel Automation suggestions, you could also try using
CRecordset
with Excel's ODBC interface."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
After you open the file, use the SeekToEnd function:
myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL);
myfile.SeekToEnd();
myfile.Write(sUserID,sUserID.GetLength());I can't say that I've ever seen anyone use CFile to write to an excel file, so I'm not sure how you're accomplishing that. Most people would write the data to a .csv file, which can then be opened in excel. Each cell data on the same line would be separated by a comma, with a CR/LF pair at the end of that line. I don't think you're actually creating a true .xls file - it's most likely just a text file, but with the .xls extension, excel may be able to open it. Be aware that just because you name a file with .xls as an extension, it's not really an Excel file. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.
-
hi jonathan, thanks alot for your reply.. can u please teach me how to pass the pointer using the getlength method? thanks once again for patiently replying.. rakesh.
Have a look at http://msdn.microsoft.com/en-us/library/8c5ccz0x(VS.80).aspx[^] where there's an example, remember it moves the pointer a number of bytes.
-
Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.
-
In addition to the Excel Automation suggestions, you could also try using
CRecordset
with Excel's ODBC interface."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
I you'd prefer the text format over the CRecordset/Automation approaches, you could use a tab character ("\t") or a comma to separate the cells, and a newline to separate the rows. If you only use the
CFile::modeCreate
flag when opening the file, the file gets truncated to zero length, effectively overwriting your data. Have a look atCFile::modeNoTruncate
. Something like this:CFile theFile;
theFile.Open(_T("example.txt"), CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, NULL);
CString strOut = _T("Row1Col1\tRow1Col2\tRow1Col3\r\nRow2Col1\tRow2Col2\tRow2Col3");
theFile.Write(strOut, strOut.GetLength() * sizeof(TCHAR));Note how the file name extension is "txt", you can still open it in Microsoft Excel by right-clicking it. Or you could keep the "xls" extension and keep pretending it's a real binary Excel file. No user will ever notice :-)
-
I you'd prefer the text format over the CRecordset/Automation approaches, you could use a tab character ("\t") or a comma to separate the cells, and a newline to separate the rows. If you only use the
CFile::modeCreate
flag when opening the file, the file gets truncated to zero length, effectively overwriting your data. Have a look atCFile::modeNoTruncate
. Something like this:CFile theFile;
theFile.Open(_T("example.txt"), CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, NULL);
CString strOut = _T("Row1Col1\tRow1Col2\tRow1Col3\r\nRow2Col1\tRow2Col2\tRow2Col3");
theFile.Write(strOut, strOut.GetLength() * sizeof(TCHAR));Note how the file name extension is "txt", you can still open it in Microsoft Excel by right-clicking it. Or you could keep the "xls" extension and keep pretending it's a real binary Excel file. No user will ever notice :-)
1. FILE * pFile; pFile = fopen ("C:file.csv","a"); if (pFile!=NULL) { CString x2 = "CString1"+","+"CString2"+"\n"; fwrite(x2, x2.GetLength(), 1, pFile); fclose (pFile); } 2. Use ,(comma) operator for seperate the objects. use .csv file. it will open in excel.
G.Paulraj