翻譯|使用教程|編輯:李顯亮|2021-06-22 11:19:38.210|閱讀 312 次
概述:在本文中,將學(xué)習(xí)如何在您的 Android 應(yīng)用程序中實(shí)現(xiàn) Excel 自動(dòng)化功能。閱讀本文后,將能夠以編程方式在您的 Android 應(yīng)用程序中從頭開(kāi)始創(chuàng)建 Excel XLSX 或 XLS 文件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
在本文中,將學(xué)習(xí)如何在您的 Android 應(yīng)用程序中實(shí)現(xiàn) Excel 自動(dòng)化功能。閱讀本文后,將能夠以編程方式在您的 Android 應(yīng)用程序中從頭開(kāi)始創(chuàng)建 Excel XLSX 或 XLS 文件。此外,本文還將介紹如何更新現(xiàn)有 Excel 文件、生成圖表、應(yīng)用公式以及在 Excel 工作表中添加數(shù)據(jù)透視表。
要將Excel電子表格轉(zhuǎn)換為PDF,我們將使用Aspose.Cells for Android via Java,它是一個(gè)強(qiáng)大的電子表格操作 API,讓您無(wú)需 MS Office 即可創(chuàng)建或修改 Excel 文件。API 支持以編程方式添加圖表、圖形、公式和執(zhí)行其他電子表格操作操作。你可以點(diǎn)擊下方按鈕獲取使用。
點(diǎn)擊下載Aspose.Cells for Python
每個(gè) Excel 工作簿由一個(gè)或多個(gè)工作表組成,這些工作表進(jìn)一步包含行和列,以將數(shù)據(jù)保持在單元格的形式。以下是從頭開(kāi)始創(chuàng)建 Excel XLSX 文件的步驟。
以下代碼示例展示了如何在 Android 中創(chuàng)建 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");
現(xiàn)在讓我們看看如何修改或插入數(shù)據(jù)到現(xiàn)有的 MS Excel 文件中。為此,您只需加載文件,訪問(wèn)所需的工作表并保存更新的文件。以下是修改現(xiàn)有 Excel 文件的步驟。
以下代碼示例展示了如何在 Android 中編輯現(xiàn)有的 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);
電子表格中的圖表用于直觀地表示存儲(chǔ)在工作表中的數(shù)據(jù)。它們使分析大量數(shù)據(jù)變得更加容易。Aspose.Cells for Android via Java 提供了廣泛的圖表,可以在 Excel 文件中以編程方式創(chuàng)建這些圖表。以下是在 Excel XLSX 文件中創(chuàng)建圖表的步驟。
以下代碼示例展示了如何在 Android 中的 Excel XLSX 中創(chuàng)建圖表。
// 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 工作表中的數(shù)據(jù)透視表具有多種用途,例如向數(shù)據(jù)添加過(guò)濾器、計(jì)算總計(jì)、匯總數(shù)據(jù)等。可以使用工作表中的單元格范圍創(chuàng)建數(shù)據(jù)透視表。以下是在 Excel 工作表中創(chuàng)建數(shù)據(jù)透視表的步驟。
以下代碼示例展示了如何在 Excel 中創(chuàng)建數(shù)據(jù)透視表。
// 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的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn