翻譯|使用教程|編輯:李顯亮|2021-06-22 11:19:38.210|閱讀 312 次
概述:在本文中,將學習如何在您的 Android 應用程序中實現 Excel 自動化功能。閱讀本文后,將能夠以編程方式在您的 Android 應用程序中從頭開始創建 Excel XLSX 或 XLS 文件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本文中,將學習如何在您的 Android 應用程序中實現 Excel 自動化功能。閱讀本文后,將能夠以編程方式在您的 Android 應用程序中從頭開始創建 Excel XLSX 或 XLS 文件。此外,本文還將介紹如何更新現有 Excel 文件、生成圖表、應用公式以及在 Excel 工作表中添加數據透視表。
要將Excel電子表格轉換為PDF,我們將使用Aspose.Cells for Android via Java,它是一個強大的電子表格操作 API,讓您無需 MS Office 即可創建或修改 Excel 文件。API 支持以編程方式添加圖表、圖形、公式和執行其他電子表格操作操作。你可以點擊下方按鈕獲取使用。
每個 Excel 工作簿由一個或多個工作表組成,這些工作表進一步包含行和列,以將數據保持在單元格的形式。以下是從頭開始創建 Excel XLSX 文件的步驟。
以下代碼示例展示了如何在 Android 中創建 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 文件的步驟。
以下代碼示例展示了如何在 Android 中編輯現有的 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 Android via Java 提供了廣泛的圖表,可以在 Excel 文件中以編程方式創建這些圖表。以下是在 Excel XLSX 文件中創建圖表的步驟。
以下代碼示例展示了如何在 Android 中的 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 工作表中創建數據透視表的步驟。
以下代碼示例展示了如何在 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的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn