|
@@ -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)}')
|