Просмотр исходного кода

完成了WD,GS示例文件的下载

wzp 3 месяцев назад
Родитель
Сommit
5d2bf0ac2a
4 измененных файлов: 331 добавлений и 16 удалений
  1. 142
    0
      Back/GS/GSExport.py
  2. 156
    1
      Back/WD/WDExport.py
  3. 31
    13
      Front/main.py
  4. 2
    2
      Front/modules/ui_main.py

+ 142
- 0
Back/GS/GSExport.py Просмотреть файл

@@ -359,6 +359,128 @@ def export_initial_data(ui, db_path, utf_en, export_folder):
359 359
     # 保存工作簿
360 360
     wb.save(excel_path)
361 361
 
362
+def export_example_data(ui, db_path, utf_en, export_folder):
363
+    # 解码 utf_en
364
+    decoded_utf_en = utf_en.decode('utf-8') if isinstance(utf_en, bytes) else utf_en
365
+    excel_file_name = f"{os.path.splitext(decoded_utf_en)[0]}.xlsx"
366
+    excel_path = os.path.join(export_folder, excel_file_name)
367
+
368
+    # 创建一个新的工作簿和工作表
369
+    wb = openpyxl.Workbook()
370
+    ws = wb.active
371
+
372
+    # 连接到数据库
373
+    conn = sqlite3.connect(db_path)
374
+    cursor = conn.cursor()
375
+
376
+    # 查询GS_Input_Param表中的数据
377
+    query = """
378
+    SELECT New_ResultName, Last_ResultName, Avg_SL, Ms_Dir, Ms_WSL, SL_Count, Dir_Count, Scale_Value
379
+    FROM GS_Input_Param
380
+    WHERE TableName = ?
381
+    """
382
+    cursor.execute(query, (utf_en,))
383
+    result = cursor.fetchone()
384
+    # 创建样式来保留指定的小数位数
385
+    decimal_style_3 = NamedStyle(name="decimal_style_3")
386
+    decimal_style_3.number_format = '0.000'
387
+    decimal_style_3.alignment = Alignment(horizontal='center', vertical='center')
388
+
389
+    decimal_style_1 = NamedStyle(name="decimal_style_1")
390
+    decimal_style_1.number_format = '0.0'
391
+    decimal_style_1.alignment = Alignment(horizontal='center', vertical='center')
392
+
393
+    decimal_style_4 = NamedStyle(name="decimal_style_4")
394
+    decimal_style_4.number_format = '0.0000'
395
+    decimal_style_4.alignment = Alignment(horizontal='center', vertical='center')
396
+    if result:
397
+        new_result_name, last_result_name, avg_sl, ms_dir, ms_wsl, sl_count, dir_count, scale_value = result
398
+
399
+        # 填充数据到Excel并应用样式
400
+        ws['B1'] = new_result_name
401
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
402
+
403
+        ws['D1'] = last_result_name
404
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
405
+
406
+        ws['G1'] = avg_sl
407
+        ws['G1'].style = decimal_style_3
408
+
409
+        ws['G2'] = ms_dir
410
+        ws['G2'].alignment = Alignment(horizontal='center', vertical='center')
411
+
412
+        ws['G3'] = ms_wsl
413
+        ws['G3'].alignment = Alignment(horizontal='center', vertical='center')
414
+
415
+        ws['G4'] = sl_count
416
+        ws['G4'].style = decimal_style_1
417
+
418
+        ws['G5'] = dir_count
419
+        ws['G5'].style = decimal_style_1
420
+
421
+        ws['G6'] = scale_value
422
+        ws['G6'].style = decimal_style_4
423
+
424
+        # 合并单元格
425
+        ws.merge_cells('B1:C1')
426
+        ws.merge_cells('D1:E1')
427
+        # 设置列宽
428
+        ws.column_dimensions['B'].width = 12.5
429
+        ws.column_dimensions['C'].width = 12.5
430
+        ws.column_dimensions['D'].width = 12.5
431
+        ws.column_dimensions['E'].width = 12.5
432
+        ws.column_dimensions['F'].width = 22
433
+        ws.column_dimensions['G'].width = 21.5
434
+
435
+        # 设置B1, D1单元格居中显示
436
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
437
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
438
+
439
+    # 设置表头
440
+    headers = [
441
+        ("A2", "点名"),
442
+        ("B2", "高斯坐标x(m)"),
443
+        ("C2", "高斯坐标y(m)"),
444
+        ("D2", "高斯坐标x(m)"),
445
+        ("E2", "高斯坐标y(m)"),
446
+        ("F1", "平均边长(m)"),
447
+        ("F2", "方向值中误差(″)"),
448
+        ("F3", "最弱边边长相对中误差"),
449
+        ("F4", "网点总测边数"),
450
+        ("F5", "网点总方向观测数"),
451
+        ("F6", "缩放值")
452
+    ]
453
+
454
+    for cell, value in headers:
455
+        ws[cell] = value
456
+        ws[cell].alignment = Alignment(horizontal='center', vertical='center')
457
+    # 查询GS_Input_Point表中的数据
458
+    query_point = """
459
+    SELECT PointName, New_X, New_Y, Last_X, Last_Y
460
+    FROM GS_Input_Point
461
+    WHERE TableName = ?
462
+    """
463
+    cursor.execute(query_point, (utf_en,))
464
+    results_point = cursor.fetchall()
465
+    # 创建一个样式来强制保留四位小数
466
+    decimal_style = NamedStyle(name="decimal_style")
467
+    decimal_style.number_format = '0000.0000'
468
+    # 填充数据到Excel
469
+    for idx, (point_name, new_x, new_y, last_x, last_y) in enumerate(results_point, start=3):
470
+        ws[f'A{idx}'] = point_name
471
+        ws[f'B{idx}'] = round(new_x, 4)
472
+        ws[f'C{idx}'] = round(new_y, 4)
473
+        ws[f'D{idx}'] = round(last_x, 4)
474
+        ws[f'E{idx}'] = round(last_y, 4)
475
+
476
+        # 应用样式以保留四位小数
477
+        ws[f'B{idx}'].style = decimal_style
478
+        ws[f'C{idx}'].style = decimal_style
479
+        ws[f'D{idx}'].style = decimal_style
480
+        ws[f'E{idx}'].style = decimal_style
481
+
482
+    # 保存工作簿
483
+    wb.save(excel_path)
362 484
 
