java导入/导出excel表格数据

开发中经常会涉及到数据导入、导出到excel表格,所以下面介绍下操作excel的方式:

目前操作excel表格有两个框架:apache poi 和jexcelapi jxl,项目中poi用的更多所以这里介绍下poi。

首先需要在pom文件中引入poi.jar包

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.8</version>
</dependency>

常用类和API:
HSSFWorkbook :代表一个excel的整个文档 
HSSFWorkbook(InputStream inputStream); // 创建一个关联输入流的工作簿,可以将一个excel文件封装成工作簿
HSSFSheet createSheet(String sheetname); 创建一个新的Sheet
HSSFSheet getSheet(String sheetName); 通过名称获取Sheet
HSSFSheet getSheetAt(int index); // 通过索引获取Sheet,索引从0开始
HSSFCellStyle createCellStyle(); 创建单元格样式
int getNumberOfSheets(); 获取sheet的个数
setActiveSheet(int index); 设置默认选中的工作表
write();
write(File newFile);
write(OutputStream stream);
HSSFSheet:工作表 
HSSFRow createRow(int rownum); 创建新行,需要指定行号,行号从0开始
HSSFRow getRow(int index); 根据索引获取指定的行
int addMergedRegion(CellRangeAddress region); 合并单元格 
CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol); 单元格范围, 用于合并单元格,需要指定要合并的首行、最后一行、首列、最后一列。
autoSizeColumn(int column); 自动调整列的宽度来适应内容
getLastRowNum(); 获取最后的行的索引,没有行或者只有一行的时候返回0
setColumnWidth(int columnIndex, int width); 设置某一列的宽度
HSSFRow :行 
HSSFCell createCell(int column); 创建新的单元格
HSSFCell setCell(shot index);
HSSFCell getCell(shot index);
setRowStyle(HSSFCellStyle style); 设置行样式
short getLastCellNum(); 获取最后的单元格号,如果单元格有第一个开始算,lastCellNum就是列的个数
setHeightInPoints(float height); 设置行的高度
HSSFCell:单元格 
setCellValue(String value); 设置单元格的值
setCellType(); 设置单元格类型,如 字符串、数字、布尔等
setCellStyle(); 设置单元格样式
String getStringCellValue(); 获取单元格中的字符串值
setCellStyle(HSSFCellStyle style); 设置单元格样式,例如字体、加粗、格式化
setCellFormula(String formula); 设置计算公式,计算的结果作为单元格的值,也提供了异常常用的函数,如求和”sum(A1,C1)”、日期函数、字符串相关函数、CountIf和SumIf函数、随机数函数等
HSSFCellStyle :单元格样式 
setFont(Font font); 为单元格设置字体样式
setAlignment(HorizontalAlignment align); // 设置水平对齐方式
setVerticalAlignment(VerticalAlignment align); // 设置垂直对齐方式
setFillPattern(FillPatternType fp);
setFillForegroundColor(short bg); 设置前景色
setFillBackgroundColor(short bg); 设置背景颜色
HSSFFont:字体, 
setColor(short color); // 设置字体颜色
setBold(boolean bold); // 设置是否粗体
setItalic(boolean italic); 设置倾斜
setUnderline(byte underline); 设置下划线

项目中导出exce代码l示例:

public int doExportLogs() {
	// TODO Auto-generated method stub
	List<SysLog> list = sysLogDao.findAllLogs();
	File file = new File("D:\\java\\upload\\sysLog.xls");
	try {
		OutputStream outputStream = new FileOutputStream(file);
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("sheet1");
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row = sheet.createRow(i);
			if(i==0) {
				row.createCell(0).setCellValue("id");
				row.createCell(1).setCellValue("username");
				row.createCell(2).setCellValue("operation");
				row.createCell(3).setCellValue("method");
				row.createCell(4).setCellValue("params");
				row.createCell(5).setCellValue("time");
				row.createCell(6).setCellValue("ip");
				row.createCell(7).setCellValue("createdTime");
			} else {
				SysLog sysLog = list.get(i);
				row.createCell(0).setCellValue(sysLog.getId());
				row.createCell(1).setCellValue(sysLog.getUsername());
				row.createCell(2).setCellValue(sysLog.getOperation());
				row.createCell(3).setCellValue(sysLog.getMethod());
				row.createCell(4).setCellValue(sysLog.getParams());
				row.createCell(5).setCellValue(sysLog.getTime());
				row.createCell(6).setCellValue(sysLog.getIp());
				Date createdTime = sysLog.getCreatedTime();
				row.createCell(7).setCellValue(StringUtils.isEmpty(createdTime)?new Date():createdTime);
			}
			row.setHeightInPoints(30);
		}
		workbook.setActiveSheet(0);
		workbook.write(outputStream);
		outputStream.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	return list.size();
}

 

Guess you like

Origin blog.csdn.net/u011821510/article/details/93082819