搞定车商方案表读取。

This commit is contained in:
Kane Wang 2019-12-19 17:10:29 +08:00
parent 57194b10ea
commit 413c6c2f29
10 changed files with 301 additions and 30 deletions

View File

@ -21,7 +21,9 @@
<ClCompile Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.cpp" /> <ClCompile Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.cpp" />
<ClCompile Include="..\..\..\source\Data\Datastructure\CarDealerAchievement\CarDealerAchievement.cpp" /> <ClCompile Include="..\..\..\source\Data\Datastructure\CarDealerAchievement\CarDealerAchievement.cpp" />
<ClCompile Include="..\..\..\source\Data\Datastructure\CarDealerScheme\CarDealerScheme.cpp" /> <ClCompile Include="..\..\..\source\Data\Datastructure\CarDealerScheme\CarDealerScheme.cpp" />
<ClCompile Include="..\..\..\source\data\excel\excel.cpp" />
<ClCompile Include="..\..\..\source\main.cpp" /> <ClCompile Include="..\..\..\source\main.cpp" />
<ClCompile Include="..\..\..\source\test\test.cpp" />
<ClCompile Include="..\..\..\source\Widgets\MainFrame\QMainFrame.cpp" /> <ClCompile Include="..\..\..\source\Widgets\MainFrame\QMainFrame.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -38,6 +40,8 @@
<ClInclude Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.h" /> <ClInclude Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.h" />
<ClInclude Include="..\..\..\source\Data\Datastructure\CarDealerAchievement\CarDealerAchievement.h" /> <ClInclude Include="..\..\..\source\Data\Datastructure\CarDealerAchievement\CarDealerAchievement.h" />
<ClInclude Include="..\..\..\source\Data\Datastructure\CarDealerScheme\CarDealerScheme.h" /> <ClInclude Include="..\..\..\source\Data\Datastructure\CarDealerScheme\CarDealerScheme.h" />
<ClInclude Include="..\..\..\source\data\excel\excel.h" />
<ClInclude Include="..\..\..\source\test\test.h" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{B12702AD-ABFB-343A-A199-8E24837244A3}</ProjectGuid> <ProjectGuid>{B12702AD-ABFB-343A-A199-8E24837244A3}</ProjectGuid>

View File

@ -50,6 +50,12 @@
<Filter Include="数据\数据管理\excel"> <Filter Include="数据\数据管理\excel">
<UniqueIdentifier>{849ef05c-ca3c-479b-ad38-3b9eae188820}</UniqueIdentifier> <UniqueIdentifier>{849ef05c-ca3c-479b-ad38-3b9eae188820}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="测试">
<UniqueIdentifier>{c6a0a5fd-26f9-4d8d-84a5-ce7edb04eb5f}</UniqueIdentifier>
</Filter>
<Filter Include="excel">
<UniqueIdentifier>{61d5dd61-6b1f-4507-bb22-4ce463ad5b85}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\..\source\main.cpp"> <ClCompile Include="..\..\..\source\main.cpp">
@ -70,6 +76,12 @@
<ClCompile Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.cpp"> <ClCompile Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.cpp">
<Filter>数据\数据管理\excel</Filter> <Filter>数据\数据管理\excel</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\source\test\test.cpp">
<Filter>测试</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\data\excel\excel.cpp">
<Filter>excel</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtRcc Include="..\..\..\source\resource.qrc"> <QtRcc Include="..\..\..\source\resource.qrc">
@ -109,5 +121,11 @@
<ClInclude Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.h"> <ClInclude Include="..\..\..\source\Data\DataManipulation\Excel\LoadFromExcel.h">
<Filter>数据\数据管理\excel</Filter> <Filter>数据\数据管理\excel</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\source\test\test.h">
<Filter>测试</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\data\excel\excel.h">
<Filter>excel</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,20 +1,11 @@
#include <libxl.h> #include <libxl.h>
#include <exception>
#include "LoadFromExcel.h"
#include <stdexcept> #include <stdexcept>
#include "LoadFromExcel.h"
#include "../../excel/excel.h"
using namespace std; using namespace std;
using namespace libxl; using namespace libxl;
void SetKey( Book * pBook )
{
if ( pBook == nullptr )
{
return;
}
pBook->setKey( L"cpic", L"windows-202d21040bc4e70060bc6264a6ucu7i1" );
}
/************************************************ /************************************************
* \brief Excel文件读取车商方案表 * \brief Excel文件读取车商方案表
@ -23,8 +14,8 @@ void SetKey( Book * pBook )
************************************************/ ************************************************/
void LoadCarDealerSchemeFromXlsx( const wstring & filePath, void LoadCarDealerSchemeFromXlsx( const wstring & filePath,
unsigned int sheetIndex, unsigned int sheetIndex,
unsigned int firstRowIndex, unsigned int startRowIndex,
map<wstring, CarDealerScheme> & schemeMap ) vector<CarDealerScheme> & schemeVector )
{ {
Book * pBook = xlCreateXMLBookW(); Book * pBook = xlCreateXMLBookW();
Sheet * pSheet = nullptr; Sheet * pSheet = nullptr;
@ -34,7 +25,7 @@ void LoadCarDealerSchemeFromXlsx( const wstring & filePath,
throw runtime_error( "libxl库加载失败" ); throw runtime_error( "libxl库加载失败" );
} }
SetKey( pBook ); setKey( pBook );
if ( pBook->load( filePath.c_str() ) != true ) if ( pBook->load( filePath.c_str() ) != true )
{ {
@ -51,8 +42,43 @@ void LoadCarDealerSchemeFromXlsx( const wstring & filePath,
string errorMessage = "读取sheet失败"; string errorMessage = "读取sheet失败";
errorMessage.append( pBook->errorMessage() ); errorMessage.append( pBook->errorMessage() );
pBook->release();
throw runtime_error( errorMessage ); 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();
} }

View File

@ -1,9 +1,11 @@
 #pragma once
#pragma once
#include <map> #include <string>
#include <exception> #include <vector>
#include "../../Datastructure/CarDealerScheme/CarDealerScheme.h" #include "../../Datastructure/CarDealerScheme/CarDealerScheme.h"
#include "../../Datastructure/CarDealerAchievement/CarDealerAchievement.h" #include "../../Datastructure/CarDealerAchievement/CarDealerAchievement.h"
void LoadCarDealerSchemeFromXlsx( const std::wstring & filePath,
unsigned int sheetIndex,
unsigned int startRowIndex,
std::vector<CarDealerScheme> & schemeVector );

View File

@ -59,14 +59,14 @@ public:
} }
CarDealerScheme( CarDealerScheme && other ) CarDealerScheme( CarDealerScheme && other )
: theYear( std::move(other.theYear) ), : theYear( std::move( other.theYear ) ),
theMonth( std::move(other.theMonth) ), theMonth( std::move( other.theMonth ) ),
carDealerCode( std::move(other.carDealerCode) ), carDealerCode( std::move( other.carDealerCode ) ),
manHourPrice( std::move(other.manHourPrice) ), manHourPrice( std::move( other.manHourPrice ) ),
partPrice( std::move(other.partPrice) ), partPrice( std::move( other.partPrice ) ),
claimSupport( std::move(other.claimSupport) ), claimSupport( std::move( other.claimSupport ) ),
scheme( std::move(other.scheme) ), scheme( std::move( other.scheme ) ),
isQualified( std::move(other.isQualified) ) isQualified( std::move( other.isQualified ) )
{ {
} }