363 485
 # 主函数 写入excel文件
364 486
 def main_function(ui, dbpath, excelname):
@@ -394,3 +516,23 @@ def main_function_initial(ui, dbpath, excelname_utf8):
394 516
             QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
395 517
     except Exception as e:
396 518
         QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
519
+
520
+
521
+def main_function_example(ui, dbpath, excelname_utf8):
522
+    # 获取应用的安装目录
523
+    app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
524
+    export_folder = os.path.join(app_install_dir, 'Example')
525
+
526
+    # 如果 Export 文件夹不存在,则创建它
527
+    if not os.path.exists(export_folder):
528
+        os.makedirs(export_folder)
529
+    try:
530
+        export_example_data(ui, dbpath, excelname_utf8, export_folder)
531
+        QMessageBox.information(ui, '成功', f'示例文件已成功导出到 {export_folder}')
532
+    except PermissionError as e:
533
+        if e.errno == 13:
534
+            QMessageBox.critical(ui, '错误', '请确认文件没有被其他程序(如Excel、文本编辑器等)打开')
535
+        else:
536
+            QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
537
+    except Exception as e:
538
+        QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')

+ 156
- 1
Back/WD/WDExport.py Просмотреть файл

@@ -335,6 +335,143 @@ def export_initial_data(ui, db_path, utf_en, export_folder):
335 335
     wb.save(excel_path)
336 336
 
337 337
 
