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