翻译|使用教程|编辑:李显亮|2020-10-15 10:46:23.287|阅读 416 次
概述:MS Excel电子表格使保留和共享大量表格数据变得更加容易。在本文中将使用Java从头开始创建MS Excel XLSX或XLS文件。此外,本文还将介绍如何在Excel工作表中更新现有的Excel文件,生成图表,应用公式以及添加数据透视表。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.Cells for JavaExcel电子表格处理API,它允许Java开发人员在自己的Java应用程序中嵌入可读取、写入和操作Excel电子表格的能力,而无需依赖Microsoft Excel。
MS Excel电子表格使保留和共享大量表格数据变得更加容易。不仅如此,您还可以执行各种操作,例如应用公式,生成图表和图形,对数据进行排序和过滤等等。
在本文中,将学习如何在Java应用程序中实现Excel自动化功能。阅读本文之后,将可以使用Java从头开始创建MS Excel XLSX或XLS文件。此外,本文还将介绍如何在Excel工作表中更新现有的Excel文件,生成图表,应用公式以及添加数据透视表。
Aspose.Cells for Java是功能强大的电子表格处理API,可让您在没有MS Office的情况下创建或修改Excel文件。该API支持添加图表,图形,公式,并以编程方式执行其他电子表格操作操作。首先,您需要在Java环境中为Java
API配置Aspose.Cells。您可以通过下载JAR文件或添加以下Maven存储库规范来进行设置:
MS Excel XLSX / XLS文件称为工作簿,每个工作簿均由一个或多个工作表组成。工作表还包含行和列,以将数据保持为单元格形式。因此,让我们从创建一个简单的工作簿开始。以下是从头开始创建Excel XLSX文件的步骤。
下面的代码示例演示如何使用Java创建Excel XLSX文件。
// Create a new workbook
Workbook workbook = new Workbook();
// Add value in the cell
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Hello World!");
// Save as Excel XLSX file
workbook.save("Excel.xlsx");
让我们看一下此代码的屏幕截图:
现在让我们看一下如何修改数据或将数据插入到现有的MS Excel文件中。为此,您只需加载文件,访问所需的工作表并保存更新的文件即可。以下是修改现有Excel文件的步骤。
下面的代码示例演示如何使用Java编辑现有的MS Excel文件。
// Create a new workbook
Workbook workbook = new Workbook("workbook.xls");
// Get the reference of "A1" cell from the cells of a worksheet
Cell cell = workbook.getWorksheets().get(0).getCells().get("A1");
// Set the "Hello World!" value into the "A1" cell
cell.setValue("updated cell value.");
// Write the Excel file
workbook.save("Excel.xls", FileFormatType.EXCEL_97_TO_2003);
电子表格中的图表用于直观地表示工作表中存储的数据。它们使轻松轻松地分析大量数据变得容易。Aspose.Cells for Java提供了多种图表,您可以通过编程在Excel文件中创建这些图表。以下是在Excel XLSX文件中创建图表的步骤。
下面的代码示例演示如何使用Java在Excel XLSX中创建图表。
// Create a new workbook
Workbook workbook = new Workbook("workbook.xlsx");
// Obtaining the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
// Adding some sample value to cells
Cells cells = sheet.getCells();
Cell 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);
// get charts in worksheet
ChartCollection charts = sheet.getCharts();
// Adding a chart to the worksheet
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5);
Chart chart = charts.get(chartIndex);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
SeriesCollection serieses = chart.getNSeries();
serieses.add("A1:B3", true);
// Write the Excel file
workbook.save("Excel_with_Chart.xlsx");
Excel工作表中的数据透视表具有多种用途,例如向数据添加过滤器,计算总计,汇总数据等。可以使用工作表中单元格的范围来创建数据透视表。以下是在Excel工作表中创建数据透视表的步骤。
下面的代码示例演示如何使用Java在Excel中创建数据透视表。
// Create a new workbook
Workbook workbook = new Workbook("workbook.xlsx");
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Obtaining Worksheet's cells collection
Cells cells = sheet.getCells();
// Setting the value to the cells
Cell 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);
PivotTableCollection pivotTables = sheet.getPivotTables();
// Adding a PivotTable to the worksheet
int index = pivotTables.add("=A1:C8", "E3", "PivotTable2");
// Accessing the instance of the newly added PivotTable
PivotTable pivotTable = pivotTables.get(index);
// Unshowing grand totals for rows.
pivotTable.setRowGrand(false);
// Dragging the first field to the row area.
pivotTable.addFieldToArea(PivotFieldType.ROW, 0);
// Dragging the second field to the column area.
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1);
// Dragging the third field to the data area.
pivotTable.addFieldToArea(PivotFieldType.DATA, 2);
// Write the Excel file
workbook.save("Excel_with_Chart.xlsx");
Aspose.Cells for Java还允许您使用Excel工作表中的公式。您可以将内置功能和附加功能应用于单元格。
在Excel中应用内置函数
对于使用内置函数,您可以简单地访问工作表中的所需单元格并使用Cell.setFormula(String)方法添加公式。下面的代码示例演示如何使用Java设置内置公式。
// Create a new workbook
Workbook workbook = new Workbook();
// Add value in the cell
workbook.getWorksheets().get(0).getCells().get(0).setFormula("=H7*(1+IF(P7 =$L$3,$M$3, (IF(P7=$L$4,$M$4,0))))");
// Save as Excel XLSX file
workbook.save("Excel.xlsx");
在Excel中添加加载项功能
在某些情况下,您必须使用用户定义的函数。为此,您将必须使用.xlam(启用Excel宏的加载项)文件注册加载项功能,然后将其用于所需的单元格。为了注册附加功能,Aspose.Cells for Java提供了registerAddInFunction(int,String)和registerAddInFunction(String,String,boolean)方法。下面的代码示例演示如何使用Java注册和使用附加功能。
// create a new workbook
Workbook workbook = new Workbook();
// Register macro enabled add-in along with the function name
int id = workbook.getWorksheets().registerAddInFunction("TESTUDF.xlam", "TEST_UDF", false);
// Register more functions in the file (if any)
workbook.getWorksheets().registerAddInFunction(id, "TEST_UDF1"); //in this way you can add more functions that are in the same file
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access first cell
Cell cell = worksheet.getCells().get("A1");
// Set formula name present in the add-in
cell.setFormula("=TEST_UDF()");
// Save as Excel XLSX file
workbook.save("Excel.xlsx");
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn