#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlwt
import os class ExcelHelper(object):
@staticmethod
def create_excel(column_heads, row_items, file_path):
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)
column_count = len(column_heads)
row_count = len(row_items)
# 写入列标题
for column_id in range(column_count):
sheet.write(0, column_id, column_heads[column_id])
# 遍历所有行
for row_id in range(row_count):
row_item = row_items[row_id]
# 遍历每行的所有列
for column_id in range(column_count):
# 将每一行的对应列写sheet中
sheet.write(row_id + 1, column_id, row_item[column_id])
# 保存文件
if os.path.exists(file_path):
os.remove(file_path)
book.save(file_path) def demo():
column_heads = ["姓名", "学校", "出生日期", "年龄"]
row_items = [
["张删", "北京大学", "1988-01-02", 29],
["王二小", "清华大学", "1989-01-02", 28],
]
file_path = r"d:\t1.xls"
ExcelHelper.create_excel(
column_heads=column_heads,
row_items=row_items,
file_path=file_path
)
print("file {0} was created".format(file_path)) if __name__ == '__main__':
demo()

引入xlwt需要安装openpyxl包

=========================================================================

python36--将数据保存为excel的更多相关文章

  1. mfc 导出数据保存成excel和txt格式

    最近做了一些东西,项目到了收尾的工作.不过这次我没有参与到控件机器的功能的那一部分,都是主管自己写的.不过,所有的控件重写都是由我来做的.还有数据库这一方面是我和主管共同完成的.不过还不错,主管写一部 ...

  2. 将matlab数据保存为excel文件

    摘录网址:https://blog.csdn.net/wangh0802/article/details/70312415 参考网址:https://jingyan.baidu.com/article ...

  3. Qt将表格table保存为excel(odbc方式)

    首先是保存excel的方法,可参照: http://dzmlmszp.blog.163.com/blog/static/179271962014819111812531/ ok,进入正题. 现在我有一 ...

  4. pandans导出Excel并将数据保存到不同的Sheet表中

    数据存在mongodb中,按照类别导出到Excel文件,问题是想把同一类的数据放到一个sheet表中,最后只导出到一个excel文件中# coding=utf-8import pandas as pd ...

  5. 【HTML5版】导出Table数据并保存为Excel

    首发我的博客 http://blog.meathill.com/tech/js/export-table-data-into-a-excel-file.html 最近接到这么个需求,要把<tab ...

  6. 如何使用免费控件将Word表格中的数据导入到Excel中

    我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...

  7. 将List下载到本地保存为Excel

    直接附上代码 /// <summary> /// 将List保存为Excel /// </summary> /// <typeparam name="T&quo ...

  8. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  9. Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010

    摘  要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...

随机推荐

  1. samtools

    samtools 用法 samtools <command> [options] command 见以下列表, 每个 command 的 options 也不同 dict faidx in ...

  2. IOS初级:NSKeyedArchiver

    NSKeyedArchiver对象归档 首先要实现<NScoding>里面的两个代理方法initWithCoder,encodeWithCoder @property (nonatomic ...

  3. Reduce 和 Transduce 的含义

    一.reduce 的用法 reduce是一种数组运算,通常用于将数组的所有成员"累积"为一个值. var arr = [1, 2, 3, 4]; var sum = (a, b) ...

  4. BP神经网络在python下的自主搭建梳理

    本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...

  5. 【转】ssh-copy-id帮你建立信任

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc. == 对于做运维的同学来说,给两台UNIX/Linux机器建立ssh信任关系是 ...

  6. Python10/17-re模块/hashlib模块/logging模块

    import logging # 1.日志的级别# logging.debug("这是一个调试信息") # 10# logging.info("常规信息") # ...

  7. keras环境

    Windows 7 python3.6 在安装 Keras 之前, 需要确认自己已经安装好了 Numpy 和 Scipy. 安装个anaconda3就可以搞定 因为 Keras 是基于 Tensorf ...

  8. 2018.10.25 bzoj4517: [Sdoi2016]排列计数(组合数学)

    传送门 组合数学简单题. Ans=(nm)∗1Ans=\binom {n} {m}*1Ans=(mn​)∗1~(n−m)(n-m)(n−m)的错排数. 前面的直接线性筛逆元求. 后面的错排数递推式本蒟 ...

  9. Interrouter Signals

    summary of traditional NoC interrouter signals summary of SMART interrouter signals flit_valid and f ...

  10. Oracle数据库常用的sql语句

    1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...