博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 操作Excel表格 使用POI3.7
阅读量:5998 次
发布时间:2019-06-20

本文共 3770 字,大约阅读时间需要 12 分钟。

/**	 * 创建Excel表格	 * 	 * @param filename	 */	public static void createExecl(String filename) {		Workbook workbook = null;		// 产生工作簿对象		if (filename.endsWith(".xls")) {			workbook = new HSSFWorkbook();		} else {			workbook = new XSSFWorkbook();		}		// 产生工作表对象		Sheet sheet = workbook.createSheet("sheet one");		// 产生一行		Row row = sheet.createRow(0);		// 产生一个单元格		Cell cell = row.createCell(0);		// 设置单元格内容为字符串类型		// CELL_TYPE_BLANK		// CELL_TYPE_BOOLEAN		// CELL_TYPE_ERROR		// CELL_TYPE_FORMULA		// CELL_TYPE_NUMERIC		// CELL_TYPE_STRING		cell.setCellType(Cell.CELL_TYPE_STRING);		// 设置单元格内容		cell.setCellValue("序号");		cell = row.createCell(1);		cell.setCellType(Cell.CELL_TYPE_STRING);		cell.setCellValue("姓名");		cell = row.createCell(2);		cell.setCellType(Cell.CELL_TYPE_STRING);		cell.setCellValue("年龄");		cell = row.createCell(3);		cell.setCellType(Cell.CELL_TYPE_STRING);		cell.setCellValue("日期");		cell = row.createCell(4);		cell.setCellType(Cell.CELL_TYPE_STRING);		cell.setCellValue("简介");		for (int i = 0; i < 5; i++) {			row = sheet.createRow(i + 1);			cell = row.createCell(0);			cell.setCellType(Cell.CELL_TYPE_STRING);			cell.setCellValue("2012080300" + i);			cell = row.createCell(1);			cell.setCellType(Cell.CELL_TYPE_STRING);			cell.setCellValue("080300" + i);			cell = row.createCell(2);			cell.setCellType(Cell.CELL_TYPE_STRING);			cell.setCellValue("080300" + i);			cell = row.createCell(3);			cell.setCellType(Cell.CELL_TYPE_STRING);			cell.setCellValue(new Date());			cell = row.createCell(4);			cell.setCellType(Cell.CELL_TYPE_STRING);			cell.setCellValue("哈哈enemy哦凹阿拉" + i);		}		OutputStream outputStream = null;		try {			outputStream = new FileOutputStream(filename);			workbook.write(outputStream);			outputStream.flush();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		} finally {			if (outputStream != null) {				try {					outputStream.close();				} catch (IOException e) {					e.printStackTrace();				}			}		}	}	/**	 * 读取Excel表格	 * 	 * @param filename	 */	public static void readExcel(String filename) {		InputStream inputStream = null;		Workbook workbook = null;		Sheet sheet = null;		Row row = null;		Cell cell = null;		StringBuffer stringBuffer = new StringBuffer();		try {			inputStream = new FileInputStream(filename);			if (filename.endsWith(".xls")) {				workbook = new HSSFWorkbook(inputStream);			} else {				workbook = new XSSFWorkbook(inputStream);			}			for (int sheetIndex = 0, sheets = workbook.getNumberOfSheets(); sheetIndex < sheets; sheetIndex++) {				sheet = workbook.getSheetAt(sheetIndex);				if (null != sheet) {					stringBuffer.append(sheet.getSheetName() + "\n");					for (int rowIndex = 0, rows = sheet.getLastRowNum(); rowIndex <= rows; rowIndex++) {						row = sheet.getRow(rowIndex);						if (null != row) {							for (int cellnum = 0, cells = row.getLastCellNum(); cellnum < cells; cellnum++) {								cell = row.getCell(cellnum);								if (!(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)										|| !(DateUtil.isCellDateFormatted(cell))) {									cell.setCellType(Cell.CELL_TYPE_STRING);									stringBuffer.append(cell											.getStringCellValue() + "\t");								} else {									Date date = cell.getDateCellValue();									stringBuffer.append(new SimpleDateFormat(											"HH:mm:ss").format(date) + "\t");								}							}							stringBuffer.append("\n");						}					}					stringBuffer.append("\n\n");				}			}		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		} finally {			try {				inputStream.close();			} catch (IOException e) {				e.printStackTrace();			}		}		System.out.println(stringBuffer);	}

 

转载于:https://www.cnblogs.com/xiaoxian1369/archive/2012/08/03/2621869.html

你可能感兴趣的文章
Nokia5233手机和我装的几个symbian V5手机软件
查看>>
[译]扩展JavaScript功能的正确方法
查看>>
将某字符串切割成阵列并排序列出
查看>>
Godaady域名+花生壳 实现动态解析
查看>>
前端代码标准最佳实践:javascript篇
查看>>
__bridge,__bridge_transfer和__bridge_retained的使用和区别【转载】
查看>>
Swift游戏实战-跑酷熊猫 10 视差滚动背景
查看>>
Java获取时间与系统时间相差8小时终极解决方案
查看>>
WeText项目:一个基于.NET实现的DDD、CQRS与微服务架构的演示案例
查看>>
mysql一个事务中有DDL语句的binlog情况
查看>>
spring remoting源码分析--Hessian分析
查看>>
视频理论基础
查看>>
WordPress插件--WP BaiDu Submit结构化数据插件又快又全的向百度提交网页
查看>>
【C语言入门教程】4.5 指针变量的定义与引用
查看>>
Java中集合Set的用法
查看>>
Oracle 树操作(select…start with…connect by…prior)
查看>>
malloc 函数工作机制(转)
查看>>
phpMyAdmim和Yii 连接Mysql报错。
查看>>
Ubuntu 16.04关闭Alt+鼠标左键移动窗口(转)
查看>>
【hihocoder 1627】Domains(字典树)
查看>>