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

完成了GS,WD的右键的全部导出功能,导出文件夹是软件安装目录下的Export文件夹中

wzp 3 месяцев назад
Родитель
Сommit
daaf80bd96
4 измененных файлов: 327 добавлений и 20 удалений
  1. 10
    3
      Back/GC/GCExport.py
  2. 151
    4
      Back/GS/GSExport.py
  3. 164
    12
      Back/WD/WDExport.py
  4. 2
    1
      Front/main.py

+ 10
- 3
Back/GC/GCExport.py Просмотреть файл

@@ -131,8 +131,10 @@ def export_result_data(ui, utf_en, db_path, export_folder):
131 131
         for cell_range in merge_cells:
132 132
             cell = ws[cell_range.split(':')[0]]
133 133
             cell.alignment = Alignment(horizontal='center', vertical='center')
134
+            # 获取当前时间并格式化为字符串,例如:20231010_143000
135
+        timestamp = time.strftime("%Y%m%d", time.localtime())
134 136
         # 保存 Excel 文件
135
-        excel_filename = f"{os.path.splitext(decoded_utf_en)[0]}-成果数据.xlsx"
137
+        excel_filename = f"{os.path.splitext(decoded_utf_en)[0]}-成果数据-{timestamp}.xlsx"
136 138
         excel_filepath = os.path.join(export_folder, excel_filename)
137 139
         wb.save(excel_filepath)
138 140
 
@@ -254,8 +256,10 @@ def export_initial_data(ui, utf_en, db_path, export_folder):
254 256
         ws.column_dimensions['E'].width = 11.5
255 257
         ws.column_dimensions['I'].width = 11.5
256 258
         ws.column_dimensions['J'].width = 11.5
259
+        # 获取当前时间并格式化为字符串,例如:20231010_143000
260
+        timestamp = time.strftime("%Y%m%d", time.localtime())
257 261
         # 设置文件名
258
-        excel_filename = f"{os.path.splitext(decoded_utf_en)[0]}-初始数据.xlsx"
262
+        excel_filename = f"{os.path.splitext(decoded_utf_en)[0]}-初始数据-{timestamp}.xlsx"
259 263
         excel_filepath = os.path.join(export_folder, excel_filename)
260 264
 
261 265
         # 保存 Excel 文件
@@ -263,7 +267,10 @@ def export_initial_data(ui, utf_en, db_path, export_folder):
263 267
 
264 268
         QMessageBox.information(ui, '成功', f'初始数据文件已成功导出到 {export_folder}')
265 269
     except Exception as e:
266
-        QMessageBox.critical(ui, '错误', f'导出初始数据过程中发生错误: {str(e)}')
270
+        if isinstance(e, PermissionError) or (isinstance(e, OSError) and e.errno == 13):
271
+            QMessageBox.critical(ui, '错误', '请确认文件没有被其他程序(如Excel、文本编辑器等)打开。')
272
+        else:
273
+            QMessageBox.critical(ui, '错误', f'导出初始数据过程中发生错误: {str(e)}')
267 274
 
268 275
 
269 276
 def main_function_initial(ui, utf_en, db_path):

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

@@ -2,10 +2,11 @@ import openpyxl
2 2
 import sqlite3
3 3
 import os
4 4
 import time
5
-from openpyxl.styles import Alignment
5
+from openpyxl.styles import Alignment, NamedStyle
6 6
 from PySide6.QtWidgets import QMessageBox
7 7
 
8 8
 
9
+# from openpyxl.styles.numbers import FORMAT_NUMBER_00
9 10
 def Arrange_Data1(list1):
10 11
     # 最终return的
11 12
     list2 = []
@@ -225,14 +226,140 @@ def openpyxl_write(file_name, dbpath, filename):
225 226
     ws3.cell(4, 1).value = str4
226 227
     str5 = '     X、Y为' + lastname + '已知系统的' + newname + '归算坐标'
