c语言如何读取excel文件并保存在数组里
时间: 2024-03-16 09:43:05 浏览: 24
要读取Excel文件并保存在数组中,可以使用C语言中的库文件libxls。下面是一个简单的示例代码,可以读取Excel文件中的数据并将其保存在数组中:
```c
#include <stdio.h>
#include <stdlib.h>
#include "xls.h"
int main(int argc, char *argv[]) {
xlsWorkBook* pWB;
xlsWorkSheet* pWS;
xlsCell* pCell;
int row, col;
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
pWB = xls_open(argv[1], "UTF-8");
if (pWB == NULL) {
printf("Error: Unable to open file %s\n", argv[1]);
return 1;
}
pWS = xls_getWorkSheet(pWB, 0);
if (pWS == NULL) {
printf("Error: Unable to open worksheet\n");
return 1;
}
for (row = 0; row <= pWS->rows.lastrow; ++row) {
for (col = 0; col <= pWS->rows.lastcol; ++col) {
pCell = xls_cell(pWS, row, col);
if (pCell == NULL) {
printf("Error: Unable to read cell\n");
return 1;
}
printf("%s\t", pCell->str);
}
printf("\n");
}
xls_close(pWB);
return 0;
}
```
这个示例代码使用了libxls库来打开Excel文件,并读取第一个工作表中的所有单元格。你可以根据你的需求修改代码来实现将Excel文件中的数据保存在数组中的功能。