依赖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;
}
}
}