① 确保Excel软件在本地服务器注册成功,没注册成功的可以通过 在运行中"E:\program Files\Microsoft Office\Office12\EXCEL.EXE" /regserver 手动注册,注意路径要用自己的excel路径。
② 确保组件配制正确,运行命令:dcomcnfg,查看DCOM配置下是否有Microsoft Excel Application,有的话万事大吉,开始设置(1)常规,身份验证:无(2)安全、配制权限自定义,控制权限,完全控制选中,保存
连接Excel控件
QAxWidget excel(“Excel.Application”);
显示当前窗口:
excel.setProperty(“Visible”, true);
更改 Excel 标题栏:
excel.setProperty(“Caption”, “Invoke Microsoft Excel”);
不显示任何警告信息
setProperty(“DisplayAlerts”,0)
添加新工作簿:
QAxObject * workbooks = excel.querySubObject(“WorkBooks”);
workbooks->dynamicCall(“Add”);
打开已存在的工作簿:
workbooks->dynamicCall(“Open (const QString&)”, QString(“c:/test.xls”));
获取活动工作簿:
QAxObject * workbook = excel.querySubObject(“ActiveWorkBook”);
获取所有的工作表:
QAxObject * worksheets = workbook->querySubObject(“WorkSheets”);
获取工作表数量:
int intCount = worksheets->property(“Count”).toInt();
获取第一个工作表:
QAxObject * worksheet = workbook->querySubObject(“Worksheets(int)”, 1);
获取cell的值:
QAxObject * range = worksheet->querySubObject(“Cells(int,int)”, 1, 1 );
获取已使用区域
m_pworksheet->querySubObject(“UsedRange”)
获取行数
QAxObject *rows = usedrange->querySubObject(“Rows”);
int nRows = rows->property(“Count”).toInt();
获取列数
QAxObject *columns = usedrange->querySubObject(“Columns”);
int nCols = columns->property(“Count”).toInt();
QAxObject *pCell = work_sheet->querySubObject("Range(const QString&)", "A1:C1");
if (pCell)
{
//字体对齐(居中)
pCell ->setProperty("HorizontalAlignment", -4108);//左对齐(xlLeft):-4131 居中(xlCenter):-4108 右对齐(xlRight):-4152
pCell ->setProperty("VerticalAlignment", -4108); //上对齐(xlTop)-4160 居中(xlCenter):-4108 下对齐(xlBottom):-4107
//设置字体
QAxObject *font = pCell->querySubObject("Font"); //获取单元格字体
font->setProperty("Bold", true); //字体加粗
font->setProperty("Name", QStringLiteral("华文彩云")); //设置单元格字体
font->setProperty("Bold", true); //设置单元格字体加粗
font->setProperty("Size", 20); //设置单元格字体大小
font->setProperty("Italic", true); //设置单元格字体斜体
font->setProperty("Underline", 2); //设置单元格下划线
font->setProperty("Color", QColor(255, 0, 0)); //设置单元格字体颜色(红色)
//设置单元格背景色(灰色)
QColor bkColor(125, 125, 125);
QAxObject* interior = pCell->querySubObject("Interior");
interior->setProperty("Color", bkColor);
//绘制单元格边框(黑色)
QAxObject* border = pCell->querySubObject("Borders");
border->setProperty("Color", QColor(0, 0, 0));
//创建组
QAxObject* rows = pCell->querySubObject("Rows");
rows->querySubObject("Group");
}
//宽度自适应
auto range = work_sheet->querySubObject("UsedRange");
QAxObject * cells = range->querySubObject("Columns");
if (cells)
{
cells->dynamicCall("AutoFit");
}
//设置单元格格式(文本)
QAxObject *oRange = work_sheet->querySubObject("Range(QString)", "A1:A99");
if (oRange)
{
oRange->setProperty("NumberFormatLocal", "@");
}
oRange = work_sheet->querySBubObject("Range(QString)", "A1:B1");
if (oRange)
{
pCell ->setProperty("WrapText", true); //内容过多,自动换行
pCell ->setProperty("MergeCells", true); //合并单元格
//pCell ->setProperty("MergeCells", false); //拆分单元格
//pcell->dynamicCall("ClearContents()"); //清空单元格内容
}
querySubObject(“Open(QString&)”,“path”)命令执行是出现如下异常信息
报错:800706BE 远程过程调用失败
描述:改报错在dynamicCall(“SetVisible(bool)”, true)时不会报错
连接COM组件,设置基本属性
int InitExcel()
{
#ifdef Q_OS_WIN
CoInitialize(nullptr);
m_pExcel = new QAxObject();
bool b_ret = m_pExcel->setControl("Excel.Application");
if (!b_ret)
{
b_ret = m_pExcel->setControl("ket.Application");
if (!b_ret)
{
return -1;
}
}
m_pExcel->dynamicCall("SetVisible(bool)", false);
m_pExcel->setProperty("DisplayAlerts", false);
m_pWorkBooks = m_pExcel->querySubObject("WorkBooks");
if (!m_pWorkBooks)
{
CloseExcel();
return -2;
}
return 0;
#else
return -1;
#endif
}
int OpenExcel(const QString& path)
{
int i_ret = 0;
#ifdef Q_OS_WIN
i_ret = InitExcel();
if (0 != i_ret)
{
return -1;
}
CloseWorkBook();
QFile file(path);
if (!file.exists())
{
CreateExcel(path);
}
else
{
m_pWorkBook = m_pWorkBooks->querySubObject("Open(QString&)", QDir::toNativeSeparators(path));
if (!m_pWorkBook)
{
return -2;
}
}
file.close();
m_pWorkSheets = m_pWorkBook->querySubObject("Sheets");
if (!m_pWorkSheets)
{
return -3;
}
m_pWorkSheet = m_pWorkSheets->querySubObject("Item(int)", 1);
if (!m_pWorkSheet)
{
return -4;
}
m_sFileName = path;
return 0;
#else
return -1;
#endif
}
//关闭workbook
void CloseWorkBook()
{
#ifdef Q_OS_WIN
if (m_pWorkBook)
{
QVariant ret = m_pWorkBook->dynamicCall("Close()");
}
#endif
}
void CreateExcel(const QString &path)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkBooks);
if (m_pWorkBooks)
{
m_pWorkBooks->dynamicCall("Add");
m_pWorkBook = m_pExcel->querySubObject("ActiveWorkBook");
SaveAsExcel(path);
}
#endif
}
int SaveAsExcel(const QString& path)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkBook);
try
{
if (m_pWorkBook)
{
QVariant ret = m_pWorkBook->dynamicCall("SaveAs(const QString &)", QDir::toNativeSeparators(path));
if (ret.toBool())
{
return 0;
}
}
}
catch (...)
{
return -2;
}
return -1;
#else
return -1;
#endif
}
void CloseExcel()
{
m_sFileName = "";
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkBooks);
Q_ASSERT(!m_pWorkBooks->isNull());
if (m_pWorkBooks && !m_pWorkBooks->isNull())
{
QVariant ret = m_pWorkBooks->dynamicCall("Close()");
}
if (m_pExcel)
{
QVariant ret = m_pExcel->dynamicCall("Quit()");
}
RELEASE_POINTER(m_pWorkSheet);
RELEASE_POINTER(m_pWorkSheets);
RELEASE_POINTER(m_pWorkBook);
RELEASE_POINTER(m_pWorkBooks);
RELEASE_POINTER(m_pExcel);
CoUninitialize();
#endif
}
int ColumnAutoFit()
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheet);
Q_ASSERT(!m_pWorkSheet->isNull());
if (m_pWorkSheet && !m_pWorkSheet->isNull())
{
try
{
QAxObject *rang = m_pWorkSheet->querySubObject("UsedRange");
QAxObject *columns = rang->querySubObject("Columns");
if (columns)
{
QVariant var = columns->dynamicCall("AutoFit");
RELEASE_POINTER(columns);
}
RELEASE_POINTER(rang);
}
catch (...)
{
return -2;
}
return 0;
}
return -1;
#else
return -1;
#endif
}
int GetWorkSheetCount()
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheets);
Q_ASSERT(!m_pWorkSheet->isNull());
if (m_pWorkSheets && !m_pWorkSheet->isNull())
{
return m_pWorkSheets->property("Count").toInt();
}
return -1;
#else
return -1;
#endif
}
int ReadAll(QList> &list)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheets);
Q_ASSERT(!m_pWorkSheet->isNull());
if (!m_pWorkSheet && m_pWorkSheet->isNull())
{
return -1;
}
list.clear();
QAxObject* usedRange = m_pWorkSheet->querySubObject("UsedRange");
//auto var = usedRange->dynamicCall("Value()");
auto var = usedRange->property("Value2");
auto rowList = var.toList();
for (auto rowData : rowList)
{
auto subData = rowData.toList();
QList subList;
for (auto data : subData)
{
subList.push_back(data);
}
list.push_back(subList);
}
RELEASE_POINTER(usedRange);
return 0;
#else
return -1;
#endif
}
int Write(const QList> &data, bool overwrite)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheets);
Q_ASSERT(!m_pWorkSheet->isNull());
if (data.size() <= 0)
{
return -1;
}
if (!m_pWorkSheet && m_pWorkSheet->isNull())
{
return -2;
}
QVariantList variantList;
for (size_t i = 0; i < data.size(); i++)
{
variantList.push_back(data.at(i));
}
QVariant variant = (QVariant)variantList;
int i_row = data.size();
int i_col = data.at(0).size();
int i_row_start = 1;
int i_row_count = 0;
if (!overwrite)
{
i_row_count = GetRowCount();
i_row_start = i_row_count + 1;
}
QString strRang;
ConvertToColName(i_col, strRang);
strRang += QString::number(i_row + i_row_count);
strRang = QString("A%1:").arg(i_row_start) + strRang;
try
{
QAxObject *range = m_pWorkSheet->querySubObject("Range(const QString&)", strRang);
range->setProperty("Value2", variant);
range->setProperty("WrapText", true); //内容过多自动换行
range->setProperty("NumberFormatLocal", "@"); //设置单元格格式(文本)
range->setProperty("HorizontalAlignment", -4108);//左对齐(xlLeft):-4131 居中(xlCenter):-4108 右对齐(xlRight):-4152
range->setProperty("VerticalAlignment", -4108); //上对齐(xlTop)-4160 居中(xlCenter):-4108 下对齐(xlBottom):-4107
QAxObject *border = range->querySubObject("Borders");
border->setProperty("Color", QColor(0, 0, 0)); //设置边框颜色
RELEASE_POINTER(border);
RELEASE_POINTER(range);
}
catch (...)
{
return -3;
}
return 0;
#else
return -1;
#endif
}
//
QString ConvertToLetters(int data)
{
QChar ch = data + 0x40; //A对应0x41
return QString(ch);
}
void ConvertToColName(int data, QString &res)
{
#ifdef Q_OS_WIN
Q_ASSERT(data > 0 && data < 65535);
int tempData = data / 26;
if (tempData > 0)
{
int mode = data % 26;
ConvertToColName(mode, res);
ConvertToColName(tempData, res);
}
else
{
res = ConvertToLetters(data) + res;
}
#endif
}
int GetCellValue(const int row, const int column, QVariant &value)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheet);
Q_ASSERT(!m_pWorkSheet->isNull());
if (!m_pWorkSheet && !m_pWorkSheet->isNull())
{
return -1;
}
try
{
QAxObject *range = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
auto var = range->property("Value2");
value = var;
RELEASE_POINTER(range);
}
catch (...)
{
return -2;
}
return 0;
#else
return -1;
#endif
}
int SetCellValue(const int row, const int column, const QVariant &value)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheet);
Q_ASSERT(!m_pWorkSheet->isNull());
if (!m_pWorkSheet && !m_pWorkSheet->isNull())
{
return -1;
}
try
{
QAxObject *range = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
bool ret = range->setProperty("Value2", value);
RELEASE_POINTER(range);
}
catch (...)
{
return -2;
}
return 0;
#else
return -1;
#endif
}
int SetWorkSheetNum(const int num)
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkBook);
Q_ASSERT(!m_pWorkBook->isNull());
if (!m_pWorkBook && !m_pWorkBook->isNull())
{
return -1;
}
if (m_pWorkSheet)
{
RELEASE_POINTER(m_pWorkSheet);
}
try
{
m_pWorkSheet = m_pWorkBook->querySubObject("Worksheets(int)", num);
}
catch (...)
{
return -2;
}
return 0;
#else
return -1;
#endif
}
int GetColumnCount()
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheet);
if (m_pWorkSheet && !m_pWorkSheet->isNull())
{
int nCols = 0;
try
{
QAxObject *usedRange = m_pWorkSheet->querySubObject("UsedRange");
QAxObject *columns = usedRange->querySubObject("Columns");
nCols = columns->property("Count").toInt();
RELEASE_POINTER(columns);
RELEASE_POINTER(usedRange);
}
catch (...)
{
return -2;
}
return nCols;
}
return -1;
#else
return -1;
#endif
}
int GetRowCount()
{
#ifdef Q_OS_WIN
Q_ASSERT(m_pWorkSheet);
if (m_pWorkSheet)
{
int nRows = 0;
try
{
QAxObject *usedRange = m_pWorkSheet->querySubObject("UsedRange");
if (usedRange)
{
QAxObject *rows = usedRange->querySubObject("Rows");
if (rows)
{
nRows = rows->property("Count").toInt();
RELEASE_POINTER(rows);
}
RELEASE_POINTER(usedRange);
}
}
catch (...)
{
return -2;
}
return nRows;
}
return -1;
#else
return -1;
#endif
}