导出
public void export(OutputStream os, User user) {
// 获取所有供应商信息
List<User> UserList = userMapper.selectUserlistByPage(user);
// 1.创建excel工作薄
HSSFWorkbook wk = new HSSFWorkbook();
// 2.创建一个工作表
HSSFSheet sheet = wk.createSheet("系统用户");
// 3.写入表头
HSSFRow row = sheet.createRow(0);
// 表头
String[] headerName = { "账号", "密码", "真实姓名 ", "出生日期 " };
// 列宽
int[] columnWidths = { 6000, 6000, 6000, 6000 };
HSSFCell cell = null;
for (int i = 0; i < headerName.length; i++) {
// 创建表头单元格
cell = row.createCell(i);
// 向表头单元格写值
cell.setCellValue(headerName[i]);
sheet.setColumnWidth(i, columnWidths[i]);
}
// 4.向内容单元格写值
int i = 1;
for (User u : UserList) {
row = sheet.createRow(i);
row.createCell(0).setCellValue(u.getUser_code());// 账号
row.createCell(1).setCellValue("********");// 密码
if (u.getUser_name() != null) {
row.createCell(2).setCellValue(u.getUser_name());// "真实姓名
}
if (u.getUser_birthday() != null) {
HSSFCellStyle style_date = wk.createCellStyle();
DataFormat df = wk.createDataFormat();
style_date.setDataFormat(df.getFormat("yyyy-MM-dd"));
row.createCell(3).setCellValue(u.getUser_birthday());// 出生日期
sheet.getRow(i).getCell(3).setCellStyle(style_date);
}
i++;
}
try {
// 写入到输出流中
wk.write(os);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭工作簿
wk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
导入
public void doImport(InputStream is) throws IOException {
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
// 读取数据
// 最后一行的行号
int lastRow = sheet.getLastRowNum();
User user = null;
for (int i = 1; i <= lastRow; i++) {
// 账号
user = new User();
user.setUser_code(sheet.getRow(i).getCell(0).getStringCellValue());
// 判断是否已经存在,通过账号来判断
List<User> list = userMapper.selectUserByUserCode(user.getUser_code());
if (list.size() > 0) {
// 说明存在用户,需要更新
user = list.get(0);
}
HSSFCell cell = null;
// 密码
cell = sheet.getRow(i).getCell(1);
cell.setCellType(CellType.STRING);
if(!cell.getStringCellValue().equals("********")) {
user.setUser_pwd(encrypt(cell.getStringCellValue(), user.getUser_code()));
}
// 真实姓名
cell = sheet.getRow(i).getCell(2);
cell.setCellType(CellType.STRING);
user.setUser_name(sheet.getRow(i).getCell(2).getStringCellValue());
// 出生日期
cell = sheet.getRow(i).getCell(3);
cell.setCellType(CellType.NUMERIC);
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// Date birthday = df.parse(sheet.getRow(i).getCell(3).getDateCellValue());
user.setUser_birthday(sheet.getRow(i).getCell(3).getDateCellValue());
if (list.size() == 0) {
// 说明不存在用户信息,需要新增
userMapper.insertUser(user);
} else {
// 更新用户信息
userMapper.updateUserByUserCode(user);
}
}
} finally {
if (null != wb) {
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
用JavaPOI导出Excel时,我们会考虑到Excel版本及数据量的问题。针对不同的Excel版本,要采用不同的工具类。
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls;
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx;
对于不同版本的EXCEL文档要使用不同的工具类,如果使用错了,会提示如下错误信息。
org.apache.poi.openxml4j.exceptions.InvalidOperationException
org.apache.poi.poifs.filesystem.OfficeXmlFileException
从POI 3.8版本开始,提供了一种基于XSSF的低内存占用的API----SXSSF
当数据量超出65536条后,在使用HSSFWorkbook或XSSFWorkbook,程序会报OutOfMemoryError:Javaheap space;内存溢出错误。这时应该用SXSSFworkbook。