338
+def export_example_data(ui, db_path, utf_en, export_folder):
339
+    # 解码文件名并去掉后缀名
340
+    decoded_utf_en = utf_en.decode('utf-8') if isinstance(utf_en, bytes) else utf_en
341
+    excel_file_name = f"{os.path.splitext(decoded_utf_en)[0]}.xlsx"
342
+    excel_path = os.path.join(export_folder, excel_file_name)
343
+
344
+    # 创建一个新的工作簿和工作表
345
+    wb = openpyxl.Workbook()
346
+    ws = wb.active
347
+
348
+    # 连接到数据库
349
+    conn = sqlite3.connect(db_path)
350
+    cursor = conn.cursor()
351
+
352
+    # 查询WD_Input_Param表中的数据
353
+    query = """
354
+    SELECT New_ResultName, Last_ResultName, Avg_SL, Ms_Dir, SL_Count, Dir_Count, Scale_Value, First_ResultName
355
+    FROM WD_Input_Param
356
+    WHERE TableName = ?
357
+    """
358
+    cursor.execute(query, (utf_en,))
359
+    result = cursor.fetchone()
360
+
361
+    # 创建样式来保留指定的小数位数
362
+    decimal_style_3 = NamedStyle(name="decimal_style_3")
363
+    decimal_style_3.number_format = '0.0000'
364
+    decimal_style_3.alignment = Alignment(horizontal='center', vertical='center')
365
+
366
+    decimal_style_1 = NamedStyle(name="decimal_style_1")
367
+    decimal_style_1.number_format = '0.0'
368
+    decimal_style_1.alignment = Alignment(horizontal='center', vertical='center')
369
+
370
+    decimal_style_4 = NamedStyle(name="decimal_style_4")
371
+    decimal_style_4.number_format = '0'
372
+    decimal_style_4.alignment = Alignment(horizontal='center', vertical='center')
373
+
374
+    if result:
375
+        new_result_name, last_result_name, avg_sl, ms_dir, sl_count, dir_count, scale_value, first_result_name = result
376
+
377
+        # 填充数据到Excel并应用样式
378
+        ws['B1'] = new_result_name
379
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
380
+
381
+        ws['D1'] = last_result_name
382
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
383
+
384
+        ws['G1'] = first_result_name  # 新增:填入First_ResultName
385
+        ws['G1'].alignment = Alignment(horizontal='center', vertical='center')  # 新增:设置居中对齐
386
+
387
+        ws['J1'] = avg_sl
388
+        ws['J1'].style = decimal_style_3
389
+
390
+        ws['J2'] = ms_dir
391
+        ws['J2'].alignment = Alignment(horizontal='center', vertical='center')
392
+
393
+        ws['J3'] = sl_count
394
+        ws['J3'].style = decimal_style_4
395
+
396
+        ws['J4'] = dir_count
397
+        ws['J4'].style = decimal_style_4
398
+
399
+        ws['J5'] = scale_value
400
+        ws['J5'].style = decimal_style_4
401
+
402
+        # 合并单元格
403
+        ws.merge_cells('B1:C1')
404
+        ws.merge_cells('D1:E1')
405
+        ws.merge_cells('G1:H1')
406
+
407
+        # 设置列宽
408
+        ws.column_dimensions['B'].width = 12.5
409
+        ws.column_dimensions['C'].width = 12.5
410
+        ws.column_dimensions['D'].width = 12.5
411
+        ws.column_dimensions['E'].width = 12.5
412
+        ws.column_dimensions['F'].width = 3
413
+        ws.column_dimensions['F'].alignment = Alignment(horizontal='center', vertical='center')  # 新增:设置F列居中对齐
414
+        ws.column_dimensions['G'].width = 12.5
415
+        ws.column_dimensions['H'].width = 12.5
416
+        ws.column_dimensions['I'].width = 14
417
+        ws.column_dimensions['J'].width = 10
418
+
419
+    # 设置表头
420
+    headers = [
421
+        ("A2", "点名"),
422
+        ("B2", "高斯坐标x(m)"),
423
+        ("C2", "高斯坐标y(m)"),
424
+        ("D2", "高斯坐标x(m)"),
425
+        ("E2", "高斯坐标y(m)"),
426
+        ("F2", "权"),
427
+        ("G2", "高斯坐标x(m)"),
428
+        ("H2", "高斯坐标y(m)"),
429
+        ("I1", "平均边长"),
430
+        ("I2", "方向值中误差"),
431
+        ("I3", "总边数"),
432
+        ("I4", "总方向数"),
433
+        ("I5", "缩放值")
434
+    ]
435
+
436
+    for cell, value in headers:
437
+        ws[cell] = value
438
+        ws[cell].alignment = Alignment(horizontal='center', vertical='center')
439
+
440
+    # 查询WD_Input_Point表中的数据
441
+    query_point = """
442
+    SELECT PointName, New_X, New_Y, Last_X, Last_Y, Wight, First_X, First_Y
443
+    FROM WD_Input_Point
444
+    WHERE TableName = ?
445
+    """
446
+    cursor.execute(query_point, (utf_en,))
447
+    results_point = cursor.fetchall()
448
+
449
+    # 创建一个样式来强制保留四位小数
450
+    decimal_style = NamedStyle(name="decimal_style")
451
+    decimal_style.number_format = '0000.0000'
452
+
453
+    # 填充数据到Excel
454
+    for idx, (point_name, new_x, new_y, last_x, last_y, wight, first_x, first_y) in enumerate(results_point, start=3):
455
+        ws[f'A{idx}'] = point_name
456
+        ws[f'B{idx}'] = round(new_x, 4)
457
+        ws[f'C{idx}'] = round(new_y, 4)
458
+        ws[f'D{idx}'] = round(last_x, 4)
459
+        ws[f'E{idx}'] = round(last_y, 4)
460
+        ws[f'F{idx}'] = round(wight, 0)  # 新增
461
+        ws[f'G{idx}'] = round(first_x, 4)  # 新增
462
+        ws[f'H{idx}'] = round(first_y, 4)  # 新增
463
+
464
+        # 应用样式以保留四位小数
465
+        ws[f'B{idx}'].style = decimal_style
466
+        ws[f'C{idx}'].style = decimal_style
467
+        ws[f'D{idx}'].style = decimal_style
468
+        ws[f'E{idx}'].style = decimal_style
469
+        ws[f'G{idx}'].style = decimal_style  # 新增
470
+        ws[f'H{idx}'].style = decimal_style  # 新增
471
+
472
+    # 保存工作簿
473
+    wb.save(excel_path)
474
+
338 475
 # 主函数 写入excel文件
