依賴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>
<groupid>org.apache.poi</groupid>
<artifactid>poi</artifactid>
<version>3.8</version>
</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();
}
}
}
import java.io.bufferedinputstream;
import java.io.fileinputstream;
import org.apache.poi.poifs.filesystem.poifsfilesystem;
/**
* 讀取excel檔案
*
* @author tuzongxun123
*/
public class exceldemo2 {
public static void main(string[] agrs) {
// 擷取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 {
return;
}
}
}