打开和写入excel文件

Posted 啊峰哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打开和写入excel文件相关的知识,希望对你有一定的参考价值。

一.使用win32读取excel内容

# -*- coding: utf-8 -*-
from win32com import client as wc
def open_excel():
    excel = wc.Dispatch(EXCEL.Application)                 #使用excel程序   
    excel.Visible = 0                                        #不打开excel界面
    my_excel = excel.Workbooks.Open(u新建表格.xls)          #指定表格路径

    my_sheet = my_excel.Sheets(Sheet1)                     #打开表格中的sheet1,根据实际情况而定

    for i in range(my_sheet.UsedRange.Rows.Count):           #遍历sheet1中的表格列数
        for j in range(my_sheet.UsedRange.Columns.Count):    #遍历sheet1中的表格行数
            print my_sheet.Cells(i + 1, j + 1).Value         #读取相应的坐标表格值,并打印
    my_excel.Close()                                         #关闭表格
    excel.Quit()
open_excel()

 

二.使用xlrd读取excel内容(比第一种方法快很多)

# -*- coding: utf-8 -*-
from xlrd import open_workbook

def open_excel2():                                                       #快很多
    wb = open_workbook(u‘新建表格.xls)                                   #指定路径
    for s in wb.sheets():                                                #遍历sheet表
        for row in range(s.nrows):                                       #遍历列
            for col in range(s.ncols):                                   #遍历行
                print s.cell(row,col).value                              #打印值

open_excel()

 

以上是关于打开和写入excel文件的主要内容,如果未能解决你的问题,请参考以下文章

用Java写入Excel

如何用java把数据写入到excel

Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据

python怎么读取txt 格式excel文件

什么函数能够将结构化数据写入excel文件中

java 将页面内容写入excel文件中并可以将其下载到本地任意位置