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

完成了GC中对.xls文件的支持

wzp 3 месяцев назад
Родитель
Сommit
44a756ae19
4 измененных файлов: 84 добавлений и 7 удалений
  1. 32
    2
      Back/GC/GC.py
  2. 20
    0
      Back/GC/GCExport.py
  3. 7
    0
      Back/GC/GCcompute.py
  4. 25
    5
      Back/GC/GCshow.py

+ 32
- 2
Back/GC/GC.py Просмотреть файл

@@ -1,10 +1,30 @@
1 1
 import openpyxl
2 2
 import sqlite3
3 3
 import os
4
+import pandas as pd
4 5
 from openpyxl import Workbook
5 6
 from PySide6.QtWidgets import QApplication, QMessageBox
6 7
 
7 8
 
9
+# 添加文件转换函数
10
+def convert_xls_to_xlsx(file_path):
11
+    # 使用 pandas 读取 .xls 文件
12
+    df = pd.read_excel(file_path, engine='xlrd')
13
+    # 创建一个新的 .xlsx 文件路径
14
+    xlsx_file_path = file_path.replace('.xls', '.xlsx')
15
+    # 将数据保存为 .xlsx 文件
16
+    df.to_excel(xlsx_file_path, index=False)
17
+    return xlsx_file_path
18
+
19
+
20
+# 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
21
+def confirm_xls_to_xlsx_conversion(file_name):
22
+    response = QMessageBox.question(None, "确认转换",
23
+                                    f".xls文件将默认转换为.xlsx文件进行计算,系统会重新生成一个.xlsx文件副本用以计算,不会删除您本来的.xls文件,点击“OK”开始上传文件,点击“Cancel”取消上传文件",
24
+                                    QMessageBox.Ok | QMessageBox.Cancel)
25
+    return response == QMessageBox.Ok
26
+
27
+
8 28
 # 读取指定单元格的数据
9 29
 def read_cells_from_excel(cells, sheet):
10 30
     """
@@ -106,8 +126,9 @@ def insert_into_database(file_name, cell_values, ms_station, last_station_count,
106 126
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
107 127
                 """,
108 128
                 (
109
-                file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station, last_station_count,
110
-                new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
129
+                    file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station,
130
+                    last_station_count,
131
+                    new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
111 132
             )
