user-0 提问: 求助！怎么使用c语言获取excel文件的行和列数后缀为.xls 

回复:
user: user-0
content: #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; }

user: user-1
content: .xls是专有协议，可以用Aspose API，或者第三方库，来解析这种文件。  如果能用.csv文本格式代替，会更加方便一些。

user: user-2
content: 仅供参考：// 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();     } }

user: user-3
content: fgets按行读取，然后根据'\t截取，最后遇到'\n'或'\0'则为结束，这样通过一行就可以知道有几列，然后用fgets直到读到文件末尾，可以知道行数