Last active
April 15, 2021 09:13
-
-
Save aspose-com-gists/3ed3378dc226c6ae79a7f1d0e9eac232 to your computer and use it in GitHub Desktop.
用Python创建Excel文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 创建一个新的XLSX工作簿 | |
wb = Workbook(FileFormatType.XLSX) | |
# 在单元格中插入值 | |
wb.getWorksheets().get(0).getCells().get("A1").putValue("Hello World!") | |
# 将工作簿另存为.xlsx文件 | |
wb.save("workbook.xlsx") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 创建一个新的XLSX工作簿 | |
wb = Workbook("workbook.xlsx") | |
# 在单元格中插入值 | |
wb.getWorksheets().get(0).getCells().get("A1").putValue("Location") | |
wb.getWorksheets().get(0).getCells().get("B1").putValue("Person") | |
wb.getWorksheets().get(0).getCells().get("A2").putValue("Home") | |
wb.getWorksheets().get(0).getCells().get("B2").putValue("abc") | |
wb.getWorksheets().get(0).getCells().get("A3").putValue("Office") | |
wb.getWorksheets().get(0).getCells().get("B3").putValue("xyz") | |
# 将工作簿另存为.xlsx文件 | |
wb.save("workbook-updated.xlsx") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 创建一个新的XLSX工作簿 | |
workbook = Workbook(FileFormatType.XLSX) | |
worksheet = workbook.getWorksheets().get(0) | |
# 将字符串值插入单元格 | |
worksheet.getCells().get("C2").setValue("Image") | |
# 设置第四行高度 | |
worksheet.getCells().setRowHeight(3, 150) | |
# 设置C列的宽度 | |
worksheet.getCells().setColumnWidth(3,50) | |
# 向D4单元格添加图片 | |
index = worksheet.getPictures().add(3, 3, "aspose-cells-for-python.png") | |
# 获取图片对象 | |
pic = worksheet.getPictures().get(index) | |
# 保存Excel文件 | |
workbook.save("workbook_with_image.xlsx") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 创建一个新的XLSX工作簿 | |
workbook = Workbook(FileFormatType.XLSX) | |
# 获取第一个工作表的参考 | |
worksheets = workbook.getWorksheets() | |
sheet = worksheets.get(0) | |
# 将一些样本值添加到单元格 | |
cells = sheet.getCells() | |
cell = cells.get("A1") | |
cell.setValue(50) | |
cell = cells.get("A2") | |
cell.setValue(100) | |
cell = cells.get("A3") | |
cell.setValue(150) | |
cell = cells.get("B1") | |
cell.setValue(4) | |
cell = cells.get("B2") | |
cell.setValue(20) | |
cell = cells.get("B3") | |
cell.setValue(50) | |
# 在工作表中获取图表 | |
charts = sheet.getCharts() | |
# 将图表添加到工作表 | |
chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5) | |
chart = charts.get(chartIndex) | |
# 将NSeries(图表数据源)添加到图表中,范围从“ A1”单元格到“ B3” | |
serieses = chart.getNSeries() | |
serieses.add("A1:B3", True) | |
# 写入Excel文件 | |
workbook.save("workbook_with_chart.xlsx") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 创建一个新的XLSX工作簿 | |
workbook = Workbook(FileFormatType.XLSX) | |
# 获取新添加的工作表的参考 | |
sheetIndex = workbook.getWorksheets().add() | |
sheet = workbook.getWorksheets().get(sheetIndex) | |
cells = sheet.getCells() | |
# 将值设置为单元格 | |
cell = cells.get("A1") | |
cell.setValue("Sport") | |
cell = cells.get("B1") | |
cell.setValue("Quarter") | |
cell = cells.get("C1") | |
cell.setValue("Sales") | |
cell = cells.get("A2") | |
cell.setValue("Golf") | |
cell = cells.get("A3") | |
cell.setValue("Golf") | |
cell = cells.get("A4") | |
cell.setValue("Tennis") | |
cell = cells.get("A5") | |
cell.setValue("Tennis") | |
cell = cells.get("A6") | |
cell.setValue("Tennis") | |
cell = cells.get("A7") | |
cell.setValue("Tennis") | |
cell = cells.get("A8") | |
cell.setValue("Golf") | |
cell = cells.get("B2") | |
cell.setValue("Qtr3") | |
cell = cells.get("B3") | |
cell.setValue("Qtr4") | |
cell = cells.get("B4") | |
cell.setValue("Qtr3") | |
cell = cells.get("B5") | |
cell.setValue("Qtr4") | |
cell = cells.get("B6") | |
cell.setValue("Qtr3") | |
cell = cells.get("B7") | |
cell.setValue("Qtr4") | |
cell = cells.get("B8") | |
cell.setValue("Qtr3") | |
cell = cells.get("C2") | |
cell.setValue(1500) | |
cell = cells.get("C3") | |
cell.setValue(2000) | |
cell = cells.get("C4") | |
cell.setValue(600) | |
cell = cells.get("C5") | |
cell.setValue(1500) | |
cell = cells.get("C6") | |
cell.setValue(4070) | |
cell = cells.get("C7") | |
cell.setValue(5000) | |
cell = cells.get("C8") | |
cell.setValue(6430) | |
pivotTables = sheet.getPivotTables() | |
# 将数据透视表添加到工作表 | |
index = pivotTables.add("=A1:C8", "E3", "PivotTable2") | |
# 访问新添加的数据透视表的实例 | |
pivotTable = pivotTables.get(index) | |
# 未显示行的总计。 | |
pivotTable.setRowGrand(False) | |
# 将第一个字段拖到行区域。 | |
pivotTable.addFieldToArea(PivotFieldType.ROW, 0) | |
# 将第二个字段拖到列区域。 | |
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1) | |
# 将第三个字段拖到数据区域。 | |
pivotTable.addFieldToArea(PivotFieldType.DATA, 2) | |
# 写入Excel文件 | |
workbook.save("workbook_with_pivot_table.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment