前几天公司的网站有了新的需求,需要将数据一键导出到Excel文件,或直接将Excel中的数据直接插入到数据库,查阅可一天的api文档和百度.也算有了一点小经验,分享出来给大家看看.有什么错误可以评论指出.
本次实验了jxl和POI两种方式解析,感觉各有千秋.
试验中使用的jar包以及工具包都在我的百度云中,各位自行下载即可 点击打开链接
一、jxl
- Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写
- 支持Excel 95-2000的所有版本(网上说目前可以支持Excel2007了,还没有尝试过)
- 生成Excel 2000标准格式
- 支持字体、数字、日期操作
- 能够修饰单元格属性
- 支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。
缺点:效率低,图片支持不完善,对格式的支持不如POI强大
二、POI
- 效率高,但编写较复杂.
- 支持公式,宏
- 能够修饰单元格属性
- 支持字体、数字、日期操作并一键转换
jxl方式:
/**
* jxl创建Excel工作簿,并写入数据
* @param fullPath
*/
public static void writeExcel(String fullPath) {
//定义表头标题
String[] labelTitle = new String[] { "ID", "姓名", "性别", "年龄" };
File file = new File(fullPath);
//定义表格单元格格式
WritableCellFormat cellFormat = new WritableCellFormat();
try {
//横向居中
cellFormat.setAlignment(Alignment.CENTRE);
//竖向居中
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
//创建工作表格簿实例
WritableWorkbook workbook = Workbook.createWorkbook(file);
//创建sheet页 参数:"sheet1"--sheet页的名字,0--sheet的索引
WritableSheet sheet = workbook.createSheet("sheet1", 0);
//文本对象
Label label = null;
for (int j = 0; j < labelTitle.length; j++) {
//参数--列,行,内容
label = new Label(j, 0, labelTitle[j], cellFormat);
//添加单元格
sheet.addCell(label);
}
for (int i = 1; i <= 10; i++) {
label = new Label(0, i, "" + i, cellFormat);
sheet.addCell(label);
label = new Label(1, i, "李四", cellFormat);
sheet.addCell(label);
label = new Label(2, i, "男", cellFormat);
sheet.addCell(label);
label = new Label(3, i, "" + (i + 20), cellFormat);
sheet.addCell(label);
}
//写入工作簿
workbook.write();
//关闭写入流
workbook.close();
System.out.println("写入完毕!");
} catch (Exception e) {
e.printStackTrace();
}
}
实验结果:
解析Excel文件
public static void readExcel(String filePath) throws BiffException, IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("文件未找到");
}
//创建workbook工作簿
Workbook workbook = Workbook.getWorkbook(file);
//获取第一张sheet页
Sheet sheet = workbook.getSheet(0);
/*
* 循环遍历每一个单元格
* sheet.getRows()获取sheet页中的行数
* sheet.getColumns()获取sheet页中的列数
* sheet.getCell()获取单元格
* sheet.getContents()获取单元格内容
*/
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
//参数列表------------------列,行
Cell cell = sheet.getCell(j, i);
System.out.print(cell.getContents() + ",");
}
System.out.println();
}
workbook.close();
}
实验结果:
POI方式:
/**
* POI创建Excel工作簿
* @param fullPath
* @throws IOException
*/
public static void CreateExcel(String fullPath) throws IOException {
String[] title = new String[] { "id", "name", "age" };
//新建Excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet页
HSSFSheet sheet = workbook.createSheet();
//创建行,首行为0
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
for (int i = 0; i < title.length; i++) {
//创建单元格
cell = row.createCell(i);
//设置单元格的值
cell.setCellValue(title[i]);
}
HSSFCell nextCell = null;
for (int i = 1; i <= 10; i++) {
HSSFRow nextRow = sheet.createRow(i);
nextCell = nextRow.createCell(0);
nextCell.setCellValue(i);
nextCell = nextRow.createCell(1);
nextCell.setCellValue("张三" + i);
nextCell = nextRow.createCell(2);
nextCell.setCellValue(30 + i);
}
//创建文件
File file = new File(fullPath);
//创建输出流
FileOutputStream stream = FileUtils.openOutputStream(file);
//写入工作簿
workbook.write(stream);
//关流
stream.close();
workbook.close();
}
实验结果:
/**
* POI解析Excel工作簿
* @param fullPath
* @throws IOException
*/
public static void readExcel(String fullPath) throws IOException {
File file = new File(fullPath);
if (!file.exists()) {
throw new FileNotFoundException("该文件不存在");
}
//获取工作簿
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
//通过索引获取sheet页
HSSFSheet sheet = workbook.getSheetAt(0);
//获取最后的行数
int rowIndex = sheet.getLastRowNum();
HSSFRow row;
short lastCellIndex;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
DecimalFormat df = new DecimalFormat("0");
for (int i = 0; i < rowIndex; i++) {
row = sheet.getRow(i);
//获取最后的单元格列数
lastCellIndex = row.getLastCellNum();
for (int j = 0; j < lastCellIndex; j++) {
HSSFCell cell = row.getCell(j);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC://数字
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
System.out.print(sdf.format(date));
} else {
System.out.print(" ");
}
} else {
System.out.print(df.format(cell.getNumericCellValue()) + " ");
}
break;
case HSSFCell.CELL_TYPE_STRING://字符串
System.out.print(cell.getStringCellValue() + " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN://布尔
System.out.print(cell.getBooleanCellValue() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK://空值
System.out.print(" ");
break;
case HSSFCell.CELL_TYPE_FORMULA://公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_ERROR://错误
System.out.print("非法字符 ");
break;
default:
System.out.print("未知类型 ");
}
}
System.out.println();
}
}
实验结果: