Backend Development
C#.Net Tutorial
Let's talk about how to use pure C language to read and write EXCELLet's talk about how to use pure C language to read and write EXCEL
如何利用纯C语言对EXCEL进行读写操作?下面本篇文章给大家介绍一下通过纯C语言进行EXCEL读写操作的方法,希望对大家有所帮助!

在之前需要使用C语言读取Excel文件内容的功能,查阅了很多资料,大部分是通过ODBC或者过OLE/COM对Excel表格的读取操作,这变要求在工程中添加类,如CApplicaton及其头文件等,这包括Excel接口、导入类、头文件等。操作十分复杂,当然我也对这种方法进行了尝试,也实现了功能,这种方法实现的功能比较多,一般我们只是进行简单的读写操作,所以并不是很想使用这种方法。下面通过C语言读写程序来实现。
一、写操作
第一步:单纯C语言写入Excel文件只能是 *.csv的后缀文件(是和txt一样,以二进制文本形式存储,它是以都逗号分隔符做个单元格内容的划分, .xls存储比较复杂, .csv文件可以可以通过.xls或者.xlsx文件另存为,选择.csv文件格式),它们可以通过Notepad++等记事本软件当做txt文件打开。
需要注意的是:当对*.xls文件写入或者读取之后,再打开Excel文件时会弹出格式兼容的提示窗口,因为这样的C语言操作Excel文件是当文本文件打开操作的,所以会忽略原有格式,但是不影响,点击“是(Y)”即可,如下图所示:

