POI3.9操作Excel (一)

 从http://poi.apache.org/download.html网符合下载jar,环境配置省略

说明: 

鉴于Excel版本2003,2007,2010的后缀名不一样,POI提供的两种方式,首先理解几个概念:

1. WORKBOOK

2. SHEET

3. ROW

4. CELL

不解释了,直接开始


入门示例

import java.io.FileOutputStream;

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 ExcelTest1 {
	public static void main(String[] args) throws Exception{
		HSSFWorkbook wb = new HSSFWorkbook();
		FileOutputStream fos = new FileOutputStream("src/hssf/workbook.xls");
		HSSFSheet sheet = wb.createSheet("aaa");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("aaaa");
		wb.write(fos);
		fos.close();
	}
}

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest2 {
	public static void main(String[] args) throws Exception {
		XSSFWorkbook xb = new XSSFWorkbook();
		FileOutputStream fos = new FileOutputStream("src/xssf/workbook.xlsx");
		XSSFSheet sheet = xb.createSheet("bbb");
		XSSFRow row = sheet.createRow(0);
		XSSFCell cell = row.createCell(0);
		cell.setCellValue("bbbb");
		xb.write(fos);
		fos.close();
	}
}

待续....


你可能感兴趣的:(POI)