From 413c6c2f2928366b07322bc9ecd0c73126917482 Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Thu, 19 Dec 2019 17:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9E=E5=AE=9A=E8=BD=A6=E5=95=86=E6=96=B9?= =?UTF-8?q?=E6=A1=88=E8=A1=A8=E8=AF=BB=E5=8F=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../car_dealer_util/car_dealer_util.vcxproj | 4 + .../car_dealer_util.vcxproj.filters | 18 ++ .../DataManipulation/Excel/LoadFromExcel.cpp | 60 +++++-- .../DataManipulation/Excel/LoadFromExcel.h | 12 +- .../CarDealerScheme/CarDealerScheme.h | 16 +- .../source/data/excel/excel.cpp | 157 ++++++++++++++++++ .../car_dealer_util/source/data/excel/excel.h | 31 ++++ 代码/cpp/car_dealer_util/source/main.cpp | 5 + .../cpp/car_dealer_util/source/test/test.cpp | 24 +++ 代码/cpp/car_dealer_util/source/test/test.h | 4 + 10 files changed, 301 insertions(+), 30 deletions(-) create mode 100644 代码/cpp/car_dealer_util/source/data/excel/excel.cpp create mode 100644 代码/cpp/car_dealer_util/source/data/excel/excel.h create mode 100644 代码/cpp/car_dealer_util/source/test/test.cpp create mode 100644 代码/cpp/car_dealer_util/source/test/test.h diff --git a/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj b/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj index d3e6d3d..b19789d 100644 --- a/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj +++ b/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj @@ -21,7 +21,9 @@ + + @@ -38,6 +40,8 @@ + + {B12702AD-ABFB-343A-A199-8E24837244A3} diff --git a/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj.filters b/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj.filters index a94ec87..8b33381 100644 --- a/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj.filters +++ b/代码/cpp/car_dealer_util/proj/vs2019/car_dealer_util/car_dealer_util.vcxproj.filters @@ -50,6 +50,12 @@ {849ef05c-ca3c-479b-ad38-3b9eae188820} + + {c6a0a5fd-26f9-4d8d-84a5-ce7edb04eb5f} + + + {61d5dd61-6b1f-4507-bb22-4ce463ad5b85} + @@ -70,6 +76,12 @@ 数据\数据管理\excel + + 测试 + + + excel + @@ -109,5 +121,11 @@ 数据\数据管理\excel + + 测试 + + + excel + \ No newline at end of file diff --git a/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.cpp b/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.cpp index f0754e6..3bea9bc 100644 --- a/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.cpp +++ b/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.cpp @@ -1,30 +1,21 @@ #include -#include -#include "LoadFromExcel.h" #include +#include "LoadFromExcel.h" +#include "../../excel/excel.h" using namespace std; using namespace libxl; -void SetKey( Book * pBook ) -{ - if ( pBook == nullptr ) - { - return; - } - - pBook->setKey( L"cpic", L"windows-202d21040bc4e70060bc6264a6ucu7i1" ); -} /************************************************ * \brief 从Excel文件读取车商方案表 * \param filePath Excel文件路径 * \param schemeMap 存放车商方案数据的map ************************************************/ -void LoadCarDealerSchemeFromXlsx( const wstring & filePath, - unsigned int sheetIndex, - unsigned int firstRowIndex, - map & schemeMap ) +void LoadCarDealerSchemeFromXlsx( const wstring & filePath, + unsigned int sheetIndex, + unsigned int startRowIndex, + vector & schemeVector ) { Book * pBook = xlCreateXMLBookW(); Sheet * pSheet = nullptr; @@ -34,7 +25,7 @@ void LoadCarDealerSchemeFromXlsx( const wstring & filePath, throw runtime_error( "libxl库加载失败!" ); } - SetKey( pBook ); + setKey( pBook ); if ( pBook->load( filePath.c_str() ) != true ) { @@ -51,8 +42,43 @@ void LoadCarDealerSchemeFromXlsx( const wstring & filePath, string errorMessage = "读取sheet失败!"; errorMessage.append( pBook->errorMessage() ); + pBook->release(); + throw runtime_error( errorMessage ); } - + int lastRowIndex = pSheet->lastRow(); + int firstRowIndex = pSheet->firstRow(); + int firstColumnIndex = pSheet->firstCol(); + int rowIndex = firstRowIndex + startRowIndex; + + while ( rowIndex <= lastRowIndex ) + { + int colunmIndex = firstRowIndex; + + const wstring && theYear = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex, true ); + const wstring && theMonth = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 1, true ); + const wstring && carDealerCode = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 2, true ); + const wstring && carDealerName = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 3, true ); + const wstring && manHourPrice = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 4, true ); + const wstring && partPrice = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 5, true ); + const wstring && claimSupport = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 6, true ); + const wstring && scheme = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 7, true ); + const wstring && isQualified = ReadCellStringFromXlsx( pBook, sheetIndex, rowIndex, colunmIndex + 8, true ); + + CarDealerScheme carDealerScheme( theYear, + theMonth, + carDealerCode, + manHourPrice, + partPrice, + claimSupport, + scheme, + isQualified ); + + schemeVector.push_back( carDealerScheme ); + + rowIndex++; + } + + pBook->release(); } diff --git a/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.h b/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.h index 74a85ba..9d69edb 100644 --- a/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.h +++ b/代码/cpp/car_dealer_util/source/Data/DataManipulation/Excel/LoadFromExcel.h @@ -1,9 +1,11 @@ - -#pragma once +#pragma once -#include -#include +#include +#include #include "../../Datastructure/CarDealerScheme/CarDealerScheme.h" #include "../../Datastructure/CarDealerAchievement/CarDealerAchievement.h" - +void LoadCarDealerSchemeFromXlsx( const std::wstring & filePath, + unsigned int sheetIndex, + unsigned int startRowIndex, + std::vector & schemeVector ); diff --git a/代码/cpp/car_dealer_util/source/Data/Datastructure/CarDealerScheme/CarDealerScheme.h b/代码/cpp/car_dealer_util/source/Data/Datastructure/CarDealerScheme/CarDealerScheme.h index 0103d66..63f9bc9 100644 --- a/代码/cpp/car_dealer_util/source/Data/Datastructure/CarDealerScheme/CarDealerScheme.h +++ b/代码/cpp/car_dealer_util/source/Data/Datastructure/CarDealerScheme/CarDealerScheme.h @@ -59,14 +59,14 @@ public: } CarDealerScheme( CarDealerScheme && other ) - : theYear( std::move(other.theYear) ), - theMonth( std::move(other.theMonth) ), - carDealerCode( std::move(other.carDealerCode) ), - manHourPrice( std::move(other.manHourPrice) ), - partPrice( std::move(other.partPrice) ), - claimSupport( std::move(other.claimSupport) ), - scheme( std::move(other.scheme) ), - isQualified( std::move(other.isQualified) ) + : theYear( std::move( other.theYear ) ), + theMonth( std::move( other.theMonth ) ), + carDealerCode( std::move( other.carDealerCode ) ), + manHourPrice( std::move( other.manHourPrice ) ), + partPrice( std::move( other.partPrice ) ), + claimSupport( std::move( other.claimSupport ) ), + scheme( std::move( other.scheme ) ), + isQualified( std::move( other.isQualified ) ) { } diff --git a/代码/cpp/car_dealer_util/source/data/excel/excel.cpp b/代码/cpp/car_dealer_util/source/data/excel/excel.cpp new file mode 100644 index 0000000..f95cc93 --- /dev/null +++ b/代码/cpp/car_dealer_util/source/data/excel/excel.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include "excel.h" + +using namespace std; +using namespace libxl; + +void setKey( libxl::Book * pBook ) +{ + if ( pBook != nullptr ) + { + pBook->setKey( L"cpic", L"windows-202d21040bc4e70060bc6264a6ucu7i1" ); + } +} + +std::wstring ReadCellStringFromXlsx( libxl::IBookT * pBook, + unsigned int sheetIndex, + unsigned int rowIndex, + unsigned int colIndex, + bool isInteger ) +{ + using namespace libxl; + + std::wstring returnValue; + + int year = 0; + int month = 0; + int day = 0; + int hour = 0; + int min = 0; + int second = 0; + + //验证 + if ( pBook == nullptr ) + { + throw std::logic_error( "ReadCellStringFromXlsx pSheet参数错误!" ); + } + + ISheetT * pSheet = pBook->getSheet( sheetIndex ); + CellType cellType = pSheet->cellType( rowIndex, colIndex ); + + //判断单元格类型 + switch ( cellType ) + { + case CellType::CELLTYPE_BOOLEAN: //bool类型 + { + returnValue = pSheet->readBool( rowIndex, colIndex ) ? L"true" : L"false"; + + break; + } + case CellType::CELLTYPE_NUMBER: //数字类型,还得再判断一下是不是日期类型 + { + double cellValue = pSheet->readNum( rowIndex, colIndex ); + wchar_t buffer[50]; //生成字符串的缓冲区 + + if ( pSheet->isDate( rowIndex, colIndex ) == true ) + { + //是日期类型 + if ( pBook->dateUnpack( cellValue, + &year, + &month, + &day, + &hour, + &min, + &second ) == false ) + { + throw std::runtime_error( "转换日期格式失败!" ); + } + + //排除bug + if ( min >= 60 ) + { + hour = hour + 1; + min = min - 60; + } + + // if ( hour >= 24 ) + // { + // day = day + 1; + // hour = hour - 24; + // } + // + // switch ( month ) + // { + // case 1: + // case 3: + // case + // } + + wsprintfW( buffer, L"%d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, day, hour, min, second ); + } + else + { + //是数字类型, 根据isInteger参数选择输出整型或者浮点型 + if ( isInteger == true ) + { + wsprintfW( buffer, L"%d", static_cast(cellValue) ); + } + else + { + wsprintfW( buffer, L"%f", cellValue ); + } + } + + returnValue = buffer; + + break; + } + case CELLTYPE_STRING: //字符串类型,要先判断一下读取的结果是不是空值 + { + const wchar_t * pValue = pSheet->readStr( rowIndex, colIndex ); + + if ( pValue != nullptr ) + { + returnValue = pValue; + } + + break; + } + } + + return returnValue; +} + +libxl::Sheet * getXlsxSheetByName( libxl::IBookT * pBook, const std::wstring & sheetName ) +{ + Sheet * pSheet = nullptr; + Sheet * pCurrentSheet = nullptr; + + if ( pBook == nullptr ) + { + throw logic_error( "参数错误!" ); + } + + int sheetCount = pBook->sheetCount(); + int sheetIndex = 0; + + while ( sheetIndex < sheetCount ) + { + pCurrentSheet = pBook->getSheet( sheetIndex ); + + wstring currentSheetName = pCurrentSheet->name(); + + if ( currentSheetName == sheetName ) + { + pSheet = pCurrentSheet; + + break; + } + + sheetIndex++; + } + + return pSheet; +} diff --git a/代码/cpp/car_dealer_util/source/data/excel/excel.h b/代码/cpp/car_dealer_util/source/data/excel/excel.h new file mode 100644 index 0000000..43738c7 --- /dev/null +++ b/代码/cpp/car_dealer_util/source/data/excel/excel.h @@ -0,0 +1,31 @@ +#pragma once +#ifndef EXCEL_H_ +#define EXCEL_H_ + +#include + +void setKey( libxl::Book * pBook ); + +/************************************************ +* \brief 从xlsx文件单元格中读取数据,以字符串为返回值。 +* \param pBook libxl的Book对象。 +* \param sheetIndex sheet的索引值 +* \param rowIndex 行号 +* \param colIndex 列号 +* \return 返回的字符串 +************************************************/ +std::wstring ReadCellStringFromXlsx( libxl::IBookT * pBook, + unsigned int sheetIndex, + unsigned int rowIndex, + unsigned int colIndex, + bool isInteger ); + +/************************************************ +* \brief 通过名称获取sheet +* \param sheetName +* \return +************************************************/ +libxl::Sheet * getXlsxSheetByName( libxl::IBookT * pBook, const std::wstring & sheetName ); + + +#endif diff --git a/代码/cpp/car_dealer_util/source/main.cpp b/代码/cpp/car_dealer_util/source/main.cpp index 4172191..e71eb52 100644 --- a/代码/cpp/car_dealer_util/source/main.cpp +++ b/代码/cpp/car_dealer_util/source/main.cpp @@ -1,6 +1,7 @@ #include #include #include "Widgets/MainFrame/QMainFrame.h" +#include "test/test.h" int main( int argc, char * argv[] ) { @@ -21,6 +22,10 @@ int main( int argc, char * argv[] ) return -1; } + test(); + + return 1; + try { pMainFrame = new QMainFrame(); diff --git a/代码/cpp/car_dealer_util/source/test/test.cpp b/代码/cpp/car_dealer_util/source/test/test.cpp new file mode 100644 index 0000000..66abe0f --- /dev/null +++ b/代码/cpp/car_dealer_util/source/test/test.cpp @@ -0,0 +1,24 @@ + +#include +#include +#include "test.h" +#include "../data/DataManipulation/Excel/LoadFromExcel.h" + +using namespace std; + +void excelTest(); + +void test() +{ + excelTest(); +} + +void excelTest() +{ + vector schemeVector; + wstring filePath = L"D:/develop/projects_win/2019/car_dealer_util/数据/PC端导入模板(管理员版).xlsx"; + + LoadCarDealerSchemeFromXlsx(filePath, 0, 1, schemeVector); + + return; +} \ No newline at end of file diff --git a/代码/cpp/car_dealer_util/source/test/test.h b/代码/cpp/car_dealer_util/source/test/test.h new file mode 100644 index 0000000..4b172a2 --- /dev/null +++ b/代码/cpp/car_dealer_util/source/test/test.h @@ -0,0 +1,4 @@ + +#pragma once + +void test(); \ No newline at end of file