从http://poi.apache.org/download.html网符合下载jar,环境配置省略
说明:
鉴于Excel版本2003,2007,2010的后缀名不一样,POI提供的两种方式,首先理解几个概念:
1. WORKBOOK
2. SHEET
3. ROW
4. CELL
不解释了,直接开始
【POI3.9操作Excel (一)】入门示例
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 解析excel 03与07或者是07版本以上不兼容问题
- poi|java poi 写word word 合并行 word合并列
- 中间件|中间件 — Apache POI & EasyExcel(POI读写03和07版本excel、EasyExcel读写07版本excel)