第二步:对表格的处理,使用C语言打开表格后,文件指针指向整个表格的第1行第1列。
如果要给它的下一个同行单元格(第1行第2列)写数据,使用"\t" ;
如果要给它的下一个同列单元格(第2行第1列)写数据,使用"\n" 。
具体代码如下:
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<p>Lets talk about how to use pure C language to read and write EXCEL</p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/a1fc0edac4daee0c1a2c38737329dae2-1.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"><br><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/a1fc0edac4daee0c1a2c38737329dae2-2.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p><h2 id="strong-二-读操作-strong"><strong>二、读操作</strong></h2><h3 id="strong-读取文件-strong"><strong>读取文件</strong></h3><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/c952046d707d0b8edad6ce2e29587c1c-3.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"><br><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/c952046d707d0b8edad6ce2e29587c1c-4.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p><p>对于读取Excel文件的操作,使用了文件随机定位函数<strong>fseek()</strong>,它的一般调用格式如下:<br> fseek(文件指针,位移量,起始位置) ;<br> **fseek()**参数说明:<br> 位移量<br> : 指重新定位时的字节偏移数,表示相对于基址的字符数,通常是一个长整型数,可以是整形常量,整形表达式等。如果用整型常量,需要再后面加上字母“L”;如果使用整形表达式需要用“(long)(表达式)”强制转换成长整形。</p>
- 起始位置
- 指重新定位时的基准点,也就是基址,用整数或符合常量表示。如下表:
| 整数 | 符号常量 | 对应的起始位置 |
|---|---|---|
| 0 | SEEK_SET | 文件开头 |
| 1 | SEEK_CUR | 文件指针的当前位置 |
| 2 | SEEK_END | 文件末尾 |
例如:
fseek(fp , 10L , 0);
具体代码如下:
#include <stdio.h>
void main()
{
FILE *fp;
char filename[40] ;
int i,j ;
float da[6][5] = {0} ;
printf(" 输入文件名: ");
gets(filename);
fp=fopen(filename,"r"); // fp指针指向文件头部
for(i = 0 ;i <p>Lets talk about how to use pure C language to read and write EXCEL</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/c952046d707d0b8edad6ce2e29587c1c-5.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<hr>
<p><strong>十分抱歉,由于个人疏忽,代码给大家带来麻烦,再次表示抱歉。以上读Excel文件的错误已经解决,代码已经更新,错误的产生是由于fseek( )函数放错了位置,以及其中第二个参数的偏移量的错误,若大家在以后学习中发现读取数据全为0或者读取数据顺序位置不正确,请查阅fseek( )函数的参数使用方法。</strong></p>
<p><strong>另外,上述开发是在CodeBlocks中进行的,如果使用Visual Stdio 2010等版本软件,出现闪退问题,是软件自身bug所致,在main( )函数结尾添加"system(“pause”); 或者getchar( ); " 即可解决,对应的VS2010读Excel实例如下:</strong></p>
<hr>
<p>例程:</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/c952046d707d0b8edad6ce2e29587c1c-6.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<p>读该Excel文件代码如下:</p>
<pre class="brush:php;toolbar:false">#include <stdio.h>
void main()
{
FILE *fp;
char filename[40] ;
int i,j ;
float da[6][5] = {0} ;
printf(" 输入文件名: ");
gets(filename);
fp=fopen("as.csv","r");
fseek(fp, 5L, SEEK_SET); // 从文件第二行开始读取
for(i = 0 ;i <p>VS2010工程如下:</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/8719e70cfc8187a991b918d170ac3f33-7.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<p>Lets talk about how to use pure C language to read and write EXCEL:</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/8719e70cfc8187a991b918d170ac3f33-8.png?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<h2 id="三-最新补充">三、最新补充</h2>
<hr>
<p>由于经常有朋友告诉我Lets talk about how to use pure C language to read and write EXCEL是0 ,并将他们的工程发到我邮箱,我试着运行了下,确实发现是0.0 ,程序没有报错。</p>
<p>然后,我试着将他们发给我的工程里的excel文件或者csv文件打开,发现会弹出提示文件Lets talk about how to use pure C language to read and write EXCEL,如果我点击“是”的话,文件同样可以继续打开,这可能是文件格式Lets talk about how to use pure C language to read and write EXCEL了,所以程序计算不了数据的位置。我简单的将他们的excel文件重新另存为了一个excel,便运行成功了。他们的excel问题如下图。</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/8c199489060abe3eea489ccf96d2e7d2-9.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<p>Lets talk about how to use pure C language to read and write EXCEL后还可以打开:</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/8c199489060abe3eea489ccf96d2e7d2-10.gif?x-oss-process=image/resize,p_40" class="lazy" alt="Lets talk about how to use pure C language to read and write EXCEL"></p>
<p>谢谢! 有问题可以,一起讨论下,不过希望自己能够一步从简到繁的进行调试也是一种学习,希望不用直接把代码扔过来让给修改。</p>
<p>相关推荐:《<a href="//m.sbmmt.com/course/list/37.html" target="_blank" textvalue="C视频教程">C视频教程</a>》</p></stdio.h>The above is the detailed content of Let's talk about how to use pure C language to read and write EXCEL. For more information, please follow other related articles on the PHP Chinese website!
C# List vs Array: which one to use?Jul 22, 2025 am 02:52 AMWhether to choose an array or a List depends on the scene. 1. Use arrays first when data is fixed, because the fixed-length feature is lighter, suitable for graphics processing, underlying API and other scenarios; 2. Use List when dynamic expansion or frequent addition and deletion, because it automatically expands and provides rich methods to improve development efficiency; 3. In terms of performance, array memory is more compact, List has management overhead but can be optimized through pre-allocated capacity; 4. The access speeds are similar, but the array does not support direct addition and deletion, and the inter-insertion efficiency of List is low. Just make reasonable choices based on the data volume, operating frequency and performance requirements.
What are top-level statements in C# 9?Jul 22, 2025 am 02:07 AMTop-levelstatementsinC#9simplifycodeforsmallprogramsbyeliminatingtheneedforexplicitclassesandMainmethods.Theyallowdirectexecutionofcode,idealforscripts,learning,andprototyping.Youcanuseusingdirectives,declarevariables,useawait,andreturnexitcodes,butc
What are value types and reference types in C#?Jul 22, 2025 am 01:06 AMIn C#, the main difference between value types and reference types is data storage and assignment behavior. 1. The value type directly stores data. When assigning, copy the value itself, such as int, float, struct and enum, modifying one variable does not affect the other; 2. The reference type stores references to the data, and copying the reference when assigning, such as class, interface and delegate, modifying one variable will affect the other; 3. The value type is usually stored on the stack, while the reference type object is allocated on the heap; 4. The value type cannot be null by default (unless the nullable type is used), and the reference type can be set to null; 5. Comparison of the actual value when comparing the value type, and compare the reference address when comparing the reference type (except
Working with File Streams and I/O Operations in C#Jul 22, 2025 am 12:22 AMWhen handling file streams and I/O operations in C#, you need to master core classes and modes such as FileStream, StreamReader, asynchronous I/O and path processing. 1. Use FileStream to perform underlying file operations, support synchronization/asynchronous and access mode control, and use should ensure resource release and pay attention to FileMode and FileShare settings; 2. StreamReader and StreamWriter provide text read and write encapsulation, which is simpler and easier to use, can specify encoding methods, and can simplify one-time operations by File.ReadAllText() and other applications; 3. Asynchronous I/O improves responsiveness through async/await, suitable for large files or networks
C# var keyword best practicesJul 21, 2025 am 03:02 AMWhen using var, it should be determined based on whether the type is clear and whether the readability is affected. 1. When the type is clear on the right side of the assignment, such as varlist=newList(); can improve the code simplicity; 2. When the type is fuzzy or returns to object or interface type, var should be avoided, such as IEnumerableresult=SomeMethod(); to improve readability; 3. Use var reasonably in anonymous types and LINQ queries, such as receiving anonymous objects, but subsequent processing is recommended to encapsulate it as a specific type; 4. In team projects, coding style should be unified, and var should be used reasonably through .editorconfig or code review to avoid abuse and affect maintenance.
How to compare two strings in C#?Jul 21, 2025 am 02:49 AMComparing strings in C# should be based on the scene selection method. The == operator is case-sensitive by default and compared based on the current culture, but is not suitable for complex scenarios. 1. Using the == operator is suitable for quick comparison, but may not meet the expected results due to culture or case; 2. Using String.Equals() and passing in StringComparison enumeration can achieve more precise control, such as Ordinal, OrdinalIgnoreCase, InvariantCulture, etc.; 3. Pay attention to handling null or empty strings when comparing. It is recommended to use the string.Equals() static method or use string.IsNullOrEmpt first.
How to format a DateTime to a custom string in C#?Jul 21, 2025 am 02:46 AMThe most common method of formatting DateTime objects into custom strings in C# is to use the ToString() method and pass in the format string, which includes: 1. Use standard format strings such as "d", "D", "t", and "T" to quickly implement common date and time formats; 2. Accurately control the output format through custom format strings such as "yyyy-MM-dd", "dd/MM/yyyyHH:mm"; 3. Use CultureInfo to handle format differences in different cultural environments to adapt to multilingual users.
C# LINQ query syntax vs method syntaxJul 21, 2025 am 02:38 AMThe query syntax or method syntax of LINQ should be selected according to the scene. ① Query syntax is suitable for SQL-like expressions, such as multi-step filtering, projection, sorting, multi-table joining, grouping statistics, etc., with clear and intuitive structure; ② The method syntax is more flexible, supports chain calls and dynamic query conditions construction, and is suitable for aggregation functions, asynchronous operations and scenarios where splicing logic is required; ③ Some operations such as Take, Skip, Any, etc. can only use method syntax. The two functions are the same, and the choice mainly depends on personal habits and specific needs. Reasonable mixing of the two can improve the readability and maintenance of the code.


Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor







