|
| 1 | +# Export เหมือนไฟล์ "export_data_09" |
| 2 | +# แต่คราวนี้จะต้องตกแต่งเนื้อหาในไฟล์ Excel ให้ดูดีมีระดับ |
| 3 | + |
| 4 | +import mysql.connector |
| 5 | +from openpyxl import Workbook |
| 6 | +from openpyxl.styles import Font, NamedStyle, Alignment |
| 7 | +from openpyxl.styles.fills import PatternFill, FILL_SOLID |
| 8 | +from openpyxl.styles.borders import Border, Side, BORDER_THIN |
| 9 | + |
| 10 | +def run(): |
| 11 | + # Database |
| 12 | + # - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ) |
| 13 | + db = mysql.connector.connect( |
| 14 | + host="localhost", |
| 15 | + port=3306, |
| 16 | + user="root", |
| 17 | + password="password1234", |
| 18 | + database='golf_want_to_buy_completed' |
| 19 | + ) |
| 20 | + cursor = db.cursor() |
| 21 | + |
| 22 | + # - โหลดข้อมูลสินค้าที่รวมประเภทสินค้ามาด้วย |
| 23 | + sql = ''' |
| 24 | + SELECT p.id AS id, p.title AS title, p.price AS price, c.title AS category |
| 25 | + FROM products AS p |
| 26 | + LEFT JOIN categories AS c |
| 27 | + ON p.category_id = c.id |
| 28 | + ''' |
| 29 | + cursor.execute(sql) |
| 30 | + products = cursor.fetchall() |
| 31 | + |
| 32 | + # - โหลดข้อมูลประเภทสินค้า พร้อมนับจำนวนสินค้าด้วย |
| 33 | + sql = ''' |
| 34 | + SELECT c.id AS id, c.title AS title, COUNT(p.id) AS products_count |
| 35 | + FROM categories AS c |
| 36 | + LEFT JOIN products AS p |
| 37 | + ON c.id = p.category_id |
| 38 | + GROUP BY c.id |
| 39 | + ''' |
| 40 | + cursor.execute(sql) |
| 41 | + categories = cursor.fetchall() |
| 42 | + |
| 43 | + # Excel |
| 44 | + # - การตกแต่งทั้งหมด |
| 45 | + border = Border( |
| 46 | + top=Side(border_style=BORDER_THIN, color='333333'), |
| 47 | + right=Side(border_style=BORDER_THIN, color='333333'), |
| 48 | + bottom=Side(border_style=BORDER_THIN, color='333333'), |
| 49 | + left=Side(border_style=BORDER_THIN, color='333333') |
| 50 | + ) |
| 51 | + |
| 52 | + header_style = NamedStyle(name='header') |
| 53 | + header_style.fill = PatternFill(fill_type=FILL_SOLID, fgColor='EEEEEE') |
| 54 | + header_style.border = border |
| 55 | + header_style.font = Font(name='Helvetica Neue', bold=True, size=16) |
| 56 | + header_style.alignment = Alignment(vertical='center') |
| 57 | + |
| 58 | + row_style = NamedStyle(name='row') |
| 59 | + row_style.border = border |
| 60 | + row_style.font = Font(name='Helvetica Neue', size=16) |
| 61 | + row_style.alignment = Alignment(vertical='center') |
| 62 | + |
| 63 | + # - สร้างไฟล์ใหม่ |
| 64 | + workbook = Workbook() |
| 65 | + |
| 66 | + # - สร้างชีทสำหรับสินค้า |
| 67 | + products_sheet = workbook.active |
| 68 | + products_sheet.title = 'สินค้า' |
| 69 | + products_sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ประเภทสินค้า']) |
| 70 | + |
| 71 | + # - ตกแต่งโดยรวม |
| 72 | + products_sheet.row_dimensions[1].height = 32 |
| 73 | + products_sheet.column_dimensions['A'].width = 10 |
| 74 | + products_sheet.column_dimensions['B'].width = 30 |
| 75 | + products_sheet.column_dimensions['C'].width = 18 |
| 76 | + products_sheet.column_dimensions['D'].width = 30 |
| 77 | + |
| 78 | + # - ตกแต่งแถวแรก (Header) |
| 79 | + product_header_row = products_sheet[1] |
| 80 | + for cell in product_header_row: |
| 81 | + cell.style = header_style |
| 82 | + |
| 83 | + # - ใส่ข้อมูลสินค้าทีละอัน เพิ่มลงไปทีละแถว |
| 84 | + for p in products: |
| 85 | + print(p) |
| 86 | + products_sheet.append(p) |
| 87 | + |
| 88 | + # - ตกแต่งแถวข้อมูล (Row) |
| 89 | + for (number, row) in enumerate(products_sheet.iter_rows(min_row=2), 2): |
| 90 | + products_sheet.row_dimensions[number].height = 32 |
| 91 | + for cell in row: |
| 92 | + cell.style = row_style |
| 93 | + |
| 94 | + # - สร้างชีทสำหรับประเภทสินค้า |
| 95 | + categories_sheet = workbook.create_sheet('ประเภทสินค้า', 1) |
| 96 | + categories_sheet.append(['ID', 'ประเภทสินค้า', 'จำนวนสินค้า']) |
| 97 | + |
| 98 | + # - ตกแต่งโดยรวม |
| 99 | + categories_sheet.row_dimensions[1].height = 32 |
| 100 | + categories_sheet.column_dimensions['A'].width = 10 |
| 101 | + categories_sheet.column_dimensions['B'].width = 30 |
| 102 | + categories_sheet.column_dimensions['C'].width = 20 |
| 103 | + |
| 104 | + # - ตกแต่งแถวแรก (Header) |
| 105 | + categories_header_row = categories_sheet[1] |
| 106 | + for cell in categories_header_row: |
| 107 | + cell.style = header_style |
| 108 | + |
| 109 | + # - ใส่ข้อมูลประเภทสินค้าทีละอัน เพิ่มลงไปทีละแถว |
| 110 | + for c in categories: |
| 111 | + print(c) |
| 112 | + categories_sheet.append(c) |
| 113 | + |
| 114 | + # - ตกแต่งแถวข้อมูล (Row) |
| 115 | + for (number, row) in enumerate(categories_sheet.iter_rows(min_row=2), 2): |
| 116 | + categories_sheet.row_dimensions[number].height = 32 |
| 117 | + for cell in row: |
| 118 | + cell.style = row_style |
| 119 | + |
| 120 | + # - Export ไฟล์ Excel |
| 121 | + workbook.save(filename="./files/exported_10.xlsx") |
| 122 | + |
| 123 | + # ปิดการเชื่อมต่อ Database |
| 124 | + cursor.close() |
| 125 | + db.close() |
0 commit comments