要從 Excel 檔案讀取資料並轉換為數組,可以使用 Apache POI 庫,遵循以下步驟:新增 Apache POI 庫相依性。建立 File 和 FileInputStream 物件。建立 Workbook 物件。取得指定索引的工作表。遍歷行和列。取得單元格值並轉換為 String。將資料儲存在二維數組中。
Java 中讀取Excel 檔案資料並轉換為陣列
簡潔概括:
要從Excel 檔案中讀取資料並將其轉換為數組,可以使用Apache POI 庫。此程式庫提供了一個 API,可輕鬆操縱和解析 Excel 檔案。
#詳細說明:
1. 新增Apache POI 函式庫:
在專案中新增Apache POI 函式庫的依賴項:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> </dependency>
2. 建立File 和FileInputStream:
要讀取Excel 文件,先建立一個File
物件和一個FileInputStream
物件:
File excelFile = new File("path/to/your.xlsx"); FileInputStream inputStream = new FileInputStream(excelFile);
3. 建立Workbook:
使用POI WorkbookFactory
類別建立一個Workbook
對象,該物件表示Excel 檔案:
Workbook workbook = WorkbookFactory.create(inputStream);
4. 處理Sheet:
每個Excel 檔案可能包含多個工作表,可以使用workbook.getSheetAt( index)
取得特定索引的Sheet:
Sheet sheet = workbook.getSheetAt(0);
5. 遍歷行與列:
使用sheet.getPhysicalNumberOfRows()
和sheet.getRow(rowIndex).getPhysicalNumberOfCells()
遍歷行與列:
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { Cell cell = row.getCell(j); } }
6. 取得儲存格值:
使用cell.toString()
取得單元格的值,將其轉換為String:
String value = cell.toString();
7. 轉換為陣列:
將讀取的資料儲存在數組中,可以建立一個二維數組String[][]
來保存資料:
String[][] data = new String[sheet.getPhysicalNumberOfRows()][sheet.getRow(0).getPhysicalNumberOfCells()]; for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { data[i][j] = cell.toString(); } }
以上是java怎麼讀取excel檔案資料並轉換成數組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具