diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3c3467 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.vs +/x64 +/xlnt +/iKXiao/x64 +/Test_iKXiao/x64 diff --git a/Test_iKXiao/Test_iKXiao.cpp b/Test_iKXiao/Test_iKXiao.cpp new file mode 100644 index 0000000..a92ec0f --- /dev/null +++ b/Test_iKXiao/Test_iKXiao.cpp @@ -0,0 +1,39 @@ +// Test_iKXiao.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include +#include "..\iKXiao\iKXiao.h" + +int main() +{ + IK_WORKBOOK wb = iKXiao::Ins().OpenExcel("E:\\admin.xlsx", NULL); + + size_t rowCount = iKXiao::Ins().GetRowCount(wb, IDX_SHEET_CUR); + for (size_t r = 0; r < rowCount; r++) + { + size_t columnCount = 0; + char* rowData = iKXiao::Ins().GetRowStringArray(wb, IDX_SHEET_CUR, r, &columnCount); + size_t offset = 0; + for (size_t c = 0; c < columnCount; c++) + { + std::cout << rowData + offset << ","; + offset += strlen(rowData + offset) + 1; + } + iKXiao::Ins().FreeString(wb, rowData); + std::cout << std::endl; + } + + + char* cellStr = iKXiao::Ins().GetCellStringValue(wb, IDX_SHEET_CUR, 0, 0); + char* cellStr1 = iKXiao::Ins().GetCellStringValueByRefName(wb, IDX_SHEET_CUR, "A1"); + + iKXiao::Ins().SetCellStringValue(wb, IDX_SHEET_CUR, 0, 0, "我爱你mensong"); + iKXiao::Ins().SetCellFormula(wb, IDX_SHEET_CUR, 0, 1, "=SUM(1,2,3)"); + auto type = iKXiao::Ins().GetCellType(wb, IDX_SHEET_CUR, 0, 1); + char* formulaValue = iKXiao::Ins().GetCellStringValue(wb, IDX_SHEET_CUR, 0, 1); + + iKXiao::Ins().SaveExcel(wb, "E:\\admin1.xlsx", NULL); + + iKXiao::Ins().CloseExcel(wb); + return 0; +} diff --git a/Test_iKXiao/Test_iKXiao.vcxproj b/Test_iKXiao/Test_iKXiao.vcxproj new file mode 100644 index 0000000..bf2f811 --- /dev/null +++ b/Test_iKXiao/Test_iKXiao.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ada3d6d0-32a5-479e-a93e-4624bf1cad74} + TestiKXiao + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Test_iKXiao/Test_iKXiao.vcxproj.filters b/Test_iKXiao/Test_iKXiao.vcxproj.filters new file mode 100644 index 0000000..26002a4 --- /dev/null +++ b/Test_iKXiao/Test_iKXiao.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Test_iKXiao/Test_iKXiao.vcxproj.user b/Test_iKXiao/Test_iKXiao.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/Test_iKXiao/Test_iKXiao.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/iKXiao.sln b/iKXiao.sln new file mode 100644 index 0000000..d4971bd --- /dev/null +++ b/iKXiao.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33723.286 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "iKXiao", "iKXiao\iKXiao.vcxproj", "{75952845-AE0E-4539-A262-67ED4820B17D}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test_iKXiao", "Test_iKXiao\Test_iKXiao.vcxproj", "{ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {75952845-AE0E-4539-A262-67ED4820B17D}.Debug|x64.ActiveCfg = Debug|x64 + {75952845-AE0E-4539-A262-67ED4820B17D}.Debug|x64.Build.0 = Debug|x64 + {75952845-AE0E-4539-A262-67ED4820B17D}.Debug|x86.ActiveCfg = Debug|Win32 + {75952845-AE0E-4539-A262-67ED4820B17D}.Debug|x86.Build.0 = Debug|Win32 + {75952845-AE0E-4539-A262-67ED4820B17D}.Release|x64.ActiveCfg = Release|x64 + {75952845-AE0E-4539-A262-67ED4820B17D}.Release|x64.Build.0 = Release|x64 + {75952845-AE0E-4539-A262-67ED4820B17D}.Release|x86.ActiveCfg = Release|Win32 + {75952845-AE0E-4539-A262-67ED4820B17D}.Release|x86.Build.0 = Release|Win32 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Debug|x64.ActiveCfg = Debug|x64 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Debug|x64.Build.0 = Debug|x64 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Debug|x86.ActiveCfg = Debug|Win32 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Debug|x86.Build.0 = Debug|Win32 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Release|x64.ActiveCfg = Release|x64 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Release|x64.Build.0 = Release|x64 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Release|x86.ActiveCfg = Release|Win32 + {ADA3D6D0-32A5-479E-A93E-4624BF1CAD74}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {22FB2136-5D93-4B2D-B286-4CF1A4B25888} + EndGlobalSection +EndGlobal diff --git a/iKXiao/dllmain.cpp b/iKXiao/dllmain.cpp new file mode 100644 index 0000000..daed8c8 --- /dev/null +++ b/iKXiao/dllmain.cpp @@ -0,0 +1,19 @@ +// dllmain.cpp : 定义 DLL 应用程序的入口点。 +#include "pch.h" + +BOOL APIENTRY DllMain( HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} + diff --git a/iKXiao/framework.h b/iKXiao/framework.h new file mode 100644 index 0000000..80cbbc9 --- /dev/null +++ b/iKXiao/framework.h @@ -0,0 +1,5 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容 +// Windows 头文件 +#include diff --git a/iKXiao/iKXiao.cpp b/iKXiao/iKXiao.cpp new file mode 100644 index 0000000..aa6afa5 --- /dev/null +++ b/iKXiao/iKXiao.cpp @@ -0,0 +1,1073 @@ +#include "pch.h" +#include "iKXiao.h" +#include +#include +#include +#include + +class iKXiaoImp +{ +public: + iKXiaoImp() + { + + } + ~iKXiaoImp() + { + for (auto it = m_stringCache.begin(); it != m_stringCache.end(); ++it) + { + delete[] (*it); + } + m_stringCache.clear(); + } + + IK_WORKBOOK Open(const char* xlsxFilepath, const char* password) + { + //set data + m_filepath = xlsxFilepath; + if (password && password[0] != '\0') + { + m_password = password; + } + + try + { + //if the file path exist,load the file to workbook + if (IsPathExist(xlsxFilepath)) + { + if (m_password.empty()) + m_workbook.load(xlnt::path(xlsxFilepath)); + else + m_workbook.load(xlnt::path(xlsxFilepath), m_password); + } + } + catch (const std::exception&) + { + return NULL; + } + + return this; + } + + int GetSheetCount() + { + return (int)m_workbook.sheet_count(); + } + + int GetSheetIndexByTitle(const char* sheetTitle) + { + auto titles = m_workbook.sheet_titles(); + for (size_t i = 0; i < titles.size(); i++) + { + if (titles[i] == sheetTitle) + return i; + } + return -1; + } + + char* GetSheetTitle(int sheetIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return moveStringBuffer(sheet.title()); + } + catch (const std::exception&) + { + return NULL; + } + } + + bool SetCurrentSheet(int sheetIndex) + { + if (sheetIndex < 0 && sheetIndex >= GetSheetCount()) + return false; + + try + { + m_workbook.active_sheet(sheetIndex); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + //atIndex==-1ʱ󴴽 + int CreateSheet(int atIndex = -1) + { + try + { + if (atIndex < 0) + { + m_workbook.create_sheet(); + return GetSheetCount() - 1; + } + else + { + m_workbook.create_sheet((size_t)atIndex); + return atIndex; + } + } + catch (const std::exception&) + { + return -1; + } + } + + int CopySheet(int srcIndex, int atIndex = -1) + { + try + { + auto sheet = getSheetByIndex(srcIndex); + + if (atIndex < 0) + { + m_workbook.copy_sheet(sheet); + return GetSheetCount() - 1; + } + else + { + m_workbook.copy_sheet(sheet, (size_t)atIndex); + return atIndex; + } + } + catch (const std::exception&) + { + return -1; + } + } + + bool RemoveSheet(int sheetIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + m_workbook.remove_sheet(sheet); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetSheetTitle(int sheetIndex, const char* title) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.title(title); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + size_t GetRowCount(int sheetIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + auto rows = sheet.rows(false); + return rows.length(); + } + catch (const std::exception&) + { + return 0; + } + } + + char* GetRowStringArray(int sheetIndex, size_t rowIndex, size_t* columnCount) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + auto rows = sheet.rows(false); + if (rowIndex >= rows.length()) + { + columnCount = 0; + return NULL; + } + auto row = rows[rowIndex]; + std::vector rowData; + size_t totalSize = 0; + for (auto cell : row) + { + rowData.push_back(Utf8ToAnsi(cell.to_string())); + totalSize += rowData[rowData.size() - 1].size() + 1; + } + char* totalBuff = allocStringBuffer(totalSize); + size_t start = 0; + for (size_t i = 0; i < rowData.size(); i++) + { + strcpy_s(totalBuff + start, totalSize - start, rowData[i].c_str()); + (totalBuff + start)[rowData[i].size()] = '\0'; + start += rowData[i].size() + 1; + } + *columnCount = rowData.size(); + return totalBuff; + } + catch (const std::exception&) + { + columnCount = 0; + return NULL; + } + } + + CellType GetCellType(int sheetIndex, size_t rowIndex, size_t columnIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + auto cell = sheet.cell(xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1)); + return (CellType)cell.data_type(); + } + catch (const std::exception&) + { + return CellType::empty_value; + } + } + + char* GetCellStringValue(int sheetIndex, size_t rowIndex, size_t columnIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + auto cell = sheet.cell(xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1)); + return moveStringBuffer(Utf8ToAnsi(cell.to_string())); + } + catch (const std::exception&) + { + return NULL; + } + } + + char* GetCellStringValueByRefName(int sheetIndex, const char* refName) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + auto cell = sheet.cell(refName); + return moveStringBuffer(Utf8ToAnsi(cell.to_string())); + } + catch (const std::exception&) + { + return NULL; + } + } + + bool SetCellStringValue(int sheetIndex, size_t rowIndex, size_t columnIndex, const char* strVal) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(AnsiToUtf8(strVal)); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellNullValue(int sheetIndex, size_t rowIndex, size_t columnIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).clear_value(); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellBoolValue(int sheetIndex, size_t rowIndex, size_t columnIndex, bool boolean_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(boolean_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool GetCellBoolValue(int sheetIndex, size_t rowIndex, size_t columnIndex, bool defVal = false) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, int int_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(int_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + int GetCellIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, int defVal = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellUIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int int_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(int_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + unsigned int GetCellUIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int defVal = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellLLIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, long long int int_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(int_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + long long int GetCellLLIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, long long int defVal = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellULLIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int int_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(int_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + unsigned long long int GetCellULLIntValue(int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int defVal = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellDoubleValue(int sheetIndex, size_t rowIndex, size_t columnIndex, double float_value) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(float_value); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + double GetCellDoubleValue(int sheetIndex, size_t rowIndex, size_t columnIndex, double defVal = 0.0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + return sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + } + catch (const std::exception&) + { + return defVal; + } + } + + bool SetCellDateValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int year_, int month_, int day_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::date d(year_, month_, day_); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(d); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool GetCellDateValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int& year_, int& month_, int& day_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::date d = sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + year_ = d.year; + month_ = d.month; + day_ = d.day; + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellDatetimeValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int year_, int month_, int day_, int hour_ = 0, int minute_ = 0, int second_ = 0, int microsecond_ = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::datetime d(year_, month_, day_, hour_, minute_, second_, microsecond_); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(d); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool GetCellDatetimeValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int& year_, int& month_, int& day_, int& hour_, int& minute_, int& second_, int& microsecond_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::datetime d = sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + year_ = d.year; + month_ = d.month; + day_ = d.day; + hour_ = d.hour; + minute_ = d.minute; + second_ = d.second; + microsecond_ = d.microsecond; + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellTimeValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int hour_ = 0, int minute_ = 0, int second_ = 0, int microsecond_ = 0) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::time t(hour_, minute_, second_, microsecond_); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(t); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool GetCellTimeValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int& hour_, int& minute_, int& second_, int& microsecond_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::time d = sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + hour_ = d.hour; + minute_ = d.minute; + second_ = d.second; + microsecond_ = d.microsecond; + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellTimeDeltaValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int days_, int hours_, int minutes_, int seconds_, int microseconds_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::timedelta t(days_, hours_, minutes_, seconds_, microseconds_); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(t); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool GetCellTimeDeltaValue(int sheetIndex, size_t rowIndex, size_t columnIndex, + int days_, int& hours_, int& minutes_, int& seconds_, int& microseconds_) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::timedelta d = sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).value(); + days_ = d.days; + hours_ = d.hours; + minutes_ = d.minutes; + seconds_ = d.seconds; + microseconds_ = d.microseconds; + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool SetCellFormula(int sheetIndex, size_t rowIndex, size_t columnIndex, const char* formula) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + sheet.cell( + xlnt::column_t(columnIndex + 1), xlnt::row_t(rowIndex + 1) + ).formula(formula); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool CopyCell(int sheetIndex, size_t fromRowIndex, size_t fromColumnIndex, size_t toRowIndex, size_t toColumnIndex) + { + try + { + auto sheet = getSheetByIndex(sheetIndex); + xlnt::cell c = sheet.cell( + xlnt::column_t(fromColumnIndex + 1), xlnt::row_t(fromRowIndex + 1)); + sheet.cell( + xlnt::column_t(toColumnIndex + 1), xlnt::row_t(toRowIndex + 1) + ).value(c); + return true; + } + catch (const std::exception&) + { + return false; + } + } + + bool Save(const char* filepath, const char* password) + { + try + { + if (!password || password[0] == '\0') + m_workbook.save(xlnt::path(filepath)); + else + m_workbook.save(xlnt::path(filepath), password); + return true; + } + catch (const std::exception&) + { + return false; + } + } + +public: + static bool IsPathExist(const char* path) + { + int nRet = _access(path, 0); + return 0 == nRet || EACCES == nRet; + } + + + //AnsiַתΪUnicodeַ + static std::wstring AnsiToUnicode(const std::string& multiByteStr) + { + wchar_t* pWideCharStr; //巵صĿַָ + int nLenOfWideCharStr; //ַעⲻֽ + const char* pMultiByteStr = multiByteStr.c_str(); + //ȡַĸ + nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, NULL, 0); + //ÿַָ + pWideCharStr = (wchar_t*)(HeapAlloc(GetProcessHeap(), 0, nLenOfWideCharStr * sizeof(wchar_t))); + MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, pWideCharStr, nLenOfWideCharStr); + // + std::wstring wideByteRet(pWideCharStr, nLenOfWideCharStr); + //ڴеַ + HeapFree(GetProcessHeap(), 0, pWideCharStr); + return wideByteRet.c_str(); + } + + //UnicodeַתΪAnsiַ + static std::string UnicodeToAnsi(const std::wstring& wideByteStr) + { + char* pMultiCharStr; //巵صĶַָ + int nLenOfMultiCharStr; //ַעⲻֽ + const wchar_t* pWideByteStr = wideByteStr.c_str(); + //ȡַĸ + nLenOfMultiCharStr = WideCharToMultiByte(CP_ACP, 0, pWideByteStr, -1, NULL, 0, NULL, NULL); + //öַָ + pMultiCharStr = (char*)(HeapAlloc(GetProcessHeap(), 0, nLenOfMultiCharStr * sizeof(char))); + WideCharToMultiByte(CP_ACP, 0, pWideByteStr, -1, pMultiCharStr, nLenOfMultiCharStr, NULL, NULL); + // + std::string sRet(pMultiCharStr, nLenOfMultiCharStr); + //ڴеַ + HeapFree(GetProcessHeap(), 0, pMultiCharStr); + return sRet.c_str(); + } + + static std::string UnicodeToUtf8(const std::wstring& wideByteStr) + { + int len = WideCharToMultiByte(CP_UTF8, 0, wideByteStr.c_str(), -1, NULL, 0, NULL, NULL); + char* szUtf8 = new char[len + 1]; + memset(szUtf8, 0, len + 1); + WideCharToMultiByte(CP_UTF8, 0, wideByteStr.c_str(), -1, szUtf8, len, NULL, NULL); + std::string s = szUtf8; + delete[] szUtf8; + return s.c_str(); + } + + static std::wstring Utf8ToUnicode(const std::string& utf8Str) + { + //ԤתõռĴС; + int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf8Str.c_str(), strlen(utf8Str.c_str()), NULL, 0); + //ռҪ'\0'ռ䣬MultiByteToWideChar'\0'ռ + wchar_t* wszString = new wchar_t[wcsLen + 1]; + //ת + ::MultiByteToWideChar(CP_UTF8, NULL, utf8Str.c_str(), strlen(utf8Str.c_str()), wszString, wcsLen); + //'\0' + wszString[wcsLen] = '\0'; + std::wstring s(wszString); + delete[] wszString; + return s; + } + + static std::string AnsiToUtf8(const std::string& multiByteStr) + { + std::wstring ws = AnsiToUnicode(multiByteStr); + return UnicodeToUtf8(ws); + } + + static std::string Utf8ToAnsi(const std::string& utf8Str) + { + std::wstring ws = Utf8ToUnicode(utf8Str); + return UnicodeToAnsi(ws); + } + +public: + + char* allocStringBuffer(int size) + { + char* buff = new char[size]; + m_stringCache.insert(buff); + return buff; + } + void freeStringBuffer(char* buff) + { + if (!buff) + return; + m_stringCache.erase(buff); + delete[] buff; + } + + char* moveStringBuffer(const std::string& innerStr) + { + char* buff = allocStringBuffer(innerStr.size() + 1); + memcpy(buff, &innerStr[0], innerStr.size()); + buff[innerStr.size()] = '\0'; + return buff; + } + + xlnt::worksheet getSheetByIndex(int sheetIndex) + { + try + { + switch (sheetIndex) + { + case -1: + return m_workbook.active_sheet(); + case -2: + return m_workbook.sheet_by_index(m_workbook.sheet_count() - 1); + default: + return m_workbook.sheet_by_index(sheetIndex); + break; + } + } + catch (const std::exception&) + { + return m_workbook.active_sheet(); + } + } + +private: + std::set m_stringCache; + +private: + xlnt::workbook m_workbook; + std::string m_filepath; + std::string m_password; +}; + +IKXIAO_API IK_WORKBOOK OpenExcel(const char* xlsxFilepath, const char* password) +{ + iKXiaoImp* imp = new iKXiaoImp(); + IK_WORKBOOK ret = imp->Open(xlsxFilepath, password); + if (!ret) + delete imp; + return ret; +} + +IKXIAO_API int GetSheetCount(IK_WORKBOOK workbook) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetSheetCount(); +} + +IKXIAO_API int GetSheetIndexByTitle(IK_WORKBOOK workbook, const char* sheetTitle) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetSheetIndexByTitle(sheetTitle); +} + +IKXIAO_API char* GetSheetTitle(IK_WORKBOOK workbook, int sheetIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetSheetTitle(sheetIndex); +} + +IKXIAO_API bool SetCurrentSheet(IK_WORKBOOK workbook, int sheetIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCurrentSheet(sheetIndex); +} + +IKXIAO_API int CreateSheet(IK_WORKBOOK workbook, int atIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->CreateSheet(atIndex); +} + +IKXIAO_API int CopySheet(IK_WORKBOOK workbook, int srcIndex, int atIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->CopySheet(srcIndex, atIndex); +} + +IKXIAO_API bool RemoveSheet(IK_WORKBOOK workbook, int sheetIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->RemoveSheet(sheetIndex); +} + +IKXIAO_API bool SetSheetTitle(IK_WORKBOOK workbook, int sheetIndex, const char* title) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetSheetTitle(sheetIndex, title); +} + +IKXIAO_API size_t GetRowCount(IK_WORKBOOK workbook, int sheetIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetRowCount(sheetIndex); +} + +IKXIAO_API char* GetRowStringArray(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t* columnCount) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetRowStringArray(sheetIndex, rowIndex, columnCount); +} + +IKXIAO_API CellType GetCellType(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellType(sheetIndex, rowIndex, columnIndex); +} + +IKXIAO_API char* GetCellStringValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellStringValue(sheetIndex, rowIndex, columnIndex); +} + +IKXIAO_API char* GetCellStringValueByRefName(IK_WORKBOOK workbook, int sheetIndex, const char* refName) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellStringValueByRefName(sheetIndex, refName); +} + +IKXIAO_API bool SetCellStringValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, const char* strVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellStringValue(sheetIndex, rowIndex, columnIndex, strVal); +} + +IKXIAO_API bool SetCellNullValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellNullValue(sheetIndex, rowIndex, columnIndex); +} + +IKXIAO_API bool SetCellBoolValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, bool boolean_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellBoolValue(sheetIndex, rowIndex, columnIndex, boolean_value); +} + +IKXIAO_API bool GetCellBoolValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, bool defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellBoolValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int int_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellIntValue(sheetIndex, rowIndex, columnIndex, int_value); +} + +IKXIAO_API int GetCellIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellIntValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellUIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int int_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellUIntValue(sheetIndex, rowIndex, columnIndex, int_value); +} + +IKXIAO_API unsigned int GetCellUIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellUIntValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellLLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, long long int int_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellLLIntValue(sheetIndex, rowIndex, columnIndex, int_value); +} + +IKXIAO_API long long int GetCellLLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, long long int defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellLLIntValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellULLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int int_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellULLIntValue(sheetIndex, rowIndex, columnIndex, int_value); +} + +IKXIAO_API unsigned long long int GetCellULLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellULLIntValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellDoubleValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, double float_value) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellDoubleValue(sheetIndex, rowIndex, columnIndex, float_value); +} + +IKXIAO_API double GetCellDoubleValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, double defVal) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellDoubleValue(sheetIndex, rowIndex, columnIndex, defVal); +} + +IKXIAO_API bool SetCellDateValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int year_, int month_, int day_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellDateValue(sheetIndex, rowIndex, columnIndex, year_, month_, day_); +} + +IKXIAO_API bool GetCellDateValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& year_, int& month_, int& day_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellDateValue(sheetIndex, rowIndex, columnIndex, year_, month_, day_); +} + +IKXIAO_API bool SetCellDatetimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int year_, int month_, int day_, int hour_, int minute_, int second_, int microsecond_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellDatetimeValue(sheetIndex, rowIndex, columnIndex, year_, month_, day_, hour_, minute_, second_, microsecond_); +} + +IKXIAO_API bool GetCellDatetimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& year_, int& month_, int& day_, int& hour_, int& minute_, int& second_, int& microsecond_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellDatetimeValue(sheetIndex, rowIndex, columnIndex, year_, month_, day_, hour_, minute_, second_, microsecond_); +} + +IKXIAO_API bool SetCellTimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int hour_, int minute_, int second_, int microsecond_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellTimeValue(sheetIndex, rowIndex, columnIndex, hour_, minute_, second_, microsecond_); +} + +IKXIAO_API bool GetCellTimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& hour_, int& minute_, int& second_, int& microsecond_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellTimeValue(sheetIndex, rowIndex, columnIndex, hour_, minute_, second_, microsecond_); +} + +IKXIAO_API bool SetCellTimeDeltaValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int days_, int hours_, int minutes_, int seconds_, int microseconds_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellTimeDeltaValue(sheetIndex, rowIndex, columnIndex, days_, hours_, minutes_, seconds_, microseconds_); +} + +IKXIAO_API bool GetCellTimeDeltaValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int days_, int& hours_, int& minutes_, int& seconds_, int& microseconds_) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->GetCellTimeDeltaValue(sheetIndex, rowIndex, columnIndex, days_, hours_, minutes_, seconds_, microseconds_); +} + +IKXIAO_API bool SetCellFormula(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, const char* formula) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->SetCellFormula(sheetIndex, rowIndex, columnIndex, formula); +} + +IKXIAO_API bool CopyCell(IK_WORKBOOK workbook, int sheetIndex, size_t fromRowIndex, size_t fromColumnIndex, size_t toRowIndex, size_t toColumnIndex) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->CopyCell(sheetIndex, fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex); +} + +IKXIAO_API bool SaveExcel(IK_WORKBOOK workbook, const char* filepath, const char* password) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + return imp->Save(filepath, password); +} + +IKXIAO_API void FreeString(IK_WORKBOOK workbook, char* buff) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + imp->freeStringBuffer(buff); +} + +IKXIAO_API void CloseExcel(IK_WORKBOOK workbook) +{ + iKXiaoImp* imp = (iKXiaoImp*)workbook; + delete imp; +} diff --git a/iKXiao/iKXiao.h b/iKXiao/iKXiao.h new file mode 100644 index 0000000..ae0481d --- /dev/null +++ b/iKXiao/iKXiao.h @@ -0,0 +1,228 @@ +#pragma once +#ifndef _AFX +#include +#endif +#include +#include + +#ifdef IKXIAO_EXPORTS +#define IKXIAO_API extern "C" __declspec(dllexport) +#else +#define IKXIAO_API extern "C" __declspec(dllimport) +#endif + +typedef void* IK_WORKBOOK; +//typedef void* IK_WORKSHEET; + +#define IDX_SHEET_CUR -1 +#define IDX_SHEET_LAST -2 + +enum CellType +{ + /// no value + empty_value, + /// value is TRUE or FALSE + boolean_value, + /// value is an ISO 8601 formatted date + date_value, + /// value is a known error code such as \#VALUE! + error_value, + /// value is a string stored in the cell + inline_string_value, + /// value is a number + number_value, + /// value is a string shared with other cells to save space + shared_string_value, + /// value is the string result of a formula + formula_string_value +}; + +IKXIAO_API IK_WORKBOOK OpenExcel(const char* xlsxFilepath, const char* password); +IKXIAO_API int GetSheetCount(IK_WORKBOOK workbook); +IKXIAO_API int GetSheetIndexByTitle(IK_WORKBOOK workbook, const char* sheetTitle); +IKXIAO_API char* GetSheetTitle(IK_WORKBOOK workbook, int sheetIndex); +IKXIAO_API bool SetCurrentSheet(IK_WORKBOOK workbook, int sheetIndex); +IKXIAO_API int CreateSheet(IK_WORKBOOK workbook, int atIndex = -1); +IKXIAO_API int CopySheet(IK_WORKBOOK workbook, int srcIndex, int atIndex = -1); +IKXIAO_API bool RemoveSheet(IK_WORKBOOK workbook, int sheetIndex); +IKXIAO_API bool SetSheetTitle(IK_WORKBOOK workbook, int sheetIndex, const char* title); +IKXIAO_API size_t GetRowCount(IK_WORKBOOK workbook, int sheetIndex); +//ݣݸʽΪ"1\02\03\0" +IKXIAO_API char* GetRowStringArray(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t* columnCount); +IKXIAO_API CellType GetCellType(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex); +IKXIAO_API char* GetCellStringValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex); +IKXIAO_API char* GetCellStringValueByRefName(IK_WORKBOOK workbook, int sheetIndex, const char* refName); +IKXIAO_API bool SetCellStringValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, const char* strVal); +IKXIAO_API bool SetCellNullValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex); +IKXIAO_API bool SetCellBoolValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, bool boolean_value); +IKXIAO_API bool GetCellBoolValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, bool defVal = false); +IKXIAO_API bool SetCellIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int int_value); +IKXIAO_API int GetCellIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int defVal = 0); +IKXIAO_API bool SetCellUIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int int_value); +IKXIAO_API unsigned int GetCellUIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned int defVal = 0); +IKXIAO_API bool SetCellLLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, long long int int_value); +IKXIAO_API long long int GetCellLLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, long long int defVal = 0); +IKXIAO_API bool SetCellULLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int int_value); +IKXIAO_API unsigned long long int GetCellULLIntValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, unsigned long long int defVal = 0); +IKXIAO_API bool SetCellDoubleValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, double float_value); +IKXIAO_API double GetCellDoubleValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, double defVal = 0.0); +IKXIAO_API bool SetCellDateValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int year_, int month_, int day_); +IKXIAO_API bool GetCellDateValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& year_, int& month_, int& day_); +IKXIAO_API bool SetCellDatetimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int year_, int month_, int day_, int hour_ = 0, int minute_ = 0, int second_ = 0, int microsecond_ = 0); +IKXIAO_API bool GetCellDatetimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& year_, int& month_, int& day_, int& hour_, int& minute_, int& second_, int& microsecond_); +IKXIAO_API bool SetCellTimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int hour_ = 0, int minute_ = 0, int second_ = 0, int microsecond_ = 0); +IKXIAO_API bool GetCellTimeValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int& hour_, int& minute_, int& second_, int& microsecond_); +IKXIAO_API bool SetCellTimeDeltaValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int days_, int hours_, int minutes_, int seconds_, int microseconds_); +IKXIAO_API bool GetCellTimeDeltaValue(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, int days_, int& hours_, int& minutes_, int& seconds_, int& microseconds_); +IKXIAO_API bool SetCellFormula(IK_WORKBOOK workbook, int sheetIndex, size_t rowIndex, size_t columnIndex, const char* formula); +IKXIAO_API bool CopyCell(IK_WORKBOOK workbook, int sheetIndex, size_t fromRowIndex, size_t fromColumnIndex, size_t toRowIndex, size_t toColumnIndex); +IKXIAO_API bool SaveExcel(IK_WORKBOOK workbook, const char* filepath, const char* password); +IKXIAO_API void FreeString(IK_WORKBOOK workbook, char* buff); +IKXIAO_API void CloseExcel(IK_WORKBOOK workbook); + +class iKXiao +{ +#define DEF_PROC(name) \ + decltype(::name)* name + +#define SET_PROC(hDll, name) \ + this->name = (decltype(::name)*)::GetProcAddress(hDll, #name) + +public: + iKXiao() + { + hDll = LoadLibraryFromCurrentDir("iKXiao.dll"); + if (!hDll) + return; + + SET_PROC(hDll, OpenExcel); + SET_PROC(hDll, GetSheetCount); + SET_PROC(hDll, GetSheetIndexByTitle); + SET_PROC(hDll, GetSheetTitle); + SET_PROC(hDll, SetCurrentSheet); + SET_PROC(hDll, CreateSheet); + SET_PROC(hDll, CopySheet); + SET_PROC(hDll, RemoveSheet); + SET_PROC(hDll, SetSheetTitle); + SET_PROC(hDll, GetRowCount); + SET_PROC(hDll, GetRowStringArray); + SET_PROC(hDll, GetCellType); + SET_PROC(hDll, GetCellStringValue); + SET_PROC(hDll, GetCellStringValueByRefName); + SET_PROC(hDll, SetCellStringValue); + SET_PROC(hDll, SetCellNullValue); + SET_PROC(hDll, SetCellBoolValue); + SET_PROC(hDll, GetCellBoolValue); + SET_PROC(hDll, SetCellIntValue); + SET_PROC(hDll, GetCellIntValue); + SET_PROC(hDll, SetCellUIntValue); + SET_PROC(hDll, GetCellUIntValue); + SET_PROC(hDll, SetCellLLIntValue); + SET_PROC(hDll, GetCellLLIntValue); + SET_PROC(hDll, SetCellULLIntValue); + SET_PROC(hDll, GetCellULLIntValue); + SET_PROC(hDll, SetCellDoubleValue); + SET_PROC(hDll, GetCellDoubleValue); + SET_PROC(hDll, SetCellDateValue); + SET_PROC(hDll, GetCellDateValue); + SET_PROC(hDll, SetCellDatetimeValue); + SET_PROC(hDll, GetCellDatetimeValue); + SET_PROC(hDll, SetCellTimeValue); + SET_PROC(hDll, GetCellTimeValue); + SET_PROC(hDll, SetCellTimeDeltaValue); + SET_PROC(hDll, GetCellTimeDeltaValue); + SET_PROC(hDll, SetCellFormula); + SET_PROC(hDll, CopyCell); + SET_PROC(hDll, SaveExcel); + SET_PROC(hDll, FreeString); + SET_PROC(hDll, CloseExcel); + } + + + DEF_PROC(OpenExcel); + DEF_PROC(GetSheetCount); + DEF_PROC(GetSheetIndexByTitle); + DEF_PROC(GetSheetTitle); + DEF_PROC(SetCurrentSheet); + DEF_PROC(CreateSheet); + DEF_PROC(CopySheet); + DEF_PROC(RemoveSheet); + DEF_PROC(SetSheetTitle); + DEF_PROC(GetRowCount); + DEF_PROC(GetRowStringArray); + DEF_PROC(GetCellType); + DEF_PROC(GetCellStringValue); + DEF_PROC(GetCellStringValueByRefName); + DEF_PROC(SetCellStringValue); + DEF_PROC(SetCellNullValue); + DEF_PROC(SetCellBoolValue); + DEF_PROC(GetCellBoolValue); + DEF_PROC(SetCellIntValue); + DEF_PROC(GetCellIntValue); + DEF_PROC(SetCellUIntValue); + DEF_PROC(GetCellUIntValue); + DEF_PROC(SetCellLLIntValue); + DEF_PROC(GetCellLLIntValue); + DEF_PROC(SetCellULLIntValue); + DEF_PROC(GetCellULLIntValue); + DEF_PROC(SetCellDoubleValue); + DEF_PROC(GetCellDoubleValue); + DEF_PROC(SetCellDateValue); + DEF_PROC(GetCellDateValue); + DEF_PROC(SetCellDatetimeValue); + DEF_PROC(GetCellDatetimeValue); + DEF_PROC(SetCellTimeValue); + DEF_PROC(GetCellTimeValue); + DEF_PROC(SetCellTimeDeltaValue); + DEF_PROC(GetCellTimeDeltaValue); + DEF_PROC(SetCellFormula); + DEF_PROC(CopyCell); + DEF_PROC(SaveExcel); + DEF_PROC(FreeString); + DEF_PROC(CloseExcel); + + +public: + static iKXiao& Ins() + { + static iKXiao s_ins; + return s_ins; + } + + static HMODULE LoadLibraryFromCurrentDir(const char* dllName) + { + char selfPath[MAX_PATH]; + MEMORY_BASIC_INFORMATION mbi; + HMODULE hModule = ((::VirtualQuery(LoadLibraryFromCurrentDir, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL); + ::GetModuleFileNameA(hModule, selfPath, MAX_PATH); + std::string moduleDir(selfPath); + size_t idx = moduleDir.find_last_of('\\'); + moduleDir = moduleDir.substr(0, idx); + std::string modulePath = moduleDir + "\\" + dllName; + char curDir[MAX_PATH]; + ::GetCurrentDirectoryA(MAX_PATH, curDir); + ::SetCurrentDirectoryA(moduleDir.c_str()); + HMODULE hDll = LoadLibraryA(modulePath.c_str()); + ::SetCurrentDirectoryA(curDir); + if (!hDll) + { + DWORD err = ::GetLastError(); + char buf[10]; + sprintf_s(buf, "%u", err); + ::MessageBoxA(NULL, ("Ҳ" + modulePath + "ģ:" + buf).c_str(), "Ҳģ", MB_OK | MB_ICONERROR); + } + return hDll; + } + ~iKXiao() + { + if (hDll) + { + FreeLibrary(hDll); + hDll = NULL; + } + } + +private: + HMODULE hDll; +}; + diff --git a/iKXiao/iKXiao.vcxproj b/iKXiao/iKXiao.vcxproj new file mode 100644 index 0000000..de4a070 --- /dev/null +++ b/iKXiao/iKXiao.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {75952845-ae0e-4539-a262-67ed4820b17d} + iKXiao + 10.0 + + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;IKXIAO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + $(SolutionDir)xlnt\include;%(AdditionalIncludeDirectories) + MultiThreadedDebug + + + Windows + true + false + $(SolutionDir)xlnt\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + xlntd.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;IKXIAO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + $(SolutionDir)xlnt\include;%(AdditionalIncludeDirectories) + MultiThreaded + + + Windows + true + true + true + false + $(SolutionDir)xlnt\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + xlnt.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;IKXIAO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + $(SolutionDir)xlnt\include;%(AdditionalIncludeDirectories) + MultiThreadedDebug + + + Windows + true + false + $(SolutionDir)xlnt\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + xlntd.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;IKXIAO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + $(SolutionDir)xlnt\include;%(AdditionalIncludeDirectories) + MultiThreaded + + + Windows + true + true + true + false + $(SolutionDir)xlnt\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + xlnt.lib;%(AdditionalDependencies) + + + + + + + + + + + + Create + Create + Create + Create + + + + + + \ No newline at end of file diff --git a/iKXiao/iKXiao.vcxproj.filters b/iKXiao/iKXiao.vcxproj.filters new file mode 100644 index 0000000..9bb62cc --- /dev/null +++ b/iKXiao/iKXiao.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + 头文件 + + + 头文件 + + + + + 源文件 + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git a/iKXiao/iKXiao.vcxproj.user b/iKXiao/iKXiao.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/iKXiao/iKXiao.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/iKXiao/pch.cpp b/iKXiao/pch.cpp new file mode 100644 index 0000000..b6fb8f4 --- /dev/null +++ b/iKXiao/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件 + +#include "pch.h" + +// 当使用预编译的头时,需要使用此源文件,编译才能成功。 diff --git a/iKXiao/pch.h b/iKXiao/pch.h new file mode 100644 index 0000000..9660927 --- /dev/null +++ b/iKXiao/pch.h @@ -0,0 +1,13 @@ +// pch.h: 这是预编译标头文件。 +// 下方列出的文件仅编译一次,提高了将来生成的生成性能。 +// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。 +// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。 +// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。 + +#ifndef PCH_H +#define PCH_H + +// 添加要在此处预编译的标头 +#include "framework.h" + +#endif //PCH_H diff --git a/xlnt.zip b/xlnt.zip new file mode 100644 index 0000000..05c0dcb Binary files /dev/null and b/xlnt.zip differ