求助!怎么使用c语言获取excel文件的行和列数后缀为.xls

顾染尘 2020-12-11 09:30:56
如题,查了百度都没有明确的完整的代码,没有可以实现的,不知道要怎么实现呢?
给一个excel文件,通过c语言获取她的行数和列数,比如图片

行数为4,列数为4,求指点!
...全文
477 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
顾染尘 2020-12-14
  • 打赏
  • 举报
回复
#include<stdio.h>
#include "stdlib.h"
#include <Windows.h>
#include <math.h>
#include <atlstr.h>

int main()
{

	FILE *fp;
	int i,j ;
    int da[5][4] = {0} ;
    fp=fopen("C:/Users/yf/Documents/Visual Studio 2010/Projects/R/R/a.xls","r");     // fp指针指向文件头部
    for(i = 0 ;i < 5 ; i++){
        for(j = 0 ;j < 4 ; j++)
        {
			fscanf(fp,"%d",&da[i][j]);
			fseek(fp, 1, SEEK_CUR);   //fp指针从当前位置向后移动
			
		}
	}
	char FsFileData[1000] = {0};  //存储每行的字符串
	memset(FsFileData, 0, sizeof(char)*1000);
	fseek(fp, 0, SEEK_SET);   //初始化
	int count=0;
	while(!feof(fp))
	{
		
		char * ret_val;
		char * find;
		ret_val = fgets(FsFileData, 1000, fp); 
		printf( "ret_val: %s  \n", ret_val );
		count++;
	}
	printf( "count: %d  \n", count );
	fclose(fp);

	system("PAUSE");
	return 0;
}
ljhm 2020-12-12
  • 打赏
  • 举报
回复
.xls是专有协议,可以用Aspose API,或者第三方库,来解析这种文件。

如果能用.csv文本格式代替,会更加方便一些。
赵4老师 2020-12-11
  • 打赏
  • 举报
回复
仅供参考:
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language  Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.

// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb.  Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

#pragma warning (disable:4192 4146)

#include <stdio.h>
#include <tchar.h>

void dump_com_error(_com_error &e)
{
    _tprintf(_T("Oops - hit an error!\n"));
    _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
    _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());
    _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
    _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}

// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called.  If any reference
// count is non-zero, a protection fault will occur.
struct StartOle
{
    StartOle() { CoInitialize(NULL); }
    ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void main()
{
    using namespace Excel;

    _ApplicationPtr pXL;

    try
    {
      pXL.CreateInstance(L"Excel.Application");

      pXL->Visible = VARIANT_TRUE;

      WorkbooksPtr pBooks = pXL->Workbooks;
      _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
      _WorksheetPtr pSheet = pXL->ActiveSheet;

      RangePtr pRange;
      pRange = pSheet->Range["A21"];
      pRange->Value2 = 75L;
      //pRange->NumberFormatLocal = "@";

      _CommandBarsPtr pCmdbars = pXL->CommandBars;
      int iCmdbars = pCmdbars->GetCount();

      Sleep(1000);
      pRange = pSheet->Range["20:20"];
      pRange->Insert( (long)Excel::xlDown );
      pRange->Merge();

      Sleep(1000);

      pBook->Saved = VARIANT_TRUE;
      pXL->Quit();
    }
    catch(_com_error &e)
    {
      dump_com_error(e);

      pXL->Quit();
    }
}


自信男孩 2020-12-11
  • 打赏
  • 举报
回复
fgets按行读取,然后根据'\t截取,最后遇到'\n'或'\0'则为结束,这样通过一行就可以知道有几列,然后用fgets直到读到文件末尾,可以知道行数

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