//excel.exe C++/CLIでxlsxファイルの読み書き #include #include //#include //#using "excelcs101.dll" //#using "excelcs101.netmodule" using namespace System; using namespace System::Runtime::InteropServices; extern "C" { __declspec(dllexport) int loaddll(); __declspec(dllexport) int opnexcel(int flag); __declspec(dllexport) int opnbook(char * string01); __declspec(dllexport) int opnsheet(char * string01); __declspec(dllexport) char * rcell(int y1,int x1,char * string01); __declspec(dllexport) char * rcellu(int y1,int x1,char * string01); __declspec(dllexport) char * rcellfx(int y1,int x1,char * string01); __declspec(dllexport) int wcellval(int y1,int x1,int int01); __declspec(dllexport) int wcellstr(int y1,int x1,char * string01); __declspec(dllexport) int wcellstru(int y1,int x1,char * string01); __declspec(dllexport) int wcellfx(int y1,int x1,char * string01); __declspec(dllexport) int cellattr( int y1, int x1, int h1, int w1, char * string01, int size1, int r1, int g1, int b1, int r2, int g2, int b2, int valign, int halign); __declspec(dllexport) int cellmrg( int y1, int x1, int y2, int x2); __declspec(dllexport) int setprint(int y1, int x1, int y2, int x2, int size1, int orient1, int top1, int bottom1, int left1, int right1, int header1, int footer1); __declspec(dllexport) int printout( int start1, int end1, int copies1); __declspec(dllexport) int savebook(char * string01); __declspec(dllexport) int savebooku(char * string01); __declspec(dllexport) int clssheet(); __declspec(dllexport) int clsbook(); __declspec(dllexport) int clsexcel(); __declspec(dllexport) int keisen01(int y1, int x1, int y2, int x2, int toplin, int topwei, int topr1, int topg1, int topb1, int bottomlin, int bottomwei, int bottomr1, int bottomg1, int bottomb1, int leftlin, int leftwei, int leftr1, int leftg1, int leftb1, int rightlin, int rightwei, int rightr1, int rightg1, int rightb1); __declspec(dllexport) int keisen02(int y1, int x1, int y2, int x2, int downlin, int uplin, int upwei, int upr1, int upg1, int upb1); __declspec(dllexport) char* cdirectory(char* string01); int ret = 0; unsigned int n = 0; char * param1; //gcroot cls; ref class refcls { public: static excelcs101::excelcscls^ cls; }; void loadcsdll() { refcls::cls = gcnew excelcs101::excelcscls(); } int loaddll() { try { loadcsdll(); } catch (Exception^ e) { return -1; } return 0; } int opnexcel(int flag) { return refcls::cls->opnexcel(flag); } int opnbook(char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->opnbook( (LPWSTR)param1); free(param1); return ret; } int opnsheet(char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->opnsheet( (LPWSTR)param1); free(param1); return ret; } char * rcell(int y1,int x1,char * string01) { char * ret01 = (char*)Marshal::StringToHGlobalAnsi(refcls::cls->rcell( y1,x1)).ToPointer(); unsigned int len = strlen(string01); n = strlen(ret01); if (len>=n) { strcpy_s(string01, len + 1, ret01); } else { strcpy_s(string01, len + 1, "バッファが足りません"); } Marshal::FreeHGlobal((IntPtr)ret01); return string01; } char * rcellu(int y1,int x1,char * string01) { wchar_t * ret01 = (wchar_t *)Marshal::StringToHGlobalUni(refcls::cls->rcell( y1,x1)).ToPointer(); n = WideCharToMultiByte(CP_UTF8, 0, ret01, -1, NULL, 0, NULL, NULL); param1 = (char *)calloc(1, n); WideCharToMultiByte(CP_UTF8, 0, ret01, -1, param1, n, NULL, NULL); unsigned int len = strlen(string01); n = strlen(param1); if (len>=n) { strcpy_s(string01, len + 1, param1); } else { strcpy_s(string01, len + 1, "バッファが足りません"); } Marshal::FreeHGlobal((IntPtr)ret01); free(param1); return string01; } char * rcellfx(int y1,int x1,char * string01) { char * ret01 = (char*)Marshal::StringToHGlobalAnsi(refcls::cls->rcellfx( y1,x1)).ToPointer(); unsigned int len = strlen(string01); n = strlen(ret01); if (len>=n) { strcpy_s(string01, len + 1, ret01); } else { strcpy_s(string01, len + 1, "バッファが足りません"); } Marshal::FreeHGlobal((IntPtr)ret01); return string01; } int wcellval(int y1,int x1,int int01) { ret = refcls::cls->wcellval( y1, x1, int01); return ret; } int wcellstr(int y1,int x1,char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->wcellstr( y1, x1, (LPWSTR)param1); free(param1); return ret; } int wcellstru(int y1,int x1,char * string01) { n = MultiByteToWideChar(CP_UTF8, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar( CP_UTF8, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->wcellstr( y1, x1, (LPWSTR)param1); free(param1); return ret; } int wcellfx(int y1,int x1,char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->wcellfx( y1, x1, (LPWSTR)param1); free(param1); return ret; } int cellattr( int y1, int x1, int h1, int w1, char * string01, int size1, int r1, int g1, int b1, int r2, int g2, int b2, int valign, int halign) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar( CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->cellattr( y1, x1, h1, w1, (LPWSTR)param1, size1, r1, g1, b1, r2, g2, b2, valign, halign); free(param1); return ret; } int cellmrg( int y1, int x1, int y2, int x2) { ret = refcls::cls->cellmrg( y1, x1, y2, x2); return ret; } int setprint(int y1, int x1, int y2, int x2, int size1, int orient1, int top1, int bottom1, int left1, int right1, int header1, int footer1) { ret = refcls::cls->setprint( y1, x1, y2, x2, size1, orient1, top1, bottom1, left1, right1, header1, footer1); return ret; } int printout( int start1, int end1, int copies1) { ret = refcls::cls->printout( start1, end1, copies1); return ret; } int savebook(char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->savebook( (LPWSTR)param1); free(param1); return ret; } int savebooku(char * string01) { n = MultiByteToWideChar(CP_ACP, 0, string01, -1, NULL, 0); param1 = (char *)calloc(1,n*2); MultiByteToWideChar(CP_ACP, 0, string01, -1, (LPWSTR)param1, n); ret = refcls::cls->savebooku( (LPWSTR)param1); free(param1); return ret; } int clssheet() { ret = refcls::cls->clssheet(); return ret; } int clsbook() { ret = refcls::cls->clsbook(); return ret; } int clsexcel() { ret = refcls::cls->clsexcel(); return ret; } int keisen01(int y1, int x1, int y2, int x2, int toplin, int topwei, int topr1, int topg1, int topb1, int bottomlin, int bottomwei, int bottomr1, int bottomg1, int bottomb1, int leftlin, int leftwei, int leftr1, int leftg1, int leftb1, int rightlin, int rightwei, int rightr1, int rightg1, int rightb1) { ret = refcls::cls->keisen01(y1, x1, y2, x2, toplin, topwei, topr1, topg1, topb1, bottomlin, bottomwei, bottomr1, bottomg1, bottomb1, leftlin, leftwei, leftr1, leftg1, leftb1, rightlin, rightwei, rightr1, rightg1, rightb1); return ret; } int keisen02(int y1, int x1, int y2, int x2, int downlin, int uplin, int upwei, int upr1, int upg1, int upb1) { ret = refcls::cls->keisen02(y1, x1, y2, x2, downlin, uplin, upwei, upr1, upg1, upb1); return ret; } char* cdirectory(char* string01) { char* ret01 = (char*)Marshal::StringToHGlobalAnsi(refcls::cls->cdirectory()).ToPointer(); unsigned int len = strlen(string01); n = strlen(ret01); if (len >= n) { strcpy_s(string01, len + 1, ret01); } else { strcpy_s(string01, len + 1, "バッファが足りません"); } Marshal::FreeHGlobal((IntPtr)ret01); return string01; } }