227 228
     ws3.cell(5, 1).value = str5
228
-    # 保存
229
-    # wb.save(outpath)
229
+    # 获取当前时间并格式化为字符串,例如:20231010_143000
230
+    timestamp = time.strftime("%Y%m%d", time.localtime())
230 231
     # 保存 Excel 文件
231
-    excel_filename = f"控制网复测平面基准计算成果表{time.strftime('%Y%m%d_%H%M%S')}.xlsx"
232
+    excel_filename = f"{os.path.splitext(filename)[0]}-成果数据-{timestamp}.xlsx"
232 233
     excel_filepath = os.path.join(file_name, excel_filename)
233 234
     wb.save(excel_filepath)
234 235
 
235 236
 
237
+def export_initial_data(ui, db_path, utf_en, export_folder):
238
+    # 获取当前时间并格式化为字符串,例如:20231010_143000
239
+    timestamp = time.strftime("%Y%m%d", time.localtime())
240
+    # 解码 utf_en
241
+    decoded_utf_en = utf_en.decode('utf-8') if isinstance(utf_en, bytes) else utf_en
242
+    excel_file_name = f"{os.path.splitext(decoded_utf_en)[0]}-初始数据-{timestamp}.xlsx"
243
+    excel_path = os.path.join(export_folder, excel_file_name)
244
+
245
+    # 创建一个新的工作簿和工作表
246
+    wb = openpyxl.Workbook()
247
+    ws = wb.active
248
+
249
+    # 连接到数据库
250
+    conn = sqlite3.connect(db_path)
251
+    cursor = conn.cursor()
252
+
253
+    # 查询GS_Input_Param表中的数据
254
+    query = """
255
+    SELECT New_ResultName, Last_ResultName, Avg_SL, Ms_Dir, Ms_WSL, SL_Count, Dir_Count, Scale_Value
256
+    FROM GS_Input_Param
257
+    WHERE TableName = ?
258
+    """
259
+    cursor.execute(query, (utf_en,))
260
+    result = cursor.fetchone()
261
+    # 创建样式来保留指定的小数位数
262
+    decimal_style_3 = NamedStyle(name="decimal_style_3")
263
+    decimal_style_3.number_format = '0.000'
264
+    decimal_style_3.alignment = Alignment(horizontal='center', vertical='center')
265
+
266
+    decimal_style_1 = NamedStyle(name="decimal_style_1")
267
+    decimal_style_1.number_format = '0.0'
268
+    decimal_style_1.alignment = Alignment(horizontal='center', vertical='center')
269
+
270
+    decimal_style_4 = NamedStyle(name="decimal_style_4")
271
+    decimal_style_4.number_format = '0.0000'
272
+    decimal_style_4.alignment = Alignment(horizontal='center', vertical='center')
273
+    if result:
274
+        new_result_name, last_result_name, avg_sl, ms_dir, ms_wsl, sl_count, dir_count, scale_value = result
275
+
276
+        # 填充数据到Excel并应用样式
277
+        ws['B1'] = new_result_name
278
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
279
+
280
+        ws['D1'] = last_result_name
281
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
282
+
283
+        ws['G1'] = avg_sl
284
+        ws['G1'].style = decimal_style_3
285
+
286
+        ws['G2'] = ms_dir
287
+        ws['G2'].alignment = Alignment(horizontal='center', vertical='center')
288
+
289
+        ws['G3'] = ms_wsl
290
+        ws['G3'].alignment = Alignment(horizontal='center', vertical='center')
291
+
292
+        ws['G4'] = sl_count
293
+        ws['G4'].style = decimal_style_1
294
+
295
+        ws['G5'] = dir_count
296
+        ws['G5'].style = decimal_style_1
297
+
298
+        ws['G6'] = scale_value
299
+        ws['G6'].style = decimal_style_4
300
+
301
+        # 合并单元格
302
+        ws.merge_cells('B1:C1')
303
+        ws.merge_cells('D1:E1')
304
+        # 设置列宽
305
+        ws.column_dimensions['B'].width = 12.5
306
+        ws.column_dimensions['C'].width = 12.5
307
+        ws.column_dimensions['D'].width = 12.5
308
+        ws.column_dimensions['E'].width = 12.5
309
+        ws.column_dimensions['F'].width = 22
310
+        ws.column_dimensions['G'].width = 21.5
311
+
312
+        # 设置B1, D1单元格居中显示
313
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
314
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
315
+
316
+    # 设置表头
317
+    headers = [
318
+        ("A2", "点名"),
319
+        ("B2", "高斯坐标x(m)"),
320
+        ("C2", "高斯坐标y(m)"),
321
+        ("D2", "高斯坐标x(m)"),
322
+        ("E2", "高斯坐标y(m)"),
323
+        ("F1", "平均边长(m)"),
324
+        ("F2", "方向值中误差(″)"),
325
+        ("F3", "最弱边边长相对中误差"),
326
+        ("F4", "网点总测边数"),
327
+        ("F5", "网点总方向观测数"),
328
+        ("F6", "缩放值")
329
+    ]
330
+
331
+    for cell, value in headers:
332
+        ws[cell] = value
333
+        ws[cell].alignment = Alignment(horizontal='center', vertical='center')
334
+    # 查询GS_Input_Point表中的数据
335
+    query_point = """
336
+    SELECT PointName, New_X, New_Y, Last_X, Last_Y
337
+    FROM GS_Input_Point
338
+    WHERE TableName = ?
339
+    """
340
+    cursor.execute(query_point, (utf_en,))
341
+    results_point = cursor.fetchall()
342
+    # 创建一个样式来强制保留四位小数
343
+    decimal_style = NamedStyle(name="decimal_style")
344
+    decimal_style.number_format = '0000.0000'
345
+    # 填充数据到Excel
346
+    for idx, (point_name, new_x, new_y, last_x, last_y) in enumerate(results_point, start=3):
347
+        ws[f'A{idx}'] = point_name
348
+        ws[f'B{idx}'] = round(new_x, 4)
349
+        ws[f'C{idx}'] = round(new_y, 4)
350
+        ws[f'D{idx}'] = round(last_x, 4)
351
+        ws[f'E{idx}'] = round(last_y, 4)
352
+
353
+        # 应用样式以保留四位小数
354
+        ws[f'B{idx}'].style = decimal_style
355
+        ws[f'C{idx}'].style = decimal_style
356
+        ws[f'D{idx}'].style = decimal_style
357
+        ws[f'E{idx}'].style = decimal_style
358
+
359
+    # 保存工作簿
360
+    wb.save(excel_path)
361
+
362
+
236 363
 # 主函数 写入excel文件
