控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GC.py 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import openpyxl
  2. import sqlite3
  3. import os
  4. from openpyxl import Workbook
  5. from PySide6.QtWidgets import QApplication, QMessageBox
  6. # 读取指定单元格的数据
  7. def read_cells_from_excel(cells, sheet):
  8. """
  9. 读取指定单元格的数据并返回字典。
  10. """
  11. cell_values = {}
  12. for cell in cells:
  13. cell_values[cell] = sheet[cell].value
  14. return cell_values
  15. # 获取最后一行有数字的单元格值
  16. def get_last_numeric_cell_value(sheet, column):
  17. """
  18. 获取指定列的最后一行有数字的单元格的值。
  19. """
  20. last_row = sheet.max_row
  21. for row in range(last_row, 0, -1):
  22. cell_value = sheet[f"{column}{row}"].value
  23. if isinstance(cell_value, (int, float)):
  24. return cell_value
  25. return None
  26. # 计算指定列中数值型数据的总和
  27. def calculate_column_sum(sheet, column):
  28. """
  29. 计算指定列中数值型数据的总和。
  30. """
  31. total_sum = 0
  32. for row in range(1, sheet.max_row + 1):
  33. cell_value = sheet[f"{column}{row}"].value
  34. if isinstance(cell_value, (int, float)):
  35. total_sum += cell_value
  36. return total_sum
  37. # 读取Excel文件并计算公式结果
  38. def load_and_calculate_excel(file_path):
  39. # 加载Excel文件并计算单元格H1的公式结果。
  40. workbook = openpyxl.load_workbook(file_path, data_only=True)
  41. sheet = workbook.active
  42. h1_value = sheet['H1'].value
  43. return h1_value
  44. # 弹窗提示用户是否更新数据
  45. def ask_for_update(file_name):
  46. response = QMessageBox.question(None, "提示", f"检测到数据库中存在相同文件 '{file_name}',是(Yes)否(No)更新数据?",
  47. QMessageBox.Yes | QMessageBox.No)
  48. return response == QMessageBox.Yes
  49. # 数据库插入函数,插入数据到GC_Input_Param中
  50. def insert_into_database(file_name, cell_values, ms_station, last_station_count, new_station_count, last_sum_hdiff,
  51. new_sum_hdiff,
  52. last_sum_rlen, new_sum_rlen, db_path,file_name_utf8):
  53. """
  54. 将文件名和结果名称插入或更新到数据库的GC_Input_Param表中。
  55. """
  56. database = db_path
  57. conn = sqlite3.connect(database=database)
  58. cursor = conn.cursor()
  59. try:
  60. # 查询是否已存在相同的记录
  61. cursor.execute(
  62. "SELECT * FROM GC_Input_Param WHERE TableName = ?",
  63. (file_name_utf8,)
  64. )
  65. existing_record = cursor.fetchone()
  66. if existing_record:
  67. # 弹窗询问用户是否更新数据
  68. if not ask_for_update(file_name):
  69. print("用户选择不更新数据")
  70. return
  71. # 更新现有记录
  72. cursor.execute(
  73. """
  74. UPDATE GC_Input_Param
  75. SET Last_ResultName = ?, ObservationLevel = ?, New_ResultName = ?, Ms_Station = ?,
  76. Last_StationCount = ?, New_StationCount = ?, Last_SumHDiff = ?, New_SumHDiff = ?,
  77. Last_SumRLen = ?, New_SumRLen = ?
  78. WHERE TableName = ?
  79. """,
  80. (cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station, last_station_count,
  81. new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen, file_name_utf8)
  82. )
  83. QMessageBox.information(None,"提示",
  84. f"文件 '{file_name}' 已成功更新到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以重新计算数据")
  85. else:
  86. # 插入新记录
  87. cursor.execute(
  88. """
  89. INSERT INTO GC_Input_Param (TableName, Last_ResultName, ObservationLevel, New_ResultName, Ms_Station,
  90. Last_StationCount, New_StationCount, Last_SumHDiff, New_SumHDiff,
  91. Last_SumRLen, New_SumRLen)
  92. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  93. """,
  94. (file_name_utf8, cell_values['D1'], cell_values['C1'], cell_values['I1'], ms_station, last_station_count,
  95. new_station_count, last_sum_hdiff, new_sum_hdiff, last_sum_rlen, new_sum_rlen)
  96. )
  97. QMessageBox.information(None,"提示",
  98. f"文件 '{file_name}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
  99. conn.commit()
  100. except sqlite3.Error as e:
  101. print(f"插入或更新文件名时发生错误: {e}")
  102. except Exception as e:
  103. print(f"处理文件时发生错误: {e}")
  104. finally:
  105. conn.close()
  106. # 遍历获取序号的数据
  107. def get_data_from_excel(file_path):
  108. """
  109. 从 Excel 文件中获取对应列的数据。
  110. """
  111. workbook = openpyxl.load_workbook(file_path)
  112. sheet = workbook.active
  113. data = []
  114. for row in range(3, sheet.max_row + 1):
  115. a_value = sheet[f"A{row}"].value
  116. b_value = sheet[f"B{row}"].value
  117. c_value = sheet[f"C{row}"].value
  118. d_value = sheet[f"D{row}"].value
  119. e_value = sheet[f"E{row}"].value
  120. f_value = sheet[f"F{row}"].value
  121. g_value = sheet[f"G{row}"].value
  122. h_value = sheet[f"H{row}"].value
  123. i_value = sheet[f"I{row}"].value
  124. j_value = sheet[f"J{row}"].value
  125. if a_value is not None or f_value is not None:
  126. data.append((a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value))
  127. return data
  128. # 插入点数据到GC_Input_Point中
  129. def insert_data_into_database(data, file_name, cell_values, db_path,file_name_utf8):
  130. """
  131. 将数据插入到数据库表 GC_Input_Point 中。如果 TableName 相同,则清空表,
  132. 然后插入新的数据。
  133. """
  134. database = db_path
  135. conn = sqlite3.connect(database=database)
  136. cursor = conn.cursor()
  137. try:
  138. # 检查是否已存在相同的 TableName
  139. cursor.execute(
  140. "SELECT * FROM GC_Input_Point WHERE TableName = ?",
  141. (file_name_utf8,)
  142. )
  143. existing_record = cursor.fetchone()
  144. if existing_record:
  145. # 如果存在相同的 TableName,清空表
  146. cursor.execute("DELETE FROM GC_Input_Point WHERE TableName = ?", (file_name_utf8,))
  147. conn.commit()
  148. # 插入新数据
  149. for a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value in data:
  150. cursor.execute(
  151. """
  152. INSERT INTO GC_Input_Point (Last_ID, Last_SPName, Last_EPName,Last_HDiff,Last_RLen, New_ID, New_SPName, New_EPName,New_HDiff,New_RLen, TableName, Last_ResultName, New_ResultName)
  153. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  154. """,
  155. (a_value, b_value, c_value, d_value, e_value, f_value, g_value, h_value, i_value, j_value, file_name_utf8,
  156. cell_values['D1'],
  157. cell_values['I1'])
  158. )
  159. conn.commit()
  160. except sqlite3.Error as e:
  161. print(f"插入或更新点数据时发生错误: {e}")
  162. except Exception as e:
  163. print(f"处理文件时发生错误: {e}")
  164. finally:
  165. conn.close()
  166. # 主函数 读取excel文件
  167. def main_function(file_path, db_path):
  168. # 调用读取Excel文件的函数
  169. file_name = os.path.basename(file_path)
  170. file_name_utf8 = file_name.encode('utf-8')
  171. # 加载工作簿
  172. workbook = openpyxl.load_workbook(file_path)
  173. sheet = workbook.active
  174. # 计算单元格 H1 的公式结果
  175. ms_station = load_and_calculate_excel(file_path)
  176. # 读取指定单元格的数据
  177. cells_to_read = ['D1', 'C1', 'I1']
  178. cell_values = read_cells_from_excel(cells_to_read, sheet)
  179. # 获取最后一行有数字的单元格值
  180. last_station_count = get_last_numeric_cell_value(sheet, 'A')
  181. new_station_count = get_last_numeric_cell_value(sheet, 'F')
  182. # 计算 D 列和 I 列中数值型数据的总和 总高差
  183. last_sum_hdiff = calculate_column_sum(sheet, 'D')
  184. new_sum_hdiff = calculate_column_sum(sheet, 'I')
  185. # 总路线长
  186. last_sum_rlen = calculate_column_sum(sheet, 'E')
  187. new_sum_rlen = calculate_column_sum(sheet, 'J')
  188. # 插入点的数组
  189. data = get_data_from_excel(file_path)
  190. # 调用插入数据库的函数
  191. if file_name:
  192. insert_into_database(file_name, cell_values, ms_station, last_station_count, new_station_count, last_sum_hdiff,
  193. new_sum_hdiff, last_sum_rlen, new_sum_rlen, db_path,file_name_utf8)
  194. insert_data_into_database(data, file_name, cell_values, db_path,file_name_utf8)
  195. else:
  196. print("没有数据可插入数据库")