網站首頁 編程語言 正文
前言:
最近公司領導要統計技術部門在各個業務條線花費的工時百分比,而 jira 當前的 Tempo 插件只能統計個人工時。于是就寫了個報表工具,將 jira 中導出的個人工時excel表格 導入數據庫,在后端處理各個業務工時占比。后來研究了 jira 的 API 文檔 ,放棄了之前的思路,直接調用 jira API 處理數據 ,這個先不談。這篇博客主要介紹 Django 上傳文件,然后解析 excel 導入數據庫。
一、上傳文件:
將文件上傳到服務器指定路徑,其實很簡單,一共有三個步驟:
1.配置 setting.py
# 文件上傳配置
UPLOAD_ROOT = os.path.join(BASE_DIR,'upload')
2.前端代碼如下,使用 <form> 表單提交,"/upload/" 路由配置在 urls 中,這個就不再多說了。
{% extends 'base.html' %}
{% block content %}
<body>
<form id="form" enctype="multipart/form-data" action="/upload/" method="post">
<p><input type="file" name="file"></p>
<input type="submit" name="提交">
</form>
</body>
{% endblock %}
3.后端代碼如下,這段代碼可以上傳任意格式的文件,沒有校驗文件類型。
@csrf_exempt
def upload(request):
# 根name取 file 的值
file = request.FILES.get('file')
logger.log().info('uplaod:%s'% file)
# 創建upload文件夾
if not os.path.exists(settings.UPLOAD_ROOT):
os.makedirs(settings.UPLOAD_ROOT)
try:
if file is None:
return HttpResponse('請選擇要上傳的文件')
# 循環二進制寫入
with open(settings.UPLOAD_ROOT + "/" + file.name, 'wb') as f:
for i in file.readlines():
f.write(i)
except Exception as e:
return HttpResponse(e)
return HttpResponse('上傳成功')
二、解析 excel 導入數據庫
1.文件上傳結束后,接下來讀取剛上傳到服務器的 excel 表格,然后寫入數據庫。所以整個后端代碼是這樣的:
# 將excel數據寫入mysql
def wrdb(filename):
# 打開上傳 excel 表格
readboot = xlrd.open_workbook(settings.UPLOAD_ROOT + "/" + filename)
sheet = readboot.sheet_by_index(0)
#獲取excel的行和列
nrows = sheet.nrows
ncols = sheet.ncols
print(ncols,nrows)
sql = "insert into working_hours (jobnum,name,workingtime,category,project,date,createtime) VALUES"
for i in range(1,nrows):
row = sheet.row_values(i)
jobnum = row[4]
name = row[5]
workingtime = row[2]
category = row[8]
project = row[1]
date = xldate_as_datetime(row[3],0).strftime('%Y/%m/%d')
values = "('%s','%s','%s','%s','%s','%s','%s')"%(jobnum,name,workingtime,category,project,date,datetime.datetime.now())
sql = sql + values +","
# 為了提高運行效率,一次性把數據 insert 進數據庫
sql = sql[:-1]
# 寫入數據庫
# DataConnection 是自定義的公共模塊,用的是第三方庫,用來操作數據庫。沒有用 ORM ,后續有 group by 等復雜 sql 不好操作。
DataConnection.MysqlConnection().insert('work',sql)
@csrf_exempt
def upload(request):
# 根name取 file 的值
file = request.FILES.get('file')
print('uplaod:%s'% file)
# 創建upload文件夾
if not os.path.exists(settings.UPLOAD_ROOT):
os.makedirs(settings.UPLOAD_ROOT)
try:
if file is None:
return HttpResponse('請選擇要上傳的文件')
# 循環二進制寫入
with open(settings.UPLOAD_ROOT + "/" + file.name, 'wb') as f:
for i in file.readlines():
f.write(i)
# 寫入 mysql
wrdb(file.name)
except Exception as e:
return HttpResponse(e)
return HttpResponse('導入成功')
2.數據導入后,通過一些處理就得到了我們想要的數據。報表其中之一的餅圖:
原文鏈接:https://www.cnblogs.com/shenh/p/12197747.html
相關推薦
- 2022-12-10 深入了解C++11中promise和future的使用_C 語言
- 2022-11-19 項目適?Oracle改造及SSL安全性配置問題匯總詳解_oracle
- 2022-05-02 C語言學生成績管理系統源碼_C 語言
- 2022-06-23 C#中的小數和百分數計算與byte數組操作_C#教程
- 2023-01-08 C++?Boost?Assign超詳細講解_C 語言
- 2022-06-13 Nginx配置?location模塊實現路由(反向代理、重定向)功能_nginx
- 2022-06-16 Python語法學習之正則表達式的使用詳解_python
- 2022-12-23 C++中關于union的使用方法說明_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支