237 364
 def main_function(ui, dbpath, excelname):
238 365
     # 获取应用的安装目录
@@ -247,3 +374,23 @@ def main_function(ui, dbpath, excelname):
247 374
         QMessageBox.information(ui, '成功', f'成果文件已成功导出到 {export_folder}')
248 375
     except Exception as e:
249 376
         QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
377
+
378
+
379
+def main_function_initial(ui, dbpath, excelname_utf8):
380
+    # 获取应用的安装目录
381
+    app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
382
+    export_folder = os.path.join(app_install_dir, 'Export')
383
+
384
+    # 如果 Export 文件夹不存在,则创建它
385
+    if not os.path.exists(export_folder):
386
+        os.makedirs(export_folder)
387
+    try:
388
+        export_initial_data(ui, dbpath, excelname_utf8, export_folder)
389
+        QMessageBox.information(ui, '成功', f'初始文件已成功导出到 {export_folder}')
390
+    except PermissionError as e:
391
+        if e.errno == 13:
392
+            QMessageBox.critical(ui, '错误', '请确认文件没有被其他程序(如Excel、文本编辑器等)打开')
393
+        else:
394
+            QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
395
+    except Exception as e:
396
+        QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')

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

@@ -2,7 +2,7 @@ import openpyxl
2 2
 import sqlite3
