当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

怎样用C语言读取excel文件

更新:03-15 整理:39baobao.com
字体:

[c语言如何读取任意格式的文件]对于程序来说,不管后缀名如何,文件分为两种类型:文本文件和二进制文件。 C语言里有一系列文件操作函数。区分文本和二进制文件,需要在打开文件时设置不同的控制符mode的变量即可...+阅读

简单的方法是通过ODBC来实现:

具体实现

一、 包含Excel文件操作类头文件

#include "CSpreadSheet.h"

二、 新建Excel文件,并写入默认数据

// 新建Excel文件名及路径,TestSheet为内部表名

CSpreadSheet SS("c:\\Test.xls", "TestSheet");

CStringArray sampleArray, testRow;

SS.BeginTransaction();

// 加入标题

sampleArray.RemoveAll();

sampleArray.Add("姓名");

sampleArray.Add("年龄");

SS.AddHeaders(sampleArray);

// 加入数据

CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鹏

如何用C语言实现读取excel文件中的数据呢

基本思路基础实现方法同上篇文章《直接通过ODBC读、写Excel表格文件》相同,都是通过ODBC来把Excel表格文件当成数据库文件来进行读、写等操作,所以在Excel表格文件中写入的行头名必须是唯一的(不要重名,相当于数据库中的ID值)。本文中对Excel文件的操作都被封装进一个类CSpreadSheet中,通过它我们可以非常简便的实现各种Excel表格数据操作,并且可以对该类进行扩充来满足自己的需求。具体实现

一、 包含Excel文件操作类头文件 #include "CSpreadSheet.h"

二、 新建Excel文件,并写入默认数据 // 新建Excel文件名及路径,TestSheet为内部表名CSpreadSheet SS("c:\\Test.xls", "TestSheet");CStringArray sampleArray, testRow;SS.BeginTransaction();// 加入标题sampleArray.RemoveAll();sampleArray.Add("姓名");sampleArray.Add("年龄");SS.AddHeaders(sampleArray);// 加入数据CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鹏"};CString strAge[] = {"27","23","28","27","26"};for(int i = 0; i

三、 读取Excel文件数据 CSpreadSheet SS("c:\\Test.xls", "TestSheet");CStringArray Rows, Column;//清空列表框m_AccessList.ResetContent();for (int i = 1; iiRows) // 超出表范围查询时 { CString str; str.Format("表中总行数为: %d, ", iRows); AfxMessageBox(str + " 查询行数大于Excel表中总行数,请重新输入!"); return; } // 读取指定行数据 if(!SS.ReadRow(Rows, iRow)) { AfxMessageBox(SS.GetLastError()); return; } CString tmpStr; for (int i = 0; iiCols) // 超出表范围查询时 { CString str; str.Format("表中总列数为: %d, ", iCols); AfxMessageBox(str + " 查询列数大于Excel表中总列数,请重新输入!"); return; } else if(iRow >iRows) { CString str; str.Format("表中总行数为: %d, ", iRows); AfxMessageBox(str + " 查询行数大于Excel表中总行数,请重新输入!"); return; } // 读取指定行、列单元格数据 if(!SS.ReadCell(tempString, iColumn, iRow)) { AfxMessageBox(SS.GetLastError()); return; } CString str; str.Format("行号: %d, 列号: %d ,内容: %s", iRow,iColumn,tempString); AfxMessageBox(str); }}

六、 将存在的Excel转换另存为指定分隔的文本文件 // 将原Excel文件转换为用分号分隔的文本,并另存为同名文本文件SS....

如何用C语言读入和输出excel里的数据

简单的方法是通过ODBC来实现:具体实现

一、 包含Excel文件操作类头文件#include "CSpreadSheet.h"

二、 新建Excel文件,并写入默认数据// 新建Excel文件名及路径,TestSheet为内部表名CSpreadSheet SS("c:\\Test.xls", "TestSheet");CStringArray sampleArray, testRow;SS.BeginTransaction();// 加入标题sampleArray.RemoveAll();sampleArray.Add("姓名");sampleArray.Add("年龄");SS.AddHeaders(sampleArray);// 加入数据CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鹏"};CString strAge[] = {"27","23","28","27","26"};for(int i = 0; i

三、 读取Excel文件数据CSpreadSheet SS("c:\\Test.xls", "TestSheet");CStringArray Rows, Column;//清空列表框m_AccessList.ResetContent();for (int i = 1; i

怎么用c读取excel中的数据或者用MATLAB读取 2000行62列要

matlab的帮助里面有例子: Example -- Importing Data From an Excel Application Assume that you have an Excel spreadsheet stocks.xls. This spreadsheet contains the prices of three stocks in row 3 (columns 1 through 3) and the number of shares of these stocks in rows 6 through 8 (column 2). Initiate conversation with Excel with the command channel = ddeinit('excel','stocks.xls') DDE functions require the rxcy reference style for Excel worksheets. In Excel terminology the prices are in r3c1:r3c3 and the shares in r6c2:r8c2. Request the prices from Excel: prices = ddereq(channel,'r3c1:r3c3') prices = 42.50 15.00 78.88 Next, request the number of shares of each stock: shares = ddereq(channel, 'r6c2:r8c2') shares = 100.00 500.00 300.00

本文地址:https://www.39baobao.com/show/29_48499.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

c语言中如何读取一个文件word txt10M并把读取的文件写进磁盘中#include <stdio.h> int main() { FILE *pword,*pword1;char a; if((pword = fopen("word.txt","rt")) == NULL) return 0; if((pword1= fopen("word1.txt","at")) == NULL) return...

C语言程序读取文件1. 关键:下面的的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库以二进制模式打开文件。如果不加b,表示默认加了t,即rt,wt,其中t表示以文本模式打...

C语言中如何从文件读取矩阵参考如下: #include #include #include int main() { file *fp; if((fp=fopen("aa.txt","r"))==null) { printf("error in reading file !\n"); exit(1); } float f1,f2,f3; int n=...

C语言中如何从文件读取#include <stdio.h> int main(void) { FILE *pfin, *pfout; int n, i; pfin = fopen("input.txt", "r"); pfout = fopen("output.txt", "w"); fscanf(pfin, "%d", &n); for(i = 0; i <...

C语言文件读取到结构体数组/* 参考代码如下: "stuInfo.txt"文件中是一些学生的姓名、宿舍号、学号. 定义一个学生结构体,其中成员包括学号(char num[10])、姓名(name)、宿舍号(dormNum)。 1.从文件中读取数据,存...

c语言分段读取文本文件1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include "stdio.h" intmain() { FILE*pf=NULL; //文件指针 in...

C语言从txt文件中读取多行用逗号分隔数据保存在数组ai别听最快回答 他的程序不会处理输入的个数 这道题说白了就是处理逗号 #include<stdio.h> using namespace std; int a[2013]; int x; int i=1; int main() { freopen("test.i...

怎样用C语言保存一个文件在C语言中,文件有多种读写方式,可以一个字符一个字符地读取,也可以读取一整行,还可以读取若干个字节。文件的读写位置也非常灵活,可以从文件开头读取,也可以从中间位置读取。 在C...

C语言怎么读取文件的每一行的内容#include <stdio.h> int main() { int i,n; char a[1024]; FILE *fp; gets(a); if((fp=fopen(a,"r"))==NULL) { printf("File Name Error.\n"); return 0; } scanf("%d",&n); i=0;...