#include "excelengine.h"
#include "qt_windows.h"
#include <QHeaderView>
ExcelEngine::ExcelEngine()
{
pExcel = NULL;
pWorkbooks = NULL;
pWorkbook = NULL;
pWorksheet = NULL;
sXlsFile = "";
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
bIsOpen = false;
bIsValid = false;
bIsANewFile = false;
bIsSaveAlready = false;
HRESULT r = OleInitialize(0);
if (r != S_OK && r != S_FALSE)
{
qDebug("Qt: Could not initialize OLE (error %x)", (unsigned int)r);
}
}
ExcelEngine::ExcelEngine(QString xlsFile)
{
pExcel = NULL;
pWorkbooks = NULL;
pWorkbook = NULL;
pWorksheet = NULL;
sXlsFile = xlsFile;
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
bIsOpen = false;
bIsValid = false;
bIsANewFile = false;
bIsSaveAlready = false;
HRESULT r = OleInitialize(0);
if (r != S_OK && r != S_FALSE)
{
qDebug("Qt: Could not initialize OLE (error %x)", (unsigned int)r);
}
}
ExcelEngine::~ExcelEngine()
{
if ( bIsOpen )
{
Close();
}
OleUninitialize();
}
bool ExcelEngine::Open(UINT nSheet, bool visible)
{
if ( bIsOpen )
{
Close();
}
nCurrSheet = nSheet;
bIsVisible = visible;
if ( NULL == pExcel )
{
pExcel = new QAxObject("Excel.Application");
if ( pExcel )
{
bIsValid = true;
}
else
{
bIsValid = false;
bIsOpen = false;
return bIsOpen;
}
pExcel->dynamicCall("SetVisible(bool)", bIsVisible);
}
if ( !bIsValid )
{
bIsOpen = false;
return bIsOpen;
}
if ( sXlsFile.isEmpty() )
{
bIsOpen = false;
return bIsOpen;
}
QFile f(sXlsFile);
if (!f.exists())
{
bIsANewFile = true;
}
else
{
bIsANewFile = false;
}
if (!bIsANewFile)
{
pWorkbooks = pExcel->querySubObject("WorkBooks");
pWorkbook = pWorkbooks->querySubObject("Open(QString, QVariant)",sXlsFile,QVariant(0));
}
else
{
pWorkbooks = pExcel->querySubObject("WorkBooks");
pWorkbooks->dynamicCall("Add");
pWorkbook = pExcel->querySubObject("ActiveWorkBook");
}
pWorksheet = pWorkbook->querySubObject("WorkSheets(int)", nCurrSheet);
QAxObject *usedrange = pWorksheet->querySubObject("UsedRange");
QAxObject *rows = usedrange->querySubObject("Rows");
QAxObject *columns = usedrange->querySubObject("Columns");
nStartRow = usedrange->property("Row").toInt();
nStartColumn = usedrange->property("Column").toInt();
nRowCount = rows->property("Count").toInt();
nColumnCount = columns->property("Count").toInt();
bIsOpen = true;
return bIsOpen;
}
bool ExcelEngine::Open(QString xlsFile, UINT nSheet, bool visible)
{
sXlsFile = xlsFile;
nCurrSheet = nSheet;
bIsVisible = visible;
return Open(nCurrSheet,bIsVisible);
}
void ExcelEngine::Save()
{
if ( pWorkbook )
{
if (bIsSaveAlready)
{
return ;
}
if (!bIsANewFile)
{
pWorkbook->dynamicCall("Save()");
}
else
{
pWorkbook->dynamicCall("SaveAs (const QString&,int,const QString&,const QString&,bool,bool)",
sXlsFile,56,QString(""),QString(""),false,false);
}
bIsSaveAlready = true;
}
}
void ExcelEngine::Close()
{
Save();
if ( pExcel && pWorkbook )
{
pWorkbook->dynamicCall("Close(bool)", true);
pExcel->dynamicCall("Quit()");
delete pExcel;
pExcel = NULL;
bIsOpen = false;
bIsValid = false;
bIsANewFile = false;
bIsSaveAlready = true;
}
}
bool ExcelEngine::SaveDataFrTable(QTableView *tableWidget)
{
if ( NULL == tableWidget )
{
return false;
}
if ( !bIsOpen )
{
return false;
}
int tableR = tableWidget->model()->rowCount();
int tableC = tableWidget->model()->columnCount();
for (int i=0; i<tableC; i++)
{
if ( tableWidget->horizontalHeader()!= NULL )
{
this->SetCellData(1,i+1,tableWidget->horizontalHeader()->model()->headerData(i,Qt::Horizontal).toString());
}
}
for (int i=0; i<tableR; i++)
{
for (int j=0; j<tableC; j++)
{
if (!tableWidget->model()->index(i,j).data().toString().isEmpty())
{
QString tempItem=tableWidget->model()->index(i,j).data().toString();
if(tempItem.count()>=15 && j!=tableC-1)
tempItem.push_front(QChar('\''));
this->SetCellData(i+2,j+1,tempItem);
}
}
}
Save();
return true;
}
bool ExcelEngine::ReadDataToTable(QTableWidget *tableWidget)
{
if ( NULL == tableWidget )
{
return false;
}
int tableColumn = tableWidget->columnCount();
tableWidget->clear();
for (int n=0; n<tableColumn; n++)
{
tableWidget->removeColumn(0);
}
int rowcnt = nStartRow + nRowCount;
int columncnt = nStartColumn + nColumnCount;
QStringList headerList;
for (int n = nStartColumn; n<columncnt; n++ )
{
QAxObject * cell = pWorksheet->querySubObject("Cells(int,int)",nStartRow, n);
if ( cell )
{
headerList<<cell->dynamicCall("Value2()").toString();
}
}
tableWidget->setColumnCount(nColumnCount);
tableWidget->setHorizontalHeaderLabels(headerList);
for (int i = nStartRow+1, r = 0; i < rowcnt; i++, r++ )
{
tableWidget->insertRow(r);
for (int j = nStartColumn, c = 0; j < columncnt; j++, c++ )
{
QAxObject * cell = pWorksheet->querySubObject("Cells(int,int)", i, j );
if ( cell )
{
tableWidget->setItem(r,c,new QTableWidgetItem(cell->dynamicCall("Value2()").toString()));
}
}
}
return true;
}
QVariant ExcelEngine::GetCellData(UINT row, UINT column)
{
QVariant data;
QAxObject *cell = pWorksheet->querySubObject("Cells(int,int)",row,column);
if ( cell )
{
data = cell->dynamicCall("Value2()");
}
return data;
}
bool ExcelEngine::SetCellData(UINT row, UINT column, QVariant data)
{
bool op = false;
QAxObject *cell = pWorksheet->querySubObject("Cells(int,int)",row,column);
if ( cell )
{
QString strData = data.toString();
cell->dynamicCall("SetValue(const QVariant&)",strData);
op = true;
}
else
{
op = false;
}
return op;
}
void ExcelEngine::Clear()
{
sXlsFile = "";
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
}
bool ExcelEngine::IsOpen()
{
return bIsOpen;
}
bool ExcelEngine::IsValid()
{
return bIsValid;
}
UINT ExcelEngine::GetRowCount()const
{
return nRowCount;
}
UINT ExcelEngine::GetColumnCount()const
{
return nColumnCount;
}