首頁  >  文章  >  後端開發  >  如何使用PHP和Vue開發線上員工考勤的資料匯出介面

如何使用PHP和Vue開發線上員工考勤的資料匯出介面

WBOY
WBOY原創
2023-09-24 11:57:26661瀏覽

如何使用PHP和Vue開發線上員工考勤的資料匯出介面

如何使用PHP和Vue開發線上員工考勤的資料匯出介面

#導語:隨著網路的快速發展,越來越多的企業開始轉向線上管理員工考勤,這為優化人力資源管理提供了很大的便利。在這篇文章中,我們將介紹如何使用PHP和Vue開發一個線上員工考勤的資料匯出介面,方便企業對考勤資料進行匯出和分析。

一、專案背景與需求分析

線上員工考勤管理系統的功能主要包括員工簽到、簽退、請假、加班等操作,並能夠產生可供匯出和分析的報表。本文重點在於如何開發一個資料匯出介面,以供管理員方便地匯出考勤資料。

此資料匯出介面需求如下:

  1. 顯示員工的考勤記錄列表,包括員工姓名、日期、簽到時間、簽退時間等資訊;
  2. 提供依照日期篩選考勤記錄的功能;
  3. 提供匯出考勤記錄的功能,支援Excel和CSV格式;
  4. 提供匯出全部考勤記錄的功能,同時也提供依照篩選條件匯出考勤記錄的功能。

二、技術選型

  1. 前端技術:使用Vue框架,實現資料的展示與條件篩選功能;
  2. 後端技術:使用PHP開發後端接口,負責查詢資料庫和產生匯出檔案;
  3. 資料庫:使用MySQL儲存員工考勤記錄。

三、前端開發

  1. 前端專案初始化

#使用Vue CLI工具初始化一個新的Vue專案。

$ npm install -g @vue/cli
$ vue create attendance-export
  1. 建立員工考勤清單元件

src/components目錄下建立一個名為AttendanceList.vue的組件,用於展示員工的考勤記錄清單。

<template>
  <div>
    <!-- 考勤记录列表 -->
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>日期</th>
          <th>签到时间</th>
          <th>签退时间</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="record in attendanceList" :key="record.id">
          <td>{{ record.name }}</td>
          <td>{{ record.date }}</td>
          <td>{{ record.startTime }}</td>
          <td>{{ record.endTime }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      attendanceList: [] // 考勤记录列表数据
    }
  },
  mounted() {
    this.getAttendanceList(); // 页面加载时获取考勤记录列表
  },
  methods: {
    getAttendanceList() {
      // 使用Vue的axios插件发送请求获取考勤记录数据
      axios.get('/api/attendance')
        .then(response => {
          this.attendanceList = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    }
  }
}
</script>

<style>
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 8px;
  border-bottom: 1px solid #ddd;
}
</style>
  1. 建立日期篩選元件

src/components目錄下建立一個名為DateFilter.vue的元件,用於實現依照日期篩選考勤記錄的功能。

<template>
  <div>
    <!-- 日期选择器 -->
    <input type="date" v-model="selectedDate" @input="filterByDate" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null // 选择的日期
    }
  },
  methods: {
    filterByDate() {
      // 使用Vue的$emit方法触发自定义事件,将选择的日期传递给父组件
      this.$emit('filter', this.selectedDate);
    }
  }
}
</script>
  1. 建立資料匯出元件

src/components目錄下建立一個名為DataExport.vue的元件,用於實現導出考勤記錄的功能。

<template>
  <div>
    <button @click="exportAll">导出全部</button>
    <button @click="exportFiltered">按条件导出</button>
  </div>
</template>

<script>
export default {
  methods: {
    exportAll() {
      // 发送导出全部考勤记录的请求
      axios.get('/api/export?type=csv')
        .then(response => {
          this.downloadFile(response.data, 'attendance.csv');
        })
        .catch(error => {
          console.error(error);
        });
    },
    exportFiltered() {
      // 发送按条件导出考勤记录的请求
      axios.get('/api/export?type=excel&date=' + this.selectedDate)
        .then(response => {
          this.downloadFile(response.data, 'attendance.xlsx');
        })
        .catch(error => {
          console.error(error);
        });
    },
    downloadFile(fileContent, fileName) {
      // 创建一个临时链接并下载文件
      const blob = new Blob([fileContent]);
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = fileName;
      link.click();
    }
  }
}
</script>

四、後端開發

  1. 建立資料庫表

#在MySQL資料庫中建立一個名為attendance的表,保存員工的考勤記錄。

CREATE TABLE attendance (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  date DATE NOT NULL,
  startTime TIME NOT NULL,
  endTime TIME NOT NULL
);
  1. 編寫後端接口

使用PHP編寫後端接口,負責查詢資料庫和產生匯出檔。

<?php
// 连接MySQL数据库
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "attendance";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// 查询考勤记录数据
function getAttendanceList($date = null) {
  global $conn;
  $sql = "SELECT * FROM attendance";
  if ($date) {
    $sql .= " WHERE date = '".$date."'";
  }
  $result = $conn->query($sql);
  $attendanceList = array();
  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      $attendanceList[] = $row;
    }
  }
  return $attendanceList;
}

// 导出考勤记录为Excel文件
function exportToExcel($attendanceList) {
  // 使用PHPExcel库生成Excel文件
  require_once 'PHPExcel.php';
  $objPHPExcel = new PHPExcel();
  $objPHPExcel->getActiveSheet()->fromArray($attendanceList, null, 'A1');
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  ob_start();
  $objWriter->save('php://output');
  $content = ob_get_clean();
  return $content;
}

// 导出考勤记录为CSV文件
function exportToCSV($attendanceList) {
  $content = "姓名,日期,签到时间,签退时间
";
  foreach ($attendanceList as $record) {
    $content .= $record['name'].','.$record['date'].','.$record['startTime'].','.$record['endTime']."
";
  }
  return $content;
}

// 根据请求参数调用不同的导出方法
if ($_GET['type'] == 'csv') {
  $attendanceList = getAttendanceList();
  $content = exportToCSV($attendanceList);
  header("Content-Disposition: attachment; filename=attendance.csv");
  header("Content-Type: text/csv");
  echo $content;
} else if ($_GET['type'] == 'excel') {
  $date = $_GET['date'];
  $attendanceList = getAttendanceList($date);
  $content = exportToExcel($attendanceList);
  header("Content-Disposition: attachment; filename=attendance.xlsx");
  header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  echo $content;
} else {
  header("HTTP/1.1 400 Bad Request");
}
?>

五、執行測試

  1. 啟動後端服務

#將上述PHP檔案命名為api.php,並將其放置到一個能被伺服器存取的目錄下。啟動一個PHP伺服器,例如使用php-cli指令:

$ php -S localhost:8000
  1. 執行前端專案
$ cd attendance-export
$ npm run serve
  1. 存取專案介面

在瀏覽器中存取http://localhost:8080,即可看到員工的考勤記錄清單、日期篩選和資料匯出按鈕。依需要進行操作,即可匯出考勤記錄。

結語:本文詳細介紹如何使用PHP和Vue開發一個線上員工考勤的資料匯出介面,透過前後端的配合,實現了考勤記錄的展示、篩選和匯出功能。希望本文能幫助您更好地應用PHP和Vue進行線上考勤管理系統的開發。

以上是如何使用PHP和Vue開發線上員工考勤的資料匯出介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn