读写Excel的动态库,供各种开发语言调用,不需安装Office

C++读写Excel的常用方法是ODBC,前提条件是你得先找个Office,安装excel。

如果没安装怎么办?所以就找到https://github.com/tealeg/xlsx这个Go语言开发的库,把常用接口改装成dll,各种开发语言都可调用。代码已开源https://github.com/6550523/ExcelDll

C++读写Excel范例:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#define ERR_LEN 1024
#define TEXT_LEN 8848
int main()
{
	HMODULE module = LoadLibrary(_T("..\\..\\Release\\excel.dll"));
	if (module == NULL)
	{
		printf("Load excel.dll failed\n");
		return -1;
	}

	typedef void(*OpenFileFunc)(char *, char*);
	OpenFileFunc OpenFile;
	OpenFile = (OpenFileFunc)GetProcAddress(module, "OpenFile");
	char output[ERR_LEN];
	memset(output, 0, ERR_LEN);
	OpenFile("..\\..\\Release\\1.xlsx", output);
	printf(output);

	typedef int(*GetSheetsCountFunc)();
	GetSheetsCountFunc GetSheetsCount;
	GetSheetsCount = (GetSheetsCountFunc)GetProcAddress(module, "GetSheetsCount");
	printf("%d\n", GetSheetsCount());

	typedef int(*GetRowsCountFunc)(int);
	GetRowsCountFunc GetRowsCount;
	GetRowsCount = (GetRowsCountFunc)GetProcAddress(module, "GetRowsCount");
	printf("%d\n", GetRowsCount(0));

	typedef int(*GetCellsCountFunc)(int, int);
	GetCellsCountFunc GetCellsCount;
	GetCellsCount = (GetCellsCountFunc)GetProcAddress(module, "GetCellsCount");
	printf("%d\n", GetCellsCount(0, 0));

	typedef int(*GetCellStringFunc)(int, int, int, char*);
	GetCellStringFunc GetCellString;
	GetCellString = (GetCellStringFunc)GetProcAddress(module, "GetCellString");
	char cell_string[TEXT_LEN];
	memset(cell_string, 0, TEXT_LEN);
	GetCellString(0, 0, 0, cell_string);
	printf(cell_string);

	typedef void(*NewFileFunc)();
	NewFileFunc NewFile;
	NewFile = (NewFileFunc)GetProcAddress(module, "NewFile");
	NewFile();

	typedef void(*AddSheetFunc)(char*);
	AddSheetFunc AddSheet;
	AddSheet = (AddSheetFunc)GetProcAddress(module, "AddSheet");
	AddSheet("new_sheet");

	typedef void(*AddRowFunc)(int);
	AddRowFunc AddRow;
	AddRow = (AddRowFunc)GetProcAddress(module, "AddRow");
	AddRow(0);

	typedef void(*AddCellFunc)(int, int);
	AddCellFunc AddCell;
	AddCell = (AddCellFunc)GetProcAddress(module, "AddCell");
	AddCell(0, 0);

	typedef void(*SetCellStringFunc)(int, int, int, char*);
	SetCellStringFunc SetCellString;
	SetCellString = (SetCellStringFunc)GetProcAddress(module, "SetCellString");
	SetCellString(0, 0, 0, "cell_string");

	typedef void(*SaveFileFunc)(char*);
	SaveFileFunc SaveFile;
	SaveFile = (SaveFileFunc)GetProcAddress(module, "SaveFile");
	SaveFile("..\\..\\Release\\2.xlsx");

	FreeLibrary(module);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这是曾经做过的一个项目时写的一个动态库,用c++来操作Excel,包括以下功能: //===================================mainly function============================ //FuncName: xls_create //function: create excel server //return value: return 1 if success, otherwise 0 _export bool xls_create(); //FuncName: xls_new_file/xls_new_file2 //function: create an excel file (from a template) //parameter: strTemplate ->excel template file name,eg: "template.xlt" or "d:\teplate.xls" // strFileName ->the file name used to save excel file as,eg: "Temp.xls" // openfile ->1: open excel file after writen it,otherwize don't open it //return value: return the excel fileID which created from template, between 1 and 256 if success, or zero otherwise _export int xls_new_file(char* szTemplate, char* szFileName, int openfile); _export int xls_new_file2(char* szFileName, int openfile); //FuncName: xls_open_file //function: Open an exist excel file //parameter:the file name will to be opened //return value: return the fileID, if open file success,otherwize return 0 _export int xls_open_file(char* szFileName); //FuncName: xls_write_xxx //function: write a double value to xls file //parameter: fileID ->the excel file ID which we will write the value into,between 1 and 256 // sheetID ->the sheet index of the excel file which we will write the value into,between 1 and 256 // crow ->the row index which we will write the value into, not less 1 // ccol ->the column index which we will write the value into, not less 1 // value ->the double value which we will write into excel //Note: if you want to input enter key in excel file,you may use \n in your str //return value: return 1 if success, otherwise 0 _export int xls_write_str(int fileID, int sheetID, int crow, int ccol, char* value); _export int xls_write_dbl(int fileID, int sheetID, int crow, int ccol, double value); _export int xls_write_int(int fileID, int sheetID, int crow, int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称6550523

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值