您的位置 首页 java

java操作poi(excel)详解

java 开发中,经常需要操作excel,也就是poi,以下是小编的测试,总结得很详细,不信你看!

一、首先要弄清楚excel的层次结构,如下:

.xls 97-03版excel

.xlsx 07版excel

结构组成:

工作簿 workbook

表空间 sheet

行 row

列 column

单元格 cell

结构关系:

workbook->sheet->row->cell

workbook->sheet->column

/**创建excel测试总结:

* 1、excel分97版和07版,其中97版以xls结尾,07版以 xlsx 结尾

* 2、07版的可以创建97版的,97版的不可以创建07版的,也就是说07版的可以创建xls和xlsx两种excel,而97版的只能创建xls一种excel

* 3、测试证明了软件版本的升级后的向下兼容性,也就是说较新的版本可以兼容较旧的版本,较旧的版本却不能兼容较新的版本

* @author

*/

测试1:excel的基本创建

代码:

public class CreateExcel {

public static void main(String[] args) {

/**

* 开始创建excel

* **/

//创建工作簿 97版excel

Workbook wb = new HSSFWorkbook();

//创建07版excel

//Workbook wb = new XSSFWorkbook();

//创建表空间

Sheet sheet1 = wb.createSheet(“sheet1”);

//创建行元素

Row row1 = sheet1.createRow(3);

//创建单元格

Cell cell1 = row1.createCell(3);

//往单元格中输入值

cell1.setCellValue(“ABC”);

/**

* excel创建完毕

* */

//创建输出流,生成excel文件

FileOutputStream fs=null;

try {

fs = new FileOutputStream(new File(“d:/test.xlsx”));

wb.write(fs);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(fs!=null){

try {

fs. close ();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

结果:

测试2:向excel中批量写入数据

代码:

public class CreatExcel1 {

public static void main(String[] args) {

List<Person> perList=new ArrayList<Person>();

perList.add(new Person(1, “张三”, 18, “13558832140”));

perList.add(new Person(2, “李四”, 28, “13558832142”));

perList.add(new Person(3, “王五”, 19, “13558832144”));

perList.add(new Person(4, “赵六”, 16, “13558832146”));

try {

//创建97版excel

Workbook wb = new HSSFWorkbook();

//创建表空间

Sheet sheet=wb.createSheet();

//设置列宽 setColumnWidth(列索引,列宽度) 看到的列宽*267

sheet.setColumnWidth(3, 12*267);

//插入表头的行

Row headRow=sheet.createRow(0);

headRow.createCell(0).setCellValue(“编号”);

headRow.createCell(1).setCellValue(“姓名”);

headRow.createCell(2).setCellValue(“年龄”);

headRow.createCell(3).setCellValue(“手机号”);

//根据list的大小创建行元素

for(int i=0;i<perList.size();i++){

Row row=sheet.createRow(i+1);

//创建单元格

row.createCell(0).setCellValue(perList.get(i).getId());

row.createCell(1).setCellValue(perList.get(i).getName());

row.createCell(2).setCellValue(perList.get(i).getAge());

row.createCell(3).setCellValue(Long.parseLong(perList.get(i).getPhone()));

}

FileOutputStream fileOut = new FileOutputStream(new File(“d:/persons1.xls”));

wb.write(fileOut);

fileOut.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

结果:

测试3:读取excel文件的内容

代码:

public class ReadExcel {

public static void main(String[] args) {

try {

// 使用文件方式

Workbook wb = WorkbookFactory.create(new File(“d:/persons.xls”));

//读取表空间

//Sheet sheet=wb.getSheet(“sheet0”);

Sheet sheet=wb.getSheetAt(0);

//获取表头的行

Row headRow=sheet.getRow(0);

//获取该行下使用的单元格数

int cellNum=headRow.getLastCellNum();

for(int i=0;i<cellNum;i++){

//获取单元格

Cell headCell=headRow.getCell(i);

//获取单元格的值

String head=headCell.getStringCellValue();

System.out.println(head);

}

System.out.println(“——————————-“);

int rowNum=sheet.getLastRowNum();

for(int i=1;i<=rowNum;i++){

Row dataRow=sheet.getRow(i);

int id=(int) dataRow.getCell(0).getNumericCellValue();

String name=dataRow.getCell(1).getStringCellValue();

int age=(int) dataRow.getCell(2).getNumericCellValue();

long phone=(long) dataRow.getCell(3).getNumericCellValue();

System.out.println(id+”–“+name+”–“+age+”–“+phone);

}

// 使用输入流 需要更多的资源消耗

//Workbook wb = WorkbookFactory.create(new FileInputStream(new File(“d:/persons.xls”)));

} catch (Exception e) {

e.printStackTrace();

}

}

}

结果:

文章来源:智云一二三科技

文章标题:java操作poi(excel)详解

文章地址:https://www.zhihuclub.com/196240.shtml

关于作者: 智云科技

热门文章

网站地图