Django框架---上传Excel文件并读取

def upload_file(request):
    if request.method == "POST":
        File = request.FILES.get("files", None)
        if File is None:
            return HttpResponse("请选择需要上传的文件")
        else:
            # with open("./%s" % File.name, 'wb+') as  f:
            #     for chunk in File.chunks():
            #         f.write(chunk)
            f = open(os.path.join(BASE_DIR, File.name), 'wb')
            for chunk in File.chunks():
                f.write(chunk)
            f.close()
            excel_type = f.name.split('.')[-1]
            # if excel_type in ['xlsx', 'xls','csv']:
            #     # 开始解析上传的excel表格
            #     # file_contents = f.open(os.path.join(BASE_DIR, File.name), 'r')
            #     wb = xlrd.open_workbook(filename=File, file_contents=f.read())
            #     table = wb.sheets()[0]
            #     rows = table.nrows  # 总行数
            # f.close()
            return render(request, "index.html")
    else:
        return render(request, "index.html")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MonkeyTest</title>
</head>
<body>
    <form method="post" action="http://127.0.0.1:8000/users/upload_file/" enctype="multipart/form-data">
        {% csrf_token %}
        <h1>请上传文件</h1>
        请输入执行人:<input type="text" name="peoples">
        请输入部门:<input type="text" name="versions"><br>
        请上传文件<input type="file" name="files"><br>
        <input type="submit" value="上传">
        <br>
        <b><small>文件上传执行结果:</small></b><hr>
    </form>
    <table border="1">
        <thead>
            <tr>
                <td>ID</td>
                <td>执行人</td>
                <td>部门</td>
                <td>xxx</td>
                <td>是否执行完</td>
                <td>备注</td>
            </tr>
        </thead>
        <tbody>


        </tbody>
    </table>


</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="案例/css/main.css">
    <title>Document</title>
    <script src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$('#btn01').click(function(){
				$('#pop').show(500)
				return false
			})
			
			$('#shutoff').click(function(){
					$('#pop').hide(500)
			})
			
			$('body').click(function(){
				$('#pop').fadeOut(500)
			})
			
//				//点击高亮的盒子,不要触发body的隐藏事件
			$('.pop_con').click(function(){
				//只阻止事件冒泡,什么程序都不写
				return false
				alert(1)
			})
			
			
			
			
			
		})
	</script>
        
</head>
<body>
    <input type="button" value="弹出弹框" id="btn01">

    <div class="pop_main" id="pop">
        <div class="pop_con">
            <div class="pop_title">
                <h3>系统提示</h3>
                <a href="#" id="shutoff">×</a>
            </div>
            <div class="pop_detail">
                <p class="pop_text">亲!请关注近期的优惠活动!</p>
                <form action='uploadFile.php' enctype="multipart/form-data" type='post'  target="uploader1">
				<input type='file'>
				<button>提交</button>
</form>




<script type="text/javascript">

</script>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42322206/article/details/101605203