乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > c语言读取txt文件数据,将数据导出至Excel或txt-c txt导入excel,txt批量导入excel

c语言读取txt文件数据,将数据导出至Excel或txt-c txt导入excel,txt批量导入excel

作者:乔山办公网日期:

返回目录:excel表格制作


简单的方法是通过ODBC来实现:
具体实现
一、 包含Excel文件操作类头文件
#include "CSpreadSheet.h"
二、 新建Excel文件,并写e68a84e8a2ade799bee5baa6337入默认数据
// 新建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 < sizeof(strName)/sizeof(CString); i++)
{
sampleArray.RemoveAll();
sampleArray.Add(strName[i]);
sampleArray.Add(strAge[i]);
SS.AddRow(sampleArray);
}

SS.Commit();

三、 读取Excel文件数据
CSpreadSheet SS("c:\\Test.xls", "TestSheet");

CStringArray Rows, Column;

//清空列表框
m_AccessList.ResetContent();
for (int i = 1; i <= SS.GetTotalRows(); i++)
{
// 读取一行
SS.ReadRow(Rows, i);
CString strContents = "";
for (int j = 1; j <= Rows.GetSize(); j++)
{
if(j == 1)
strContents = Rows.GetAt(j-1);
else
strContents = strContents + " --> " + Rows.GetAt(j-1);
}

m_AccessList.AddString(strContents);
}

你好!
数据按行读取,然后分割,再保存

e79fa5e98193e59b9ee7ad94331C语言写一个程序:

1.txt

1 2 3 4 5
1 2
-1 1 0 15
78 9 10


2.txt

-1 1 0 15
1 2 3 4 5

3.txt

1 2 3 4 5
-1 1 0 15

#include <stdio.h>
#include <string.h>
#define N 255
int freadline(FILE *fp,char *A)
  {
    int i=0;
    char c;
    do 
     {
          c=fgetc(fp);
          if (c==EOF)
            {
              A[i]='\0';
              return EOF;
            } 
          if (c=='\n')
            {
              A[i]='\0';
              return i;
            }
          A[i++]=c;
     }while (1);
}
int main(int argc,char **argv)
{FILE *fp1,*fp2,*fp3;
 char a[N],b[N];
 char f1[N],f2[N],f3[N];
 int status1,status2;
printf("请输入第一个文件名:\n");
gets(f1);
printf("请输入第二个文件名:\n");
gets(f2);
printf("请输入输出文件名:\n");
gets(f3);
fp1=fopen(f1,"rt");
fp3=fopen(f3,"wt");
while(((status1=freadline(fp1,a))!=EOF)|(strlen(a)!=0))
 {
  printf("a=%s\n",a);
 fp2=fopen(f2,"rt");
 while ((status2=freadline(fp2,b))!=EOF|(strlen(b)!=0))
   {
     printf("b=%s\n",b);
     if (strcmp(a,b)==0) {printf("找到一个%s\n",a);fputs(a,fp3);fputc('\n',fp3);break;}
     if (status2==EOF) break;
   } 
  fclose(fp2);
  if (status1==EOF) break;
  }
fclose(fp1);fclose(fp3);
getchar();
return (0);
}

 



象txt文本文件不能象楼上那样直接用double 读吧,.\
我这思路是先从文件读取一行,应该是字符型的.
然后从每行中再分解出e68a84e8a2ade799bee5baa6363字串,再用函数转换成浮点数.
TC下己OK.

float atof(char *c)
{
float f;
sscanf(c,"%f",&f);
return f;
}

main(){
FILE *fp;
float x[10],y[10],z[10]; //这里随你行数多少定大小
char str[80],s1[20],s2[20],s3[20];
int i,j;
fp=fopen("filename","rb");
printf("here ok 1"); //调试用

getch();
i=0;
while(!feof(fp)){
fgets(str,80,fp);
sscanf(str,"%s%s%s",s1,s2,s3);
x[i]=atof(s1);
y[i]=atof(s2);
z[i]=atof(s3);

i++;
}
printf("here ok 2");//调试用
getch();
for(j=0;j<i;j++){
printf("\n%f %f %f",x[j],y[j],z[j]);

}
getch();
}

相关阅读

关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网