C++和excel交互

最近碰到在c++端打算把数据写入交互excel,一开始觉得opencv也有对应的库函数,后来发现c++端使用IO流就可以实现,现在记录一下:

// openblas-test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <fstream>
#include <streambuf>
#include <iostream>
#include <string>
/*
#include <stdio.h>
#include <stdlib.h>

#include <opencv2\opencv.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\core.hpp>

#include "cblas.h"
#include "cblas_f77.h"

using namespace std;
using namespace cv;
using namespace cv::cuda;

void nonlocalMeansFilter(Mat&, Mat&, int, int, double, double);
void writeToExcel(Mat, string);
*/
using namespace std;

void write_to_excel(string filename, bool&flag);

#define INVALID -1

int main(int argv, char**argc) {
	/*
	Mat srcImg = imread("D:\\project\\Image\\lena.jpg", 1);
	Mat dst1, dst2, dst3;

	// bilateralFilter(srcImg, dst1, 15, 130, 200);

	// writeToExcel(srcImg, "2.xls");


	imshow("src", srcImg);
	// imshow("bilateralFilter result", dst1);

	// fastNlMeansDenoising(srcImg, dst3);
	// nonlocalMeansFilter(srcImg, dst2, 3, 7, 25, 25);
	*/

	bool flag = false;
	write_to_excel("test2.xls", flag);

	// imshow("fastNlMeansDenoising result", dst2);

	// waitKey(0);

	return 0;
}
/*
void writeToExcel(Mat outputImage, string fileName)
{

	ofstream Fs(fileName);
	if (!Fs.is_open())
	{
		cout << "error!" << endl;
		return;
	}

	int channels = outputImage.channels();            //获取图像channel  
	int nrows = outputImage.rows;                     //矩阵的行数  
	int ncols = outputImage.cols*channels;             //矩阵的总列数=列数*channel分量数  

													   //循环用变量
	int i = 0;
	int j = 0;

	if (outputImage.depth() == CV_8U)//uchar
	{
		for (i = 0; i<nrows; i++)
		{
			for (j = 0; j<ncols; j++)
			{
				int tmpVal = (int)outputImage.ptr<uchar>(i)[j];
				Fs << tmpVal << '\t';
			}
			Fs << endl;
		}
	}
	else if (outputImage.depth() == CV_16S)//short
	{
		for (i = 0; i<nrows; i++)
		{
			for (j = 0; j<ncols; j++)
			{
				Fs << (short)outputImage.ptr<short>(i)[j] << '\t';
			}
			Fs << endl;
		}
	}
	else if (outputImage.depth() == CV_16U)//unsigned short
	{
		for (i = 0; i<nrows; i++)
		{
			for (j = 0; j<ncols; j++)
			{
				Fs << (unsigned short)outputImage.ptr<unsigned short>(i)[j] << '\t';
			}
			Fs << endl;
		}
	}
	else if (outputImage.depth() == CV_32S)//int 
	{
		for (i = 0; i<nrows; i++)
		{
			for (j = 0; j<ncols; j++)
			{
				Fs << (int)outputImage.ptr<int>(i)[j] << '\t';
			}
			Fs << endl;
		}
	}
	else if (outputImage.depth() == CV_32F)//float
	{
		for (i = 0; i<nrows; i++)
		{
			for (j = 0; j<ncols; j++)
			{
				Fs << (float)outputImage.ptr<float>(i)[j] << '\t';
			}
			Fs << endl;
		}
	}
	else//CV_64F double
	{
		for (i = 0; i < nrows; i++)
		{
			for (j = 0; j < ncols; j++)
			{
				Fs << (double)outputImage.ptr<double>(i)[j] << '\t';
			}
			Fs << endl;
		}
	}

	Fs.close();
}
*/
void write_to_excel(string filename, bool&flag)
{
	//定义文件输出流   
	ofstream oFile;

	//打开要输出的文件   
	oFile.open(filename, ios::out | ios::trunc);    // 这样就很容易的输出一个需要的excel 文件  
	oFile << "姓名" << "\t" << "年龄" << "\t" << "班级" << "\t" << "班主任" << endl;
	oFile << "张三" << "\t" << "22" << "\t" << "1" << "\t" << "JIM" << endl;
	oFile << "李四" << "\t" << "23" << "\t" << "3" << "\t" << "TOM" << endl;

	oFile.close();


	//打开要输出的文件   
	ifstream iFile(filename);
	string   readStr((std::istreambuf_iterator<char>(iFile)), std::istreambuf_iterator<char>());
	cout << readStr.c_str();

	flag = 1;

}

猜你喜欢

转载自blog.csdn.net/oliverkingli/article/details/80523123
今日推荐