搞定车商方案表读取。
This commit is contained in:
157
代码/cpp/car_dealer_util/source/data/excel/excel.cpp
Normal file
157
代码/cpp/car_dealer_util/source/data/excel/excel.cpp
Normal 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;
|
||||
}
|
31
代码/cpp/car_dealer_util/source/data/excel/excel.h
Normal file
31
代码/cpp/car_dealer_util/source/data/excel/excel.h
Normal 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
|
Reference in New Issue
Block a user