Java使用poi操作excel實例解析
更新時間:2016年05月20日 14:58:59 作者:Past_Future
這篇文章主要為大家詳細介紹了Java使用poi操作excel的簡單實例,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java使用poi操作excel的具體代碼,供大家參考,具體內(nèi)容如下
依賴poi的jar包,pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>excelDemo1</groupId> <artifactId>excelDemo1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>excelDemo1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency> </dependencies> <build> <finalName>excelDemo1</finalName> </build> </project>
相應(yīng)的java測試代碼分別如下:
package excelDemo1; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelDemo0 { /** * java生成excel文件并寫入磁盤 * * @author:tuzongxun * @Title: main * @param@param args * @return void * @date Apr 28,2016 7:32:52 PM * @throws */ public static void main(String[] args) { //C:\Users\tuzongxun123\Desktop桌面,windows和linux的斜杠不一樣,而且java對于“/”需要轉(zhuǎn)義處理,F(xiàn)ile.separator可以實現(xiàn)跨平臺 File file = new File("C:" + File.separator + "Users" + File.separator + "tuzongxun123" + File.separator + "Desktop" + File.separator + "ioFile" + File.separator + "user.xls"); try { OutputStream outputStream = new FileOutputStream(file); // 創(chuàng)建excel文件,注意這里的hssf是excel2007及以前版本可用,2007版以后的不可用,要用xssf HSSFWorkbook workbook = new HSSFWorkbook(); // 創(chuàng)建excel工作表 HSSFSheet sheet = workbook.createSheet("user"); // 為工作表增加一行 HSSFRow row = sheet.createRow(0); // 在指定的行上增加兩個單元格 row.createCell(0).setCellValue("name"); row.createCell(1).setCellValue("password"); // 調(diào)用輸出流把excel文件寫入到磁盤 workbook.write(outputStream); // 關(guān)閉輸出流 outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
package excelDemo1; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * 讀取excel文件 * * @author tuzongxun123 * */ public class ExcelDemo2 { public static void main(String[] agrs) { try { // 獲取excel文件輸入流 FileInputStream fileInputStream = new FileInputStream("C:" + File.separator + "Users" + File.separator + "tuzongxun123" + File.separator + "Desktop" + File.separator + "ioFile" + File.separator + "user.xls"); BufferedInputStream bufferedInputStream = newBufferedInputStream( fileInputStream); POIFSFileSystem fileSystem = new POIFSFileSystem( bufferedInputStream); // 獲取excel文件 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); // 根據(jù)名稱獲取指定的excel工作薄 HSSFSheet sheet = hssfWorkbook.getSheet("user"); // 這里實際上可以用sheet.rowIterator()來遍歷 for (int i = 1;; i++) { HSSFRow row = sheet.getRow(i); if (row != null) { String nameString1 = row.getCell(0).getStringCellValue(); String password = row.getCell(i).getStringCellValue(); System.out.println("name:" + nameString1); System.out.println("password:" + password); bufferedInputStream.close(); } else { bufferedInputStream.close(); return; } } } catch (Exception e) { e.printStackTrace(); } } }
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)java程序設(shè)計有所幫助。
相關(guān)文章
詳解Java枚舉類在生產(chǎn)環(huán)境中的使用方式
本文主要介紹了Java枚舉類在生產(chǎn)環(huán)境中的使用方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02java中VO和DTO之間的轉(zhuǎn)換實現(xiàn)
本文主要介紹了java中VO和DTO之間的轉(zhuǎn)換實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05SpringBoot @PostConstruct原理用法解析
這篇文章主要介紹了SpringBoot @PostConstruct原理用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型
面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型...2006-12-12