需要导入
poi-3.7.jar
commons-io-2.4.jar
Excel2003导出
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class poiexp {
public static void main(String[] args) {
String[] title={"id","name","sex"};
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell=null;
for (int i = 0; i < title.length; i++) {
cell=row.createCell(i);
cell.setCellValue(title[i]);
}
for (int i = 1; i <= 10; i++) {
HSSFRow nextrow =sheet.createRow(i);
HSSFCell Cell1 = nextrow.createCell(0);
Cell1.setCellValue("id"+i);
Cell1 = nextrow.createCell(1);
Cell1.setCellValue("name"+i);
Cell1 = nextrow.createCell(2);
Cell1.setCellValue("nv"+i);
}
File file =new File("d:/wook.xls");
try {
file.createNewFile();
FileOutputStream stream=FileUtils.openOutputStream(file);
workbook.write(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Excel2003导出入
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelRead {
public static void main(String[] args) {
File file =new File("d:/wook.xls");
try {
HSSFWorkbook Workbook =
new HSSFWorkbook(FileUtils.openInputStream(file));
/*HSSFSheet sheet = Workbook.getSheet("sheet0");*/
HSSFSheet sheet = Workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int j = 0; j < lastRowNum; j++) {
HSSFRow row = sheet.getRow(j);
short lastCellNum = row.getLastCellNum();
for (int k = 0; k < lastCellNum; k++) {
HSSFCell cell = row.getCell(k);
String stringCellValue = cell.getStringCellValue();
System.out.print(stringCellValue);
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Excel2007导出只要把HSSF批量替换为XSSF就行 重新导入包 (导入也可以但是得注意Excel的格式尤其是数字文本的区别)
导出的建议HSSF 考虑客户的兼容性 导入的时候需要判断2003还是2007
需要导入
poi-3.9-20121203.jar
commons-io-2.4.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
dom4j-1.6.1.jar (也需要dom4j)
如果未导入xmlbeans-2.3.0.jar
报一下错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObjectpublic class poiexp {
public static void main(String[] args) {
String[] title={"id","name","sex"};
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell=null;
for (int i = 0; i < title.length; i++) {
cell=row.createCell(i);
cell.setCellValue(title[i]);
}
for (int i = 1; i <= 10; i++) {
XSSFRow nextrow =sheet.createRow(i);
XSSFCell Cell1 = nextrow.createCell(0);
Cell1.setCellValue("id"+i);
Cell1 = nextrow.createCell(1);
Cell1.setCellValue("name"+i);
Cell1 = nextrow.createCell(2);
Cell1.setCellValue("nv"+i);
}
File file =new File("d:/22.xlsx");
try {
file.createNewFile();
FileOutputStream stream=FileUtils.openOutputStream(file);
workbook.write(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
}