Excel是一种常用的电子表格软件,广泛应用于金融、商业和教育等领域。它提供了强大的数据处理和分析功能,可进行各种计算和公式运算,并能创建各种类型的图表和可视化数据。Excel的灵活性使其成为处理和管理数据的重要工具。本文将介绍如何使用 Spire.XLS for Python 通过代码创建Excel文件,以及读取Excel数据。
Spire.XLS for Python是一款第三方 Python Excel组件,可用于在各种 Python 平台创建、管理、操作及转换 Excel 文档。要完成此任务,需要先通过以下 pip 命令将Spire.XLS for Python 和 plum-dispatch v1.7.4安装到 VS Code中。
pip install Spire.XLS-for-Python
pip install plum-dispatch==1.7.4
官网也提供了详细安装教程,具体可参考: 如何在 VS Code中安装 Spire.XLS for Python
Spire.XLS for Python 提供了各种各样的类、属性和方法可供开发人员对工作簿、工作表、某个单元格范围甚至指定的单元格进行操作。要创建一个Excel文件,并写入具体数据,可参考以下Python代码。
from spire.xls import *
from spire.common import *
wb = Workbook()
wb.Worksheets.Clear()
sheet = wb.Worksheets.Add("销售表")
sheet.Range["A1:G1"].Merge()
sheet.Range["A1"].Text = "销售明细表"
sheet.Range["A1"].HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["A1"].VerticalAlignment = VerticalAlignType.Center
sheet.Range["A1"].Style.Font.IsBold = True
sheet.Range["A1"].Style.Font.Size = 13
sheet.Rows[0].RowHeight = 30
sheet.Range["A2"].Text = "订单编号"
sheet.Range["B2"].Text = "下单日期"
sheet.Range["C2"].Text = "客户名称"
sheet.Range["D2"].Text = "产品名称"
sheet.Range["E2"].Text = "订单金额"
sheet.Range["F2"].Text = "交货日期"
sheet.Range["G2"].Text = "订单状态"
sheet.Range["A3"].Text = "DXA-001"
sheet.Range["B3"].Text = "2022/12/1"
sheet.Range["C3"].Text = "客户1"
sheet.Range["D3"].Text = "产品1"
sheet.Range["E3"].Text = "5000"
sheet.Range["F3"].Text = "2022/12/12"
sheet.Range["G3"].Text = "已完成"
sheet.Range["A4"].Text = "DXA-002"
sheet.Range["B4"].Text = "2022/12/1"
sheet.Range["C4"].Text = "客户2"
sheet.Range["D4"].Text = "产品2"
sheet.Range["E4"].Text = "3000"
sheet.Range["F4"].Text = "2022/12/15"
sheet.Range["G4"].Text = "进行中"
sheet.Range["A5"].Text = "DXA-003"
sheet.Range["B5"].Text = "2022/12/18"
sheet.Range["C5"].Text = "客户3"
sheet.Range["D5"].Text = "产品3"
sheet.Range["E5"].Text = "1500"
sheet.Range["F5"].Text = "2022/12/28"
sheet.Range["G5"].Text = "未完成"
sheet.Range["A6"].Text = "DXA-004"
sheet.Range["B6"].Text = "2022/12/19"
sheet.Range["C6"].Text = "客户4"
sheet.Range["D6"].Text = "产品4"
sheet.Range["E6"].Text = "2500"
sheet.Range["F6"].Text = "2022/12/24"
sheet.Range["G6"].Text = "已取消"
sheet.Range["A7"].Text = "DXA-005"
sheet.Range["B7"].Text = "2022/12/20"
sheet.Range["C7"].Text = "客户5"
sheet.Range["D7"].Text = "产品5"
sheet.Range["E7"].Text = "5000"
sheet.Range["F7"].Text = "2022/1/5"
sheet.Range["G7"].Text = "进行中"
sheet.Range["A2:G7"].RowHeight = 20
sheet.SetColumnWidth(2, 11)
sheet.SetColumnWidth(3, 9)
sheet.SetColumnWidth(4, 8)
sheet.SetColumnWidth(6, 11)
sheet.SetColumnWidth(7, 10)
sheet.Range["A2:G7"].BorderAround(LineStyleType.Medium)
sheet.Range["A2:G7"].BorderInside(LineStyleType.Thin)
sheet.Range["A2:G2"].BorderAround(LineStyleType.Medium)
sheet.Range["A2:G7"].Borders.KnownColor = ExcelColors.Black
sheet.Range["A2:G2"].Style.Color = Color.get_Gold()
sheet.Range["A2:G2"].Style.Font.IsBold = True
wb.SaveToFile("创建Excel.xlsx", FileFormat.Version2016)
生成文件:
Spire.XLS for Python提供的 Worksheet.Range.Value 属性能以字符串形式返回指定单元格的数字值或文本值。要读取一个Excel工作表中的数据,可参考以下Python代码。
from spire.xls import *
from spire.common import *
wb = Workbook()
wb.LoadFromFile("创建Excel.xlsx");
sheet = wb.Worksheets[0]
locatedRange = sheet.AllocatedRange
for i in range(len(sheet.Rows)):
for j in range(len(locatedRange.Rows\[i\].Columns)):
# 获取单元格的数据
print(locatedRange\[i + 1, j + 1\].Value + " ", end='')
print("")
返回结果:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章