C++实现Excel存储数组2

C++实现Excel存储数组2

说明

之前的那个版本是有很多模板函数但又不是模板类,看起来非常的难受,所以专门改写了一个纯模板类的Excel数组存储的程序。

使用说明

使用时需要将cpp文件和h文件放置在同一文件夹下,但同时cpp文件不要加入到工程中,否则必然报错。

程序

//CXEcelT.h
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <CDebug.h>
using namespace std;
template <class T>
class CExcelT
{
public:CExcelT(string str);
	   void modifyName(string str);
	   ~CExcelT(){ }
	   vector<vector<T>> read();
	   bool write(vector<vector<T>> vec);
private: string name;
};
#include "CExcelT.cpp"

//CExcelT.cpp
#include "pch.h"
#include "CExcelT.h"
template<class T>
inline CExcelT<T>::CExcelT(string str)
{
	name = str;
}

template<class T>
inline void CExcelT<T>::modifyName(string str)
{
	name = str;
}

template<class T>
inline vector<vector<T>> CExcelT<T>::read()
{
	ifstream myfile(name.c_str());
	if (!myfile.is_open())
	{
		CDebug() << "can not open this file";
		return { };
	}
	string line;
	int w, h;
	getline(myfile, line);
	string filed;
	istringstream mystr(line);
	getline(mystr, filed, ',');
	w = atoi(filed.c_str());
	getline(mystr, filed, ',');
	h = atoi(filed.c_str());
	vector<vector<T>> vec;
	vec.resize(w);
	for (int i = 0; i < w; i++)
	{
		vec[i].resize(h);
	}
	for (int i = 0; i < w; i++)
	{
		string line;
		getline(myfile, line);
		istringstream mystr(line);
		for (int j = 0; j < h; j++)
		{
			string filed;
			getline(mystr, filed, ',');
			vec[i][j] = filed;
		}
	}
	myfile.close();
	return vec;

}

template<class T>
inline bool CExcelT<T>::write(vector<vector<T>> vec)
{
	if (vec.empty())  //空的,不做任何处理
		return false;
	if (name.empty())
		return false;
	unsigned int w = vec.size();
	unsigned int h = vec[0].size();
	ofstream outfile(name.c_str());
	if (!outfile.is_open())
	{
		CDebug() << "can not open this file";
		return false;
	}
	outfile.clear();
	outfile << w << "," << h << "\n";
	for (unsigned int i = 0; i < w; i++)
	{
		for (unsigned int j = 0; j < h; j++)
		{
			outfile << vec[i][j];
			if (j != h - 1)
				outfile << ",";
		}
		outfile << "\n";
	}
	outfile.close();
	return true;
}

最后

这里写入数据是有一定的格式的,如果需要对格式进行一定的修改,应当在原有程序的基础上进行必要的修改操作。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以使用第三方库来读取 Excel 文件中的数据,例如使用 `libxls` 库。以下是一个简单的示例代码,演示如何使用 `libxls` 库读取 Excel 文件中的数据,并将其存储在目标数组中。 ```c #include <stdio.h> #include <libxls/xls.h> int main() { xlsWorkBook* pWorkbook = xls_open("example.xls", "UTF-8"); xlsWorkSheet* pWorksheet = xls_getWorkSheet(pWorkbook, 0); // 获取行数和列数 int numRows = xls_rowCount(pWorksheet); int numCols = xls_colCount(pWorksheet); // 分配内存 int** data = (int**)malloc(numRows * sizeof(int*)); for (int i = 0; i < numRows; i++) { data[i] = (int*)malloc(numCols * sizeof(int)); } // 读取数据 for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { data[i][j] = xls_cellValue(pWorksheet, i, j); } } // 打印数据 for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { printf("%d ", data[i][j]); } printf("\n"); } // 释放内存 for (int i = 0; i < numRows; i++) { free(data[i]); } free(data); xls_close(pWorkbook); return 0; } ``` 在这个示例中,我们使用 `xls_open` 函数打开 Excel 文件,并使用 `xls_getWorkSheet` 函数获取第一个工作表。然后,我们使用 `xls_rowCount` 和 `xls_colCount` 函数获取行数和列数。接下来,我们分配足够的内存来存储数据,并使用 `xls_cellValue` 函数逐个读取单元格的值并将其存储在目标数组中。最后,我们打印数据,并释放分配的内存。 请注意,这个示例仅适用于读取整数类型的单元格值。如果您需要读取其他类型的单元格值,请参考 `libxls` 库的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值