Java使用poi操作excel執行個體解析_java

來源:互聯網
上載者:User

本文執行個體為大家分享了Java使用poi操作excel的具體代碼,供大家參考,具體內容如下

依賴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>

 相應的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對於“/”需要轉義處理,File.separator可以實現跨平台     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);       // 建立excel檔案,注意這裡的hssf是excel2007及以前版本可用,2007版以後的不可用,要用xssf       HSSFWorkbook workbook = new HSSFWorkbook();       // 建立excel工作表       HSSFSheet sheet = workbook.createSheet("user");       // 為工作表增加一行       HSSFRow row = sheet.createRow(0);       // 在指定的行上增加兩個儲存格       row.createCell(0).setCellValue("name");       row.createCell(1).setCellValue("password");       // 調用輸出資料流把excel檔案寫入到磁碟       workbook.write(outputStream);       // 關閉輸出資料流       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);       // 根據名稱擷取指定的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();     }   } } 

以上就是本文的全部內容,希望對大家學習java程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.