The function of C language to read and write Excel files is mostly to read excel tables through ODBC or OLE/COM, which requires adding classes in reading, such as CApplicaton and its header files. The operation is very complex. Generally, we only carry out simple reading and writing operations, so we don't want to use this method very much. The following mainly introduces the reading and writing operations of excel through pure C language.
1, Write operation of EXCEL file in C language
Step 1: the Excel file written in C language can only be a *. csv suffix file (like TXT, it is stored in the form of binary text. It is divided into cell contents with comma separator. xls storage is complex. csv files can be saved as. xls or. xlsx files, and the. csv file format can be selected), They can be opened as txt files through Notepad + + and other Notepad software.
It should be noted that when you write or read the *. xls file and then open the excel file, a prompt window with compatible format will pop up. Because such C language operation of Excel file is when you open the text file, the original format will be ignored, but it will not be affected. Click Yes (Y),
Step 2: process the table. After opening the table in C language, the file pointer points to the first row and column of the whole table.
If you want to write data to its next row cell (row 1, column 2), use "\ t";
If you want to write data to its next cell in the same column (row 2, column 1), use "\ n".
The specific codes are as follows:
void writeExcel() { char chy[4]={ 'x' ,'a' ,'h','w' } ; int data[4]={ 1 , 3 , 6 ,9 }; int i ; FILE *fp = NULL ; fp = fopen("G:\\Desktop\\test.csv","w") ; for (i=0 ; i<4 ;i++) fprintf(fp,"%c\t%d\n",chy[i],data[i] ) ; fclose(fp); } void main() { writeExcel() ; }
The operation results are as follows:
2, Reading operation of EXCEL file in C language
read file
For the operation of reading Excel files, the file random positioning function fseek() is used, and its general calling format is as follows:
Fseek (file pointer, displacement, starting position);
**fseek() * * Parameter Description:
Displacement
: refers to the byte offset during relocation. It represents the number of characters relative to the base address. It is usually a long integer number, which can be an integer constant, an integer expression, etc. If you use an integer constant, you need to add the letter "L" after it; If you use an integer expression, you need to cast a long integer with "(long) (expression)".
Starting position
It refers to the reference point at the time of relocation, that is, the base address, expressed as an integer or a coincidence constant. The following table:
For example:
fseek(fp , 10L , 0) ;
The specific codes are as follows:
#include <stdio.h> void main() { FILE *fp; char filename[40] ; int i,j ; float da[6][5] = {0} ; printf(" Enter file name: "); gets(filename); fp=fopen(filename,"r"); // fp Pointer to file header for(i = 0 ;i < 6 ; i++) for(j = 0 ;j < 5 ; j++) { fscanf(fp,"%f",&da[i][j]); fseek(fp, 5L, SEEK_CUR); /*fp The pointer moves backward from the current position*/ } for(i = 0 ;i < 6 ; i++) printf("%f\t%f\t%f\t%f\t%f\t\n",da[i][0], da[i][1],da[i][2],da[i][3],da[i][4]); }
Operation results
If you find that the read data is all 0 or the read data sequence position is incorrect in future learning, please refer to the parameter usage of fseek() function.
In addition, the above is carried out in CodeBlocks. If you use VS and other versions of software, the flash back problem is caused by the bug of the software itself. You can solve it by adding "system(" pause "); or getchar();" at the end of the main() function. The corresponding examples of vs reading Excel are as follows:
The code for reading this Excel file is as follows:
#include <stdio.h> void main() { FILE *fp; char filename[40] ; int i,j ; float da[6][5] = {0} ; printf(" Enter file name: "); gets(filename); fp=fopen("as.csv","r"); fseek(fp, 5L, SEEK_SET); // Read from the second line of the file for(i = 0 ;i < 6 ; i++) for(j = 0 ;j < 5 ; j++) { fscanf(fp,"%f",&da[i][j]); fseek(fp, 1L, SEEK_CUR); /*fp The pointer moves backward from the current position*/ } for(i = 0 ;i < 6 ; i++) printf("%f\t%f\t%f\t%f\t%f\t\n",da[i][0], da[i][1],da[i][2],da[i][3],da[i][4]); getchar() ; }
VS is as follows:
Operation results:
3, Add something
Because a friend told me that the running result was 0 and sent their project to me, I tried to run it and found that it was 0.0, and the program did not report an error.
Then, I try to open the excel file or csv file they sent me, and a prompt will pop up that the file is damaged. If I click "yes", the file can also continue to open. This may be because the file format is damaged, so the program can't calculate the location of the data. I simply saved their excel file as an excel, and it ran successfully. Their excel problems are shown in the figure below.
It can also be opened after damage.