当前位置:网站首页>QT excel operation summary
QT excel operation summary
2022-04-23 18:19:00 【Things will turn when they reach the extreme 1024】
1. Basic read and write file operation
2. The save dialog box pops up when saving
3. The problem of not saving the file after canceling the save dialog box
Reading and writing Excel
1.pro File to add
CONFIG += qaxcontainer
2. The header file
#include <ActiveQt/QAxObject>
3. Read file flow
Establish process and open file , Get table properties
// establish excel Process and get the total number of columns in the table
QAxObject excel("Excel.Application"); // Build process
excel.setProperty("Visible",false); // I do not know!
QAxObject *workbooks = excel.querySubObject("WorkBooks"); // The following operations are all in here
QString initFile=QDir::currentPath(); // File current path
initFile+="/debug/Table.xlsx";
//initFile+="/Table.xlsx";
workbooks->dynamicCall("Open (const QString&)",initFile); // Open file
QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook
QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet
QAxObject *used_range = worksheet->querySubObject("UsedRange");
QAxObject *rows = used_range->querySubObject("Rows");
QAxObject *columns = used_range->querySubObject("Columns");
int row_start = used_range->property("Row").toInt(); // Get the starting line
int column_start = used_range->property("Column").toInt(); // Get the starting column
int row_count = rows->property("Count").toInt(); // Get the number of lines
int column_count = columns->property("Count").toInt(); // Get the number of columns
qDebug()<<" Start line :"<<row_start<<" Start column :"<<column_start<<" Row number :"<<row_count<<" Number of columns :"<<column_count;
Read the file
QAxObject *rangeData = worksheet->querySubObject("Cells(int,int)",row,1); // obtain cell Value row,1 Is the coordinate
QString DataVal = rangeData->dynamicCall("Value2()").toString(); // Format
Data[row-1]=DataVal; // Take out
Close document end excel Threads
// Close document end excel Threads
workbooks->dynamicCall("Close(Boolean)",false); // close document
excel.dynamicCall("Quit(void)"); // sign out excel Threads
//delete excel; // If you create excel When using a pointer, you need to delete the pointer QAxObject *excel = new QAxObject(this);
4. Rewrite the file
Establish process and open file , Get table attributes as above
Rewrite the file
QAxObject *cell = worksheet->querySubObject("Cells(int,int)", row, 1);
cell->setProperty("Value", Data[row-1]); // Set cell values
Save the file
excel.setProperty("DisplayAlerts", false);/* Do not display any warning messages , If true, Then when it is closed, something like “ Whether to save the changes to XX file ”*/
workbook->dynamicCall("Save()"); // Save the active workbook file This sentence can't be wrong
// Close document end excel Threads
workbooks->dynamicCall("Close(Boolean)",false); // close document
excel.dynamicCall("Quit(void)"); // sign out excel Threads
5. Summary routine
void MainWindow::writeToFile(QString *Data,QString *name,int len)
{
// establish excel Process and get the total number of columns in the table
QAxObject excel("Excel.Application");
excel.setProperty("Visible",false);
QAxObject *workbooks = excel.querySubObject("WorkBooks");
QString initFile=QDir::currentPath();
initFile+="/debug/Table.xlsx";
//initFile+="/Table.xlsx";
workbooks->dynamicCall("Open (const QString&)",initFile);
QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook
QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet
int row_count=Data->length();
// Read and extract the contents of the form into the program
for(int row=1;row<row_count+1;row++)
{
QAxObject *cell = worksheet->querySubObject("Cells(int,int)", row, 1);
cell->setProperty("Value", Data[row-1]); // Set cell values
cell = worksheet->querySubObject("Cells(int,int)", row, 2);
cell->setProperty("Value", name[row-1]); // Set cell values
qDebug()<<row<<Data[row-1]<<" "<<name[row-1];
}
excel.setProperty("DisplayAlerts", false);/* Do not display any warning messages , If true, Then when it is closed, something like “ Whether to save the changes to XX file ”*/
workbook->dynamicCall("Save()"); // Save the active workbook file
// Close document end excel Threads
workbooks->dynamicCall("Close(Boolean)",false); // close document
excel.dynamicCall("Quit(void)"); // sign out excel Threads
}
void MainWindow::readfromFile(QString *Data,QString *name,int len)
{
// establish excel Process and get the total number of columns in the table
QAxObject excel("Excel.Application");
excel.setProperty("Visible",false);
QAxObject *workbooks = excel.querySubObject("WorkBooks");
QString initFile=QDir::currentPath();
initFile+="/debug/Table.xlsx";
//initFile+="/Table.xlsx";
workbooks->dynamicCall("Open (const QString&)",initFile);
QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook
QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet
QAxObject *used_range = worksheet->querySubObject("UsedRange");
QAxObject *rows = used_range->querySubObject("Rows");
QAxObject *columns = used_range->querySubObject("Columns");
int row_start = used_range->property("Row").toInt(); // Get the starting line
int column_start = used_range->property("Column").toInt(); // Get the starting column
int row_count = rows->property("Count").toInt(); // Get the number of lines
int column_count = columns->property("Count").toInt(); // Get the number of columns
qDebug()<<" Start line :"<<row_start<<" Start column :"<<column_start<<" Row number :"<<row_count<<" Number of columns :"<<column_count;
// Redefine the cache array size according to the table size
Data->resize(row_count);
name->resize(row_count);
// Read and extract the contents of the form into the program
for(int row=row_start;row<row_count+1;row++)
{
QAxObject *rangeData = worksheet->querySubObject("Cells(int,int)",row,1); // obtain cell Value
QAxObject *rangeName = worksheet->querySubObject("Cells(int,int)",row,2); // obtain cell Value
QString DataVal = rangeData->dynamicCall("Value2()").toString();
QString NameVal = rangeName->dynamicCall("Value2()").toString();
Data[row-1]=DataVal;
name[row-1]=NameVal;
qDebug()<<row<<Data[row-1]<<" "<<name[row-1];
delete rangeData;delete rangeName;
}
// Close document end excel Threads
workbooks->dynamicCall("Close(Boolean)",false); // close document
excel.dynamicCall("Quit(void)"); // sign out excel Threads
}
版权声明
本文为[Things will turn when they reach the extreme 1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610057618.html
边栏推荐
- 深度学习经典网络解析目标检测篇(一):R-CNN
- Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
- Notepad + + replaces tabs with spaces
- Calculation of fishing net road density
- Visualization of residential house prices
- Robocode Tutorial 4 - robocode's game physics
- Correct opening method of option
- Win1远程出现“这可能是由于credssp加密oracle修正”解决办法
- 解决允许在postman中写入注释请求接口方法
- Gson fastjason Jackson of object to JSON difference modifies the field name
猜你喜欢

【ACM】509. 斐波那契数(dp五部曲)

【ACM】376. 摆动序列

Visualization of residential house prices

【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))

Dock installation redis

Imx6 debugging LVDS screen technical notes

PowerDesigner various font settings; Preview font setting; SQL font settings

QT tablewidget insert qcombobox drop-down box

【ACM】70. climb stairs

Deep learning classic network analysis and target detection (I): r-cnn
随机推荐
Climbing watermelon video URL
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
PowerDesigner various font settings; Preview font setting; SQL font settings
Robocode tutorial 3 - Robo machine analysis
Re regular expression
Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]
Selenium + webdriver + chrome realize Baidu to search for pictures
Spark performance optimization guide
Closure type of rust (difference between FN, fnmut and fnone)
Docker 安装 MySQL
Crawling mobile game website game details and comments (MQ + multithreading)
Solution to Chinese garbled code after reg file is imported into the registry
7-21 wrong questions involve knowledge points.
Correct opening method of option
Notepad + + replaces tabs with spaces
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
If condition judgment in shell language
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
Excel opens large CSV format data
STM32学习记录0008——GPIO那些事1