339 476
 def main_function(ui, dbpath, excelname):
340 477
     # 获取应用的安装目录
@@ -369,4 +506,22 @@ def main_function_initial(ui, dbpath, excelname_utf8):
369 506
             QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
370 507
     except Exception as e:
371 508
         QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
372
-        print(e)
509
+
510
+def main_function_example(ui, dbpath, excelname_utf8):
511
+    # 获取应用的安装目录
512
+    app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
513
+    export_folder = os.path.join(app_install_dir, 'Example')
514
+
515
+    # 如果 Export 文件夹不存在,则创建它
516
+    if not os.path.exists(export_folder):
517
+        os.makedirs(export_folder)
518
+    try:
519
+        export_example_data(ui, dbpath, excelname_utf8, export_folder)
520
+        QMessageBox.information(ui, '成功', f'示例文件已成功导出到 {export_folder}')
521
+    except PermissionError as e:
522
+        if e.errno == 13:
523
+            QMessageBox.critical(ui, '错误', '请确认文件没有被其他程序(如Excel、文本编辑器等)打开')
524
+        else:
525
+            QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
526
+    except Exception as e:
527
+        QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')

+ 31
- 13
Front/main.py Просмотреть файл

@@ -30,6 +30,8 @@ import platform
30 30
 
31 31
 import Back.Program_Run.database_operations
32 32
 from Back.GC.GCExport import main_function_example
33
+from Back.GS.GSExport import main_function_example
34
+from Back.WD.WDExport import main_function_example
33 35
 # IMPORT / GUI AND MODULES AND WIDGETS
34 36
 # ///////////////////////////////////////////////////////////////
35 37
 from modules import *
@@ -924,7 +926,7 @@ class MainWindow(QMainWindow):
924 926
         search_button.itemDoubleClicked.connect(self.itembuttonClick1)
925 927
         # 隐藏默认label
926 928
         self.ui.defaultLabel.setVisible(False)
927
-        #清空搜索框内容
929
+        # 清空搜索框内容
928 930
         self.ui.lineEdit_2.clear()
929 931
 
930 932
     # 全树的item展示
@@ -1006,7 +1008,7 @@ class MainWindow(QMainWindow):
1006 1008
             if widgets.lineEdit_2.text():
1007 1009
                 # 模拟 search 按钮点击事件
1008 1010
                 self.ui.search.click()
1009
-            #用于编辑完成操作
1011
+            # 用于编辑完成操作
1010 1012
             else:
1011 1013
                 self.update_dataclicked()
1012 1014
         else:
@@ -1049,19 +1051,19 @@ class MainWindow(QMainWindow):
1049 1051
 
1050 1052
     def update_dataclicked(self):
1051 1053
         inputmodel = self.ui.resultTableView1.model()
1052
-        #转化为list
1054
+        # 转化为list
1053 1055
         list1 = []
1054 1056
         ii = 0
1055 1057
         while ii < inputmodel.rowCount():
1056 1058
             list2 = []
1057 1059
             jj = 0
1058 1060
             while jj < inputmodel.columnCount():
1059
-                index1 = inputmodel.index(ii,jj)
1061
+                index1 = inputmodel.index(ii, jj)
1060 1062
                 list2.append(inputmodel.data(index1))
1061 1063
                 jj = jj + 1
1062 1064
             list1.append(list2)
