AO, cit, itat report download code commit.
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
from AppCode.Config import DBConfig
|
||||
import mysql.connector
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
|
||||
|
||||
class CITHandler:
|
||||
|
||||
@@ -75,3 +79,42 @@ class CITHandler:
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
|
||||
def cit_report_download(self, selected_year):
|
||||
try:
|
||||
# Call stored procedure
|
||||
self.cursor.callproc("GetCITByYear", [selected_year])
|
||||
|
||||
rows = []
|
||||
for result in self.cursor.stored_results():
|
||||
rows = result.fetchall()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# Excel output
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
|
||||
for i, (_, row) in enumerate(df.iterrows(), start=1):
|
||||
# Convert row to vertical format
|
||||
vertical_df = pd.DataFrame(row).reset_index()
|
||||
vertical_df.columns = ['Field', 'Value']
|
||||
|
||||
start_row = (i - 1) * (len(vertical_df) + 3) # gap between blocks
|
||||
vertical_df.to_excel(
|
||||
writer,
|
||||
sheet_name='CIT_Report',
|
||||
index=False,
|
||||
startrow=start_row
|
||||
)
|
||||
|
||||
output.seek(0)
|
||||
return output
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print("MySQL Error:", e)
|
||||
return None
|
||||
Reference in New Issue
Block a user