112 133
             QMessageBox.information(None, "提示",
113 134
                                     f"文件 '{file_name}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
@@ -192,6 +213,15 @@ def insert_data_into_database(data, file_name, cell_values, db_path, file_name_u
192 213
 
193 214
 # 主函数 读取excel文件
194 215
 def main_function(file_path, db_path):
216
+    # 检查文件扩展名
217
+    if file_path.endswith('.xls'):
218
+        # 弹窗提示用户 .xls 文件将转换为 .xlsx 文件
219
+        if not confirm_xls_to_xlsx_conversion(os.path.basename(file_path)):
220
+            print("用户取消了转换操作")
221
+            return
222
+
223
+        # 转换 .xls 文件为 .xlsx 文件
224
+        file_path = convert_xls_to_xlsx(file_path)
195 225
     # 调用读取Excel文件的函数
196 226
     file_name = os.path.basename(file_path)
197 227
     file_name_utf8 = file_name.encode('utf-8')

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

@@ -272,8 +272,26 @@ def export_initial_data(ui, utf_en, db_path, export_folder):
272 272
         else:
273 273
             QMessageBox.critical(ui, '错误', f'导出初始数据过程中发生错误: {str(e)}')
274 274
 
275
+def process_utf_en(utf_en):
276
+    # 确保 utf_en 是字节字符串
277
+    if not isinstance(utf_en, bytes):
278
+        utf_en = utf_en.encode('utf-8')
279
+
280
+    # 将字节字符串解码为普通字符串
281
+    file_name = utf_en.decode('utf-8')
282
+
283
+    # 检查文件后缀名并进行相应处理
284
+    if file_name.endswith('.xls'):
285
+        # 去掉 .xls 后缀并添加 .xlsx 后缀
286
+        file_name = file_name[:-4] + '.xlsx'
287
+
288
+    # 将处理后的字符串重新编码为字节字符串
289
+    utf_en = file_name.encode('utf-8')
290
+
291
+    return utf_en
275 292
 
276 293
 def main_function_initial(ui, utf_en, db_path):
294
+
277 295
     # 获取应用的安装目录
278 296
     app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
279 297
     export_folder = os.path.join(app_install_dir, 'Export')
@@ -287,6 +305,8 @@ def main_function_initial(ui, utf_en, db_path):
287 305
 
288 306
 
289 307
 def main_function_result(ui, utf_en, db_path):
308
+    # 处理 utf_en
309
+    utf_en = process_utf_en(utf_en)
290 310
     # 获取应用的安装目录
291 311
     app_install_dir = os.path.dirname(os.path.abspath(__file__))  # 假设脚本文件位于应用的安装目录下
292 312
     export_folder = os.path.join(app_install_dir, 'Export')

+ 7
- 0
Back/GC/GCcompute.py Просмотреть файл

@@ -278,6 +278,12 @@ def main_function(file_path, db_path):
278 278
         file_name = os.path.basename(file_path)
279 279
         file_name_utf8 = file_name.encode('utf-8')
280 280
 
281
+        # 检查文件后缀名
282
+        if file_name.endswith('.xls'):
283
+            # 去掉 .xls 后缀并添加 .xlsx 后缀
284
+            file_name = file_name[:-4] + '.xlsx'
285
+            file_name_utf8 = file_name.encode('utf-8')
286
+
281 287
         # 处理查询结果
282 288
         abs_last_hdiffs = calculate_absolute_height_differences(db_path, file_name_utf8, "Last_HDiff")
283 289
         abs_new_hdiffs = calculate_absolute_height_differences(db_path, file_name_utf8, "New_HDiff")
@@ -359,3 +365,4 @@ def main_function(file_path, db_path):
359 365
 
360 366
     except Exception as e:
361 367
         QMessageBox.critical(None, "错误", f"文件 '{file_name}' 计算失败!错误信息: {str(e)}")
368
+        print(f"文件 '{file_name}' 计算失败!错误信息: {str(e)}")

+ 25
- 5
Back/GC/GCshow.py Просмотреть файл

@@ -5,11 +5,29 @@ from PySide6.QtGui import QStandardItemModel, QStandardItem
5 5
 from PySide6.QtCore import QItemSelectionModel
6 6
 
7 7
 
8
-def main_function(ui, db_path, utf_en):
8
+def process_utf_en(utf_en):
9 9
     # 确保 utf_en 是字节字符串
10 10
     if not isinstance(utf_en, bytes):
11 11
         utf_en = utf_en.encode('utf-8')
12 12
 
13
+    # 将字节字符串解码为普通字符串
14
+    file_name = utf_en.decode('utf-8')
15
+
16
+    # 检查文件后缀名并进行相应处理
17
+    if file_name.endswith('.xls'):
18
+        # 去掉 .xls 后缀并添加 .xlsx 后缀
19
+        file_name = file_name[:-4] + '.xlsx'
20
+
21
+    # 将处理后的字符串重新编码为字节字符串
22
+    utf_en = file_name.encode('utf-8')
23
+
24
+    return utf_en
25
+
26
+
27
+def main_function(ui, db_path, utf_en):
28
+    # 处理 utf_en
29
+    utf_en = process_utf_en(utf_en)
30
+
13 31
     # 设置 QTabWidget 的可见性和标签文本
14 32
     tabs_to_show = [(0, '水准测段高差计算成果表')]
15 33
     for index in range(ui.tabWidget.count()):
@@ -53,7 +71,8 @@ def main_function(ui, db_path, utf_en):
53 71
     model.setHorizontalHeaderLabels(headers)
54 72
 
55 73
     # 使用 itertools.zip_longest 处理长度不一致的情况
56
-    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result, fillvalue=(None, None)):
74
+    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
75
+                                                               fillvalue=(None, None)):
57 76
         items = []
58 77
         for i, item in enumerate(row_data):
59 78
             if item is None:
@@ -142,7 +161,8 @@ def search_show_function(ui, db_path, utf_en):
142 161
     input_model.setHorizontalHeaderLabels(input_headers)
143 162
 
144 163
     # 使用 itertools.zip_longest 处理长度不一致的情况
145
-    for row_data, param_data in itertools.zip_longest(input_result, param_result, fillvalue=(None, None, None, None, None, None, None, None, None)):
164
+    for row_data, param_data in itertools.zip_longest(input_result, param_result,
165
+                                                      fillvalue=(None, None, None, None, None, None, None, None, None)):
146 166
         items = []
147 167
         for i, item in enumerate(row_data):
148 168
             if item is None:
@@ -235,7 +255,8 @@ def search_show_function(ui, db_path, utf_en):
235 255
     output_model.setHorizontalHeaderLabels(output_headers)
236 256
 
237 257
     # 使用 itertools.zip_longest 处理长度不一致的情况
238
-    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result, fillvalue=(None, None)):
258
+    for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
259
+                                                               fillvalue=(None, None)):
239 260
         items = []
240 261
         for i, item in enumerate(row_data):
241 262
             if item is None:
@@ -283,4 +304,3 @@ def search_show_function(ui, db_path, utf_en):
283 304
 
284 305
     # 隐藏 QLabel
285 306
     ui.default_remind1.hide()
286
-

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