1063 1065
             ii = ii + 1
1064
-        #读取对应的db和方法
1066
+        # 读取对应的db和方法
1065 1067
         select_item = self.ui.allTreeWidget.currentItem()
1066 1068
         if select_item == None:
1067 1069
             select_item = self.ui.qureyTreeWidget.currentItem()
@@ -1090,7 +1092,7 @@ class MainWindow(QMainWindow):
1090 1092
         # 关联自定义的右键菜单方法到customContextMenuRequested信号
1091 1093
         self.ui.resultTableView1.customContextMenuRequested.connect(self.show_custom_context_menu)
1092 1094
 
1093
-        #把编辑状态改回去
1095
+        # 把编辑状态改回去
1094 1096
         self.ui.resultTableView1.setEditTriggers(QTableView.EditTrigger.NoEditTriggers)
1095 1097
 
1096 1098
     def edit_dataclicked(self):
@@ -1108,21 +1110,37 @@ class MainWindow(QMainWindow):
1108 1110
         self.ui.resultTableView1.setEditTriggers(QTableView.EditTrigger.AllEditTriggers)
1109 1111
 
1110 1112
 
1113
+    def on_download_1_clicked(self):
1114
+        # 获取应用的安装目录
1115
+        app_install_dir = os.path.dirname(os.path.abspath(__file__))
1116
+        # 构建数据库路径
1117
+        db_path = os.path.join(app_install_dir, '..', 'SQL', 'DataBase.db')
1118
+        db_path = os.path.abspath(db_path)
1119
+        file_name = "平面控制网稳定性计算示例表.xlsx"
1120
+        excelname_utf8 = file_name.encode('utf-8')
1121
+        Back.WD.WDExport.main_function_example(self, db_path, excelname_utf8)
1122
+
1123
+    def on_download_2_clicked(self):
1124
+        # 获取应用的安装目录
1125
+        app_install_dir = os.path.dirname(os.path.abspath(__file__))
1126
+        # 构建数据库路径
1127
+        db_path = os.path.join(app_install_dir, '..', 'SQL', 'DataBase.db')
1128
+        db_path = os.path.abspath(db_path)
1129
+        file_name = "控制网复测平面基准计算示例表.xlsx"
1130
+        excelname_utf8 = file_name.encode('utf-8')
1131
+        Back.GS.GSExport.main_function_example(self, db_path, excelname_utf8)
1132
+
1133
+
1111 1134
     def on_download_3_clicked(self):
1112 1135
         # 获取应用的安装目录
1113 1136
         app_install_dir = os.path.dirname(os.path.abspath(__file__))
1114 1137
         # 构建数据库路径
1115
-        db_path = os.path.join(app_install_dir, '..',  'SQL', 'DataBase.db')
1138
+        db_path = os.path.join(app_install_dir, '..', 'SQL', 'DataBase.db')
1116 1139
         db_path = os.path.abspath(db_path)
1117 1140
         file_name = "水准测段高差稳定计算示例文件.xlsx"
1118 1141
         utf_en = file_name.encode('utf-8')
1119
-        print(db_path)
1120 1142
         # 调用main_function_example函数
1121
-        main_function_example(self, utf_en, db_path)
1122
-
1123
-
1124
-
1125
-
1143
+        Back.GC.GCExport.main_function_example(self, utf_en, db_path)
1126 1144
 
1127 1145
 
1128 1146
 if __name__ == "__main__":

+ 2
- 2
Front/modules/ui_main.py Просмотреть файл

@@ -1448,12 +1448,12 @@ class Ui_MainWindow(object):
1448 1448
         self.verticalLayout_22.setObjectName(u"verticalLayout_22")
1449 1449
         self.download_1 = QPushButton(self.row_3)
1450 1450
         self.download_1.setObjectName(u"download_1")
1451
-
1451
+        self.download_1.clicked.connect(self.main_window.on_download_1_clicked)
1452 1452
         self.verticalLayout_22.addWidget(self.download_1)
1453 1453
         # 下载2
1454 1454
         self.download_2 = QPushButton(self.row_3)
1455 1455
         self.download_2.setObjectName(u"download_2")
1456
-
1456
+        self.download_2.clicked.connect(self.main_window.on_download_2_clicked)
1457 1457
         self.verticalLayout_22.addWidget(self.download_2)
1458 1458
         # 下载3
1459 1459
         self.download_3 = QPushButton(self.row_3)

Загрузка…
Отмена
Сохранить