3 3
 import os
4 4
 import time
5
-from openpyxl.styles import Alignment
5
+from openpyxl.styles import Alignment, NamedStyle
6 6
 from PySide6.QtWidgets import QMessageBox
7 7
 
8 8
 
@@ -187,17 +187,156 @@ def openpyxl_write(file_name, dbpath, filename):
187 187
     ws2.cell(4, 1).value = str4
188 188
     str5 = '     X、Y为' + lastname + '已知系统的' + newname + '归算坐标'
189 189
     ws2.cell(5, 1).value = str5
190
+    # 获取当前时间并格式化为字符串,例如:20231010_143000
191
+    timestamp = time.strftime("%Y%m%d", time.localtime())
190 192
     # 保存 Excel 文件
191
-    excel_filename = f"平面控制网稳定性计算成果表{time.strftime('%Y%m%d_%H%M%S')}.xlsx"
193
+    excel_filename = f"{os.path.splitext(filename)[0]}-成果数据-{timestamp}.xlsx"
192 194
     excel_filepath = os.path.join(file_name, excel_filename)
193 195
     wb.save(excel_filepath)
194 196
 
195 197
 
198
+def export_initial_data(ui, db_path, utf_en, export_folder):
199
+    # 获取当前时间并格式化为字符串,例如:20231010_143000
200
+    timestamp = time.strftime("%Y%m%d", time.localtime())
201
+    # 解码文件名并去掉后缀名
202
+    decoded_utf_en = utf_en.decode('utf-8') if isinstance(utf_en, bytes) else utf_en
203
+    excel_file_name = f"{os.path.splitext(decoded_utf_en)[0]}-初始数据-{timestamp}.xlsx"
204
+    excel_path = os.path.join(export_folder, excel_file_name)
205
+
206
+    # 创建一个新的工作簿和工作表
207
+    wb = openpyxl.Workbook()
208
+    ws = wb.active
209
+
210
+    # 连接到数据库
211
+    conn = sqlite3.connect(db_path)
212
+    cursor = conn.cursor()
213
+
214
+    # 查询WD_Input_Param表中的数据
215
+    query = """
216
+    SELECT New_ResultName, Last_ResultName, Avg_SL, Ms_Dir, SL_Count, Dir_Count, Scale_Value, First_ResultName
217
+    FROM WD_Input_Param
218
+    WHERE TableName = ?
219
+    """
220
+    cursor.execute(query, (utf_en,))
221
+    result = cursor.fetchone()
222
+
223
+    # 创建样式来保留指定的小数位数
224
+    decimal_style_3 = NamedStyle(name="decimal_style_3")
225
+    decimal_style_3.number_format = '0.0000'
226
+    decimal_style_3.alignment = Alignment(horizontal='center', vertical='center')
227
+
228
+    decimal_style_1 = NamedStyle(name="decimal_style_1")
229
+    decimal_style_1.number_format = '0.0'
230
+    decimal_style_1.alignment = Alignment(horizontal='center', vertical='center')
231
+
232
+    decimal_style_4 = NamedStyle(name="decimal_style_4")
233
+    decimal_style_4.number_format = '0'
234
+    decimal_style_4.alignment = Alignment(horizontal='center', vertical='center')
235
+
236
+    if result:
237
+        new_result_name, last_result_name, avg_sl, ms_dir, sl_count, dir_count, scale_value, first_result_name = result
238
+
239
+        # 填充数据到Excel并应用样式
240
+        ws['B1'] = new_result_name
241
+        ws['B1'].alignment = Alignment(horizontal='center', vertical='center')
242
+
243
+        ws['D1'] = last_result_name
244
+        ws['D1'].alignment = Alignment(horizontal='center', vertical='center')
245
+
246
+        ws['G1'] = first_result_name  # 新增:填入First_ResultName
247
+        ws['G1'].alignment = Alignment(horizontal='center', vertical='center')  # 新增:设置居中对齐
248
+
249
+        ws['J1'] = avg_sl
250
+        ws['J1'].style = decimal_style_3
251
+
252
+        ws['J2'] = ms_dir
253
+        ws['J2'].alignment = Alignment(horizontal='center', vertical='center')
254
+
255
+        ws['J3'] = sl_count
256
+        ws['J3'].style = decimal_style_4
257
+
258
+        ws['J4'] = dir_count
259
+        ws['J4'].style = decimal_style_4
260
+
261
+        ws['J5'] = scale_value
262
+        ws['J5'].style = decimal_style_4
263
+
264
+        # 合并单元格
265
+        ws.merge_cells('B1:C1')
266
+        ws.merge_cells('D1:E1')
267
+        ws.merge_cells('G1:H1')
268
+
269
+        # 设置列宽
270
+        ws.column_dimensions['B'].width = 12.5
271
+        ws.column_dimensions['C'].width = 12.5
272
+        ws.column_dimensions['D'].width = 12.5
273
+        ws.column_dimensions['E'].width = 12.5
274
+        ws.column_dimensions['F'].width = 3
275
+        ws.column_dimensions['F'].alignment = Alignment(horizontal='center', vertical='center')  # 新增:设置F列居中对齐
276
+        ws.column_dimensions['G'].width = 12.5
277
+        ws.column_dimensions['H'].width = 12.5
278
+        ws.column_dimensions['I'].width = 14
279
+        ws.column_dimensions['J'].width = 10
280
+
281
+    # 设置表头
282
+    headers = [
283
+        ("A2", "点名"),
284
+        ("B2", "高斯坐标x(m)"),
285
+        ("C2", "高斯坐标y(m)"),
286
+        ("D2", "高斯坐标x(m)"),
287
+        ("E2", "高斯坐标y(m)"),
288
+        ("F2", "权"),
289
+        ("G2", "高斯坐标x(m)"),
290
+        ("H2", "高斯坐标y(m)"),
291
+        ("I1", "平均边长"),
292
+        ("I2", "方向值中误差"),
293
+        ("I3", "总边数"),
294
+        ("I4", "总方向数"),
295
+        ("I5", "缩放值")
296
+    ]
297
+
298
+    for cell, value in headers:
299
+        ws[cell] = value
300
+        ws[cell].alignment = Alignment(horizontal='center', vertical='center')
301
+
302
+    # 查询WD_Input_Point表中的数据
303
+    query_point = """
304
+    SELECT PointName, New_X, New_Y, Last_X, Last_Y, Wight, First_X, First_Y
305
+    FROM WD_Input_Point
306
+    WHERE TableName = ?
307
+    """
308
+    cursor.execute(query_point, (utf_en,))
309
+    results_point = cursor.fetchall()
310
+
311
+    # 创建一个样式来强制保留四位小数
312
+    decimal_style = NamedStyle(name="decimal_style")
313
+    decimal_style.number_format = '0000.0000'
314
+
315
+    # 填充数据到Excel
316
+    for idx, (point_name, new_x, new_y, last_x, last_y, wight, first_x, first_y) in enumerate(results_point, start=3):
317
+        ws[f'A{idx}'] = point_name
318
+        ws[f'B{idx}'] = round(new_x, 4)
319
+        ws[f'C{idx}'] = round(new_y, 4)
320
+        ws[f'D{idx}'] = round(last_x, 4)
321
+        ws[f'E{idx}'] = round(last_y, 4)
322
+        ws[f'F{idx}'] = round(wight, 0)  # 新增
323
+        ws[f'G{idx}'] = round(first_x, 4)  # 新增
324
+        ws[f'H{idx}'] = round(first_y, 4)  # 新增
325
+
326
+        # 应用样式以保留四位小数
327
+        ws[f'B{idx}'].style = decimal_style
328
+        ws[f'C{idx}'].style = decimal_style
329
+        ws[f'D{idx}'].style = decimal_style
330
+        ws[f'E{idx}'].style = decimal_style
331
+        ws[f'G{idx}'].style = decimal_style  # 新增
332
+        ws[f'H{idx}'].style = decimal_style  # 新增
333
+
334
+    # 保存工作簿
335
+    wb.save(excel_path)
336
+
337
+
196 338
 # 主函数 写入excel文件
