Excel poi的使用
常用API
创建工作薄和创建新Sheet页,创建单元格
public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
wb.createSheet("第二个Sheet页"); // 创建第二个Sheet页
Row row=sheet.createRow(0); // 创建一个行
Cell cell=row.createCell(0); // 创建一个单元格 第1列
cell.setCellValue(1); // 给单元格设置值
row.createCell(1).setCellValue(1.2); // 创建一个单元格 第2列 值是1.2
row.createCell(2).setCellValue("这是一个字符串类型"); // 创建一个单元格 第3列 值为一个字符串
row.createCell(3).setCellValue(false); // 创建一个单元格 第4列 值为布尔类型
FileOutputStream fileOut=new FileOutputStream("c:\\用Poi搞出来的Cell.xls");
wb.write(fileOut);
fileOut.close();
}
设置数据类型
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row=sheet.createRow(0); // 创建一个行
Cell cell=row.createCell(0); // 创建一个单元格 第1列
cell.setCellValue(new Date()); // 给单元格设置值
row.createCell(1).setCellValue(1);
row.createCell(2).setCellValue("一个字符串");
row.createCell(3).setCellValue(true);
row.createCell(4).setCellValue(HSSFCell.CELL_TYPE_NUMERIC);
row.createCell(5).setCellValue(false);
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
try {
wb.write(fileOut);
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
设置时间
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row=sheet.createRow(0); // 创建一个行
Cell cell=row.createCell(0); // 创建一个单元格 第1列
cell.setCellValue(new Date()); // 给单元格设置值
CreationHelper createHelper=wb.getCreationHelper();
CellStyle cellStyle=wb.createCellStyle(); //单元格样式类
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss"));
cell=row.createCell(1); // 第二列
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
cell=row.createCell(2); // 第三列
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
try {
wb.write(fileOut);
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
遍历工作薄的行和列
public static void main(String[] args) throws Exception{
InputStream is=new FileInputStream("c:\\名单.xls");
POIFSFileSystem fs=new POIFSFileSystem(is);
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet hssfSheet=wb.getSheetAt(0); // 获取第一个Sheet页
if(hssfSheet==null){
return;
}
// 遍历行Row
for(int rowNum=0;rowNum
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?