您当前的位置: 首页 > 

wespten

暂无认证

  • 1浏览

    0关注

    899博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Excel poi的使用

wespten 发布时间:2018-12-09 10:53:58 ,浏览量:1

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            
关注
打赏
1665965058
查看更多评论
0.0819s