197 339
 def main_function(ui, dbpath, excelname):
198
-    # dbpath = r"D:\4work_now\20240819GS\ControlNetwork\ControlNetwork\UI\SQL\DataBase.db"
199
-    # outpath = r'D:\4work_now\20240819GS\JPG\WD成果表.xlsx'
200
-    # file_path = r'D:\4work_now\20240819GS\test_wd.xls'
201 340
     # 获取应用的安装目录
202 341
     app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
203 342
     export_folder = os.path.join(app_install_dir, 'Export')
@@ -211,10 +350,23 @@ def main_function(ui, dbpath, excelname):
211 350
     except Exception as e:
212 351
         QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
213 352
 
214
-# dbpath = r"D:\4work_now\20240819GS\ControlNetwork\ControlNetwork\UI\SQL\DataBase.db"
215
-# # dbpath = r"D:/Code/ControlNetwork/UI/SQL/DataBase.db"
216
-# outpath = r'D:\4work_now\20240819GS\JPG\WD成果表.xlsx'
217
-# file_path = r'D:\4work_now\20240819GS\test_wd.xls'
218
-# file_name = os.path.basename(file_path)
219
-# # outpath为包含输出excel名字的全路径
220
-# openpyxl_write(outpath,dbpath,file_name)
353
+
354
+def main_function_initial(ui, dbpath, excelname_utf8):
355
+    # 获取应用的安装目录
356
+    app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
357
+    export_folder = os.path.join(app_install_dir, 'Export')
358
+
359
+    # 如果 Export 文件夹不存在,则创建它
360
+    if not os.path.exists(export_folder):
361
+        os.makedirs(export_folder)
362
+    try:
363
+        export_initial_data(ui, dbpath, excelname_utf8, export_folder)
364
+        QMessageBox.information(ui, '成功', f'初始文件已成功导出到 {export_folder}')
365
+    except PermissionError as e:
366
+        if e.errno == 13:
367
+            QMessageBox.critical(ui, '错误', '请确认文件没有被其他程序(如Excel、文本编辑器等)打开')
368
+        else:
369
+            QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
370
+    except Exception as e:
371
+        QMessageBox.critical(ui, '错误', f'导出过程中发生错误: {str(e)}')
372
+        print(e)

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

@@ -293,8 +293,10 @@ class ElTree(QWidget):
293 293
             GCExport.main_function_result(self, tablename_utf8, dbpath)
294 294
         elif parent_name == '控制网复测平面基准计算':
295 295
             GSExport.main_function(self, dbpath, tablename)
296
+            GSExport.main_function_initial(self, dbpath, tablename_utf8)
296 297
         elif parent_name == '平面控制网稳定性计算':
297 298
             WDExport.main_function(self, dbpath, tablename)
299
+            WDExport.main_function_initial(self, dbpath, tablename_utf8)
298 300
         else:
299 301
             QMessageBox.warning(self, '警告', '未知的父节点')
300 302
             return
@@ -479,7 +481,6 @@ class ElTree1(QWidget):
479 481
             main_window.refresh_tree()
480 482
 
481 483
 
482
-
483 484
 # class DatabaseChangeHandler(FileSystemEventHandler):
484 485
 #     def __init__(self, main_window):
485 486
 #         self.main_window = main_window

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