View File

@ -0,0 +1,157 @@
#include <string>
#include <stdio.h>
#include <windows.h>
#include <stdexcept>
#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<wchar_t> * 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<wchar_t> * 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<long>(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<wchar_t> * 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;
}

View File

@ -0,0 +1,31 @@
#pragma once
#ifndef EXCEL_H_
#define EXCEL_H_
#include <libxl.h>
void setKey( libxl::Book * pBook );
/************************************************
* \brief xlsx文件单元格中读取数据
* \param pBook libxl的Book对象
* \param sheetIndex sheet的索引值
* \param rowIndex
* \param colIndex
* \return
************************************************/
std::wstring ReadCellStringFromXlsx( libxl::IBookT<wchar_t> * pBook,
unsigned int sheetIndex,
unsigned int rowIndex,
unsigned int colIndex,
bool isInteger );
/************************************************
* \brief sheet
* \param sheetName
* \return
************************************************/
libxl::Sheet * getXlsxSheetByName( libxl::IBookT<wchar_t> * pBook, const std::wstring & sheetName );
#endif

View File

@ -1,6 +1,7 @@
#include <QtWidgets/QtWidgets> #include <QtWidgets/QtWidgets>
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include "Widgets/MainFrame/QMainFrame.h" #include "Widgets/MainFrame/QMainFrame.h"
#include "test/test.h"
int main( int argc, char * argv[] ) int main( int argc, char * argv[] )
{ {
@ -21,6 +22,10 @@ int main( int argc, char * argv[] )
return -1; return -1;
} }
test();
return 1;
try try
{ {
pMainFrame = new QMainFrame(); pMainFrame = new QMainFrame();

View File

@ -0,0 +1,24 @@

#include <vector>
#include <string>
#include "test.h"
#include "../data/DataManipulation/Excel/LoadFromExcel.h"
using namespace std;
void excelTest();
void test()
{
excelTest();
}
void excelTest()
{
vector<CarDealerScheme> schemeVector;
wstring filePath = L"D:/develop/projects_win/2019/car_dealer_util/数据/PC端导入模板管理员版.xlsx";
LoadCarDealerSchemeFromXlsx(filePath, 0, 1, schemeVector);
return;
}

View File

@ -0,0 +1,4 @@

#pragma once
void test();