poi读写Excel的常用用法

首先讲下 HSSFWorkbook和XSSFWorkbook的区别

HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls

XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx

不同版本Excel要使用不同的工具类,使用错了会报错的,其中XSSF的试用性和兼容性更广,这里就都使用XSSF的。

一、读取Excel文件


/**
     * @Title: readExcel 
     * @Description:读取Excel里的数据
     * @param string void
     * @throws IOException 
     */
	private static void readExcel(String path) throws IOException {
		FileInputStream fis=new FileInputStream(new File(d:/test/xx.xlsx));	
		XSSFWorkbook wb=new XSSFWorkbook(fis);//构建XSSFWorkbook对象
		XSSFSheet sheet=wb.getSheetAt(0);//读取sheet第一个表格
		XSSFRow row;
		String cell;//定义行、列
		XSSFRow row0=sheet.getRow(0);//获取第一行
		if(row0!=null){
			int rowAll=sheet.getPhysicalNumberOfRows();//获取Excel总行数
			for(int i=sheet.getFirstRowNum();i<rowAll;i++){
				row=sheet.getRow(i);
				String name=row.getCell(0).toString();
				String course=row.getCell(1).toString();
				String score=row.getCell(2).toString();
				System.out.println(name+" "+course+" "+score);
			}
		}	
	}

二、生成Excel文件

/**
	 * @param list
	 * @throws IOException 
	 * @Title: createExcel
	 * @Description: 生成新的Excel
	 */
	private static void createExcel(List<Students> list) throws IOException {

		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("成绩表");
		XSSFRow row = sheet.createRow(0);
		XSSFCellStyle style = wb.createCellStyle();// 创建样式
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 设置居中

		XSSFCell cell = row.createCell(0);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);

		cell = row.createCell(1);
		cell.setCellValue("科目");
		cell.setCellStyle(style);

		cell = row.createCell(2);
		cell.setCellValue("分数");
		cell.setCellStyle(style);

		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow(i + 1);
			Students students = list.get(i);
			row.createCell(0).setCellValue(students.getName());
			row.createCell(1).setCellValue(students.getCourse());
			row.createCell(2).setCellValue(students.getScore());
		}
		try {
			FileOutputStream fos=new FileOutputStream("d:/test/分数表.xlsx");
			wb.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

其中students数据自己封装传进去就好,没啥好讲的。

三、生成合并单元格的Excel文件

/**
	 * @Title: createMergeExcel
	 * @Description:生成合并单元格的Excel
	 * @param list
	 *            void
	 * @throws IOException
	 */
	private static void createMergeExcel(List<Students> list)
			throws IOException {
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("成绩表");
		XSSFRow row0 = sheet.createRow(0);
		XSSFCellStyle style = wb.createCellStyle();// 创建样式
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 设置居中
		style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直


		String[] colName = new String[] { "姓名", "课程", "分数" };
		// 生成标题
		for (int i = 0; i < colName.length; i++) {
			createCell(row0, style, (short) (i), colName[i]);
		}
		// 生成内容
		for (int j = 0; j < list.size(); j++) {
			XSSFRow row = sheet.createRow(j + 1);
			Students students = list.get(j);
			row.createCell(0).setCellValue(students.getName());
			row.createCell(1).setCellValue(students.getCourse());
			row.createCell(2).setCellValue(students.getScore());


		}
		//静态合并  知道要合并的区域
		//sheet.addMergedRegion(new CellRangeAddress(1,2,0,0));
		//sheet.addMergedRegion(new CellRangeAddress(3,4,0,0));
		//sheet.addMergedRegion(new CellRangeAddress(5,6,0,0));	
		
		//动态合并 判断单元格内容是否相同,相同的进行合并
		int k=1;
		int index=1;//合并的起始行
		for(int j = 0; j < list.size(); j++){
			while(j<list.size()-1&&list.get(j).getName().equals(list.get(j+1).getName())){
				k++;
				j++;
			}
			sheet.addMergedRegion(new CellRangeAddress(index,k,0,0));
			index=k+1;
			k++;
		}
		
		try {
			FileOutputStream fos = new FileOutputStream(
					"d:/test/合并.xlsx");
			wb.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}
	// 创建行
	private static void createCell(XSSFRow row, XSSFCellStyle cellstyle,
			short col, String val) {
		XSSFCell cell = row.createCell(col);
		cell.setCellStyle(cellstyle);
		XSSFRichTextString cellString = new XSSFRichTextString(val);
		cell.setCellValue(cellString);


	}

其中sheet.addMergedRegion(new CellRangeAddress(1,2,0,0));四个参数的含义分别是起始行、终止行、其实列、终止列

一般我们用动态合并,根据单元格内的内容是否相同去合并,除非我们明确要合并的单元格区域。



慢慢会补充其他常用小例子。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值