使用POI读写Excel文件

本文介绍了如何使用Apache POI库进行Excel文件的读写操作。起因是作者在研究游戏配置文件生成过程中,发现Excel转二进制的实现,从而深入学习POI。示例包括了创建和读取Excel文件的基础步骤。
摘要由CSDN通过智能技术生成

最近想学习怎样读写Excel,找到了POI,在学习文档的基础上一起开始我们的学习里程吧!

非常不好意思,光撩了一个标题在这儿,一直没写博客。

先来说说我为什么会想到这么一个奇怪的东西,因为公司做游戏的时候使用的配置文件都是由EXCEl表格生成的二进制文件,我就琢磨了一下是怎么实现的没在网上搜索了一下,确实有这方面的资料。看到资料稍微多点的是就是这个POI,官网的资料也不少,官网在这里,大家可以看看http://poi.apache.org/

1.先来看看官方是怎么写EXCEl文件的

	public void wirte() {
		try {
			//创建一个输出流,用于把excel写出的文件
			FileOutputStream out = new FileOutputStream("c:\\workbook.xls");
			//创建一个excel
			Workbook wb = new HSSFWorkbook();
			//创建一个页面
			Sheet s = wb.createSheet();
			Row r = null;
			Cell c = null;
			CellStyle cs = wb.createCellStyle();
			CellStyle cs2 = wb.createCellStyle();
			CellStyle cs3 = wb.createCellStyle();
			DataFormat df = wb.createDataFormat();
			Font f = wb.createFont();
			Font f2 = wb.createFont();
			//设置字体高度
			f.setFontHeightInPoints((short) 12);
			f.setColor((short) 0xc);
			//粗体
			f.setBoldweight(Font.BOLDWEIGHT_BOLD);
			f2.setFontHeightInPoints((short) 10);
			//设置字体颜色
			f2.setColor((short) Font.COLOR_RED);
			f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
			f2.setStrikeout(true);
			cs.setFont(f);
			cs.setDataFormat(df.getFormat("#,##0.0"));
			cs2.setBorderBottom(cs2.BORDER_THIN);
			cs2.setFillPattern((short) CellStyle.SOLID_FOREGROUND);
			cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
			cs2.setFont(f2);
			wb.setSheetName(
					0,
					"\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F "
							+ "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430");
			int rownum;
			//for循环输出30行数据
			for (rownum = (short) 0; rownum < 30; rownum++) {
				r = s.createRow(rownum);
				if ((rownum % 2) == 0) {//设置偶数行单元格的高度
					r.setHeight((short) 0x249);
				}
				//10列数据
				for (short cellnum = (short) 0; cellnum < 10; cellnum += 2) {
					c = r.createCell(cellnum);
					//设置单元格的数值
					c.setCellValue(rownum
							* 10000
							+ cellnum
							+ (((double) rownum / 1000) + ((double) cellnum / 10000)));

					String cellValue;
					c = r.createCell((short) (cellnum + 1));
					//偶数行设置值为Test
					if ((rownum % 2) == 0) {
						c.setCellStyle(cs);
						c.setCellValue("Test");
					} else {
						c.setCellStyle(cs2);
						c.setCellValue("\u0422\u0435\u0441\u0442");
					}
					s.setColumnWidth((short) (cellnum + 1),
							(short) ((50 * 8) / ((double) 1 / 20)));
				}
			}
			rownum++;
			rownum++;

			r = s.createRow(rownum);
			cs3.setBorderBottom(cs3.BORDER_THICK);
			for (short cellnum = (short) 0; cellnum < 50; cellnum++) 
			{
				c = r.createCell(cellnum);
				c.setCellStyle(cs3);
			}
			s = wb.createSheet();
			//设置第二页的页名
			wb.setSheetName(1, "DeletedSheet");
			wb.removeSheetAt(1);
			wb.write(out);
			out.close();
		} catch (IOException e) {

		}
	}

运行结果是这样的:



2.下面看一下我读取excel文件的简单例子

public void readExcel() {
		POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		HSSFSheet sheet = null;
		HSSFRow row = null;
		try {
			FileInputStream is = new FileInputStream("E:\\hero.xls");
			fs = new POIFSFileSystem(is);
			wb = new HSSFWorkbook(fs);
		} catch (IOException e) {
			e.printStackTrace();
		}
		sheet = wb.getSheetAt(0);

		for (int i = 2; i < 6; i++) {
			String resultString = "row = " + (i + 1);
			row = sheet.getRow(i);

			row.getCell(0).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.getCell(1).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.getCell(2).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.getCell(3).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.getCell(4).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.getCell(5).setCellType(HSSFCell.CELL_TYPE_STRING);
			
			resultString += ",id = " + row.getCell(0).getStringCellValue();
			resultString += ",name = " + row.getCell(1).getStringCellValue();
			resultString += ",type = " + row.getCell(2).getStringCellValue();
			resultString += ",attack = " + row.getCell(3).getStringCellValue();
			resultString += ",speed = " + row.getCell(4).getStringCellValue();
			resultString += ",guard = " + row.getCell(5).getStringCellValue();
			System.out.println(resultString);
		}
	}

我这里均已String类型读取数据,只显示数据内容,实际情况应该是根据数据判断读取的。

看一下数据文件hero.xls


看一下读取的结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值