当前位置:网站首页>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
边栏推荐
- Crawl lottery data
- Multifunctional toolbox wechat applet source code
- 14 py games source code share the second bullet
- Connection mode of QT signal and slot connect() and the return value of emit
- Map basemap Library
- Daily network security certification test questions (April 12, 2022)
- If condition judgment in shell language
- WiFi ap6212 driver transplantation and debugging analysis technical notes
- Robocode tutorial 5 - enemy class
- CISSP certified daily knowledge points (April 13, 2022)
猜你喜欢
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
多功能工具箱微信小程序源码
【ACM】376. 摆动序列
Docker 安装 Redis
STM32 learning record 0008 - GPIO things 1
Docker 安裝 Redis
Docker installation MySQL
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
Batch export ArcGIS attribute table
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
随机推荐
Deep learning classic network analysis and target detection (I): r-cnn
According to the result set queried by SQL statement, it is encapsulated as JSON
Feign requests the log to be printed uniformly
Multi thread safe reference arc of rust
QT notes on qmap container freeing memory
How to ensure the security of futures accounts online?
Jenkspy package installation
A few lines of code teach you to crawl lol skin pictures
多功能工具箱微信小程序源码
Daily CISSP certification common mistakes (April 12, 2022)
线上怎么确定期货账户安全的?
Daily CISSP certification common mistakes (April 13, 2022)
Halo 开源项目学习(七):缓存机制
GDAL + ogr learning
Installation du docker redis
C medium? This form of
Nodejs安装
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
Crawler for querying nicknames and avatars based on qqwebapi