You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
10 KiB
C++

1 year ago
#include "excelengine.h"
#include "qt_windows.h"
#include <QDebug>
#define QDEBUG(x) (qDebug() <<__FUNCTION__ <<x)
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 )
{
//析构前先保存数据然后关闭workbook
Close();
}
OleUninitialize();
}
/**
*@brief sXlsFileexcel
*@return true :
* false:
*/
bool ExcelEngine::Open(UINT nSheet, bool visible)
{
if ( bIsOpen )
{
//return 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)); //打开xls对应的工作簿
QAxObject *workBook = pExcel->querySubObject("ActiveWorkBook");
pSheets =workBook->querySubObject("Sheets");
sheetcounts=pSheets->property("Count").toInt();
}
else
{
pWorkbooks = pExcel->querySubObject("WorkBooks"); //获取工作簿
pWorkbooks->dynamicCall("Add"); //添加一个新的工作薄
pWorkbook = pExcel->querySubObject("ActiveWorkBook"); //新建一个xls
}
pWorksheet = pWorkbook->querySubObject("WorkSheets(int)", nCurrSheet);//打开第一个sheet
//至此已打开,开始获取相应属性
QAxObject *usedrange = pWorksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
QAxObject *rows = usedrange->querySubObject("Rows");
QAxObject *columns = usedrange->querySubObject("Columns");
//因为excel可以从任意行列填数据而不一定是从0,0开始因此要获取首行列下标
nStartRow = usedrange->property("Row").toInt(); //第一行的起始位置
nStartColumn = usedrange->property("Column").toInt(); //第一列的起始位置
nRowCount = rows->property("Count").toInt(); //获取行数
nColumnCount = columns->property("Count").toInt(); //获取列数
bIsOpen = true;
return bIsOpen;
}
/**
*@brief Open()
*/
bool ExcelEngine::Open(QString xlsFile, UINT nSheet, bool visible)
{
sXlsFile = xlsFile;
nCurrSheet = nSheet;
bIsVisible = visible;
return Open(nCurrSheet,bIsVisible);
}
/**
*@brief
*/
void ExcelEngine::Save()
{
if ( pWorkbook )
{
if (bIsSaveAlready)
{
return ;
}
if (!bIsANewFile)
{
pWorkbook->dynamicCall("Save()");
}
else /*如果该文档是新建出来的则使用另存为COM接口*/
{
QDEBUG(sXlsFile);
pWorkbook->dynamicCall("SaveAs (const QString&,int,const QString&,const QString&,bool,bool)",
sXlsFile,56,QString(""),QString(""),false,false);
}
bIsSaveAlready = true;
}
}
/**
*@brief Excel COM
*/
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;
}
}
/**
*@brief tableWidgetexcel
*@param tableWidget : GUItablewidget
*@return true :
* false:
*/
bool ExcelEngine::SaveDataFrTable(QTableWidget *tableWidget)
{
if ( NULL == tableWidget )
{
return false;
}
if ( !bIsOpen )
{
return false;
}
int tableR = tableWidget->rowCount();
int tableC = tableWidget->columnCount();
//获取表头写做第一行
for (int i=0; i<tableC; i++)
{
if ( tableWidget->horizontalHeaderItem(i) != NULL )
{
this->SetCellData(1,i+1,tableWidget->horizontalHeaderItem(i)->text());
}
}
//写数据
for (int i=0; i<tableR; i++)
{
for (int j=0; j<tableC; j++)
{
if ( tableWidget->item(i,j) != NULL )
{
this->SetCellData(i+2,j+1,tableWidget->item(i,j)->text());
}
}
}
//保存
Save();
return true;
}
/**
*@brief xlstableWidget
*@param tableWidget : tablewidget
*@return true :
* false:
*/
//bool ExcelEngine::ReadDataToTable(QTableWidget *tableWidget)
bool ExcelEngine::ReadDataToTable( )
{
// if ( NULL == tableWidget )
// {
// return false;
// }
// //先把table的内容清空
// int tableColumn = tableWidget->columnCount();
// tableWidget->clear();
// for (int n=0; n<tableColumn; n++)
// {
// tableWidget->removeColumn(0);
// }
int rowcnt = nStartRow + nRowCount;
int columncnt = 13;//nStartColumn + nColumnCount;
//获取excel中的第一行数据作为表头
QStringList headerList;
for(int i =nStartRow; i<rowcnt; i++)
{
QString qstring;
for (int n = nStartColumn; n<columncnt; n++ )
{
QAxObject * cell = pWorksheet->querySubObject("Cells(int,int)", i, n);//nStartRow
if ( cell )
{
headerList<<cell->dynamicCall("Value2()").toString();
qstring.append(cell->dynamicCall("Value2()").toString()).append(" ");
//qDebug()<<cell->dynamicCall("Value2()").toString();
QFile file_write("/home/data/usb_excel_data");//文件路径
if(file_write.open(QIODevice::WriteOnly|QIODevice::Append))
{
QTextStream setLORA_data(&file_write);
setLORA_data<<cell->dynamicCall("Value2()").toString()<<endl;
}
file_write.close();
}
}
// QDEBUG(qstring);
}
//重新创建表头
// 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 );//获取单元格
// //在r新行中添加子项数据
// if ( cell )
// {
// tableWidget->setItem(r,c,new QTableWidgetItem(cell->dynamicCall("Value2()").toString()));
// }
// }
// }
return true;
}
/**
*@brief
*@param row :
*@param column :
*@return [row,column]
*/
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;
}
/**
*@brief
*@param row :
*@param column :
*@param data :
*@return true :
* false:
*/
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(); //excel 居然只能插入字符串和整型,浮点型无法插入
cell->dynamicCall("SetValue(const QVariant&)",strData); //修改单元格的数据
op = true;
}
else
{
op = false;
}
return op;
}
/**
*@brief
*/
void ExcelEngine::Clear()
{
sXlsFile = "";
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
}
/**
*@brief excel
*@return true :
* false:
*/
bool ExcelEngine::IsOpen()
{
return bIsOpen;
}
/**
*@brief excel COMexcel
*@return true :
* false:
*/
bool ExcelEngine::IsValid()
{
return bIsValid;
}
/**
*@brief excel
*/
UINT ExcelEngine::GetRowCount()const
{
return nRowCount;
}
/**
*@brief excel
*/
UINT ExcelEngine::GetColumnCount()const
{
return nColumnCount;
}
QString ExcelEngine::GetSheetName(int i)
{
QAxObject* pWorksheettemp=pWorkbook->querySubObject("WorkSheets(int)", i);
return pWorksheettemp->property("Name").toString();
}