码迷,mamicode.com
首页 > 数据库 > 详细

POI 读取Excel文件,将Excel数据导入数据库

时间:2015-08-05 10:05:23      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

/**
* 读取Excel单元格所显示的字符串(显示什么就返回什么)
* 创建人:minlorry
* 创建日期:2015-07-31
* 更新日期:2015-07-31
* 更新说明:生硬地处理了数值单元格的处理,日期单元格未作处理。
* @param cell Excel单元格
* @return String 单元格显示的字符串
*/
public static String getStringValue(Cell cell) {
String stringValue = "";//返回值
if (cell == null) {
return "";
}

//读取Excel单元格式,转为DecimalFormat,使用前还会调整
String fmt="0";//DecimalFormat格式
CellStyle cellStyle = cell.getCellStyle();
if(cellStyle!=null){
//这字符串就是自定义单元格式[常规、数值、货币、自定义)的那个格式串
//详见:org.apache.poi.ss.usermodel.BuiltinFormats|org.apache.poi.ss.usermodel.DataFormatter
//有关于有那些基本串的说明。
String dataFormatString = cellStyle.getDataFormatString();
if(dataFormatString!=null && !"".equals(dataFormatString)){
fmt= dataFormatString.replaceAll("[_()]", "").trim();
}else{
fmt="0";
}
}
DecimalFormat df;

switch (cell.getCellType()){
case Cell.CELL_TYPE_STRING:
stringValue = cell.getStringCellValue();
//stringValue = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
// if (DateUtil.isCellDateFormatted(cell)) {
// //日期
//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
//stringValue = cell.getDateCellValue().toLocaleString();

//方法2:这样子的data格式是不带带时分秒的:2011-10-12
//Date date = cell.getDateCellValue();
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//stringValue = sdf.format(date);
// }else{
// //数值
// }
if(fmt.equals("General")){
fmt="#,##0.00";//数值单元格,又是常规格式,好像就是公式
}else if(fmt.equals("0")){
fmt="#,##0";//整数单元格
}

df = new DecimalFormat(fmt);
stringValue = df.format(cell.getNumericCellValue()).trim();
if(stringValue.endsWith(".00")){
stringValue=stringValue.substring(0, stringValue.lastIndexOf("."));
}
break;
case Cell.CELL_TYPE_FORMULA:
try {
if(fmt.endsWith("0.00%")){
fmt="#,##0.00%";
}else if(fmt.endsWith("0.00")){
fmt="#,##0.00";
}else{
fmt="#,##0";
}
df = new DecimalFormat(fmt);
stringValue = df.format(cell.getNumericCellValue());
}catch (Exception e) {
try{
stringValue = String.valueOf(cell.getRichStringCellValue());
}catch (Exception e1) {
stringValue="公式计算出错";
}
}
break;
case Cell.CELL_TYPE_BOOLEAN:
stringValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK:
stringValue = "";
break;
case Cell.CELL_TYPE_ERROR:
stringValue = "";
default:
return "";
}
if ("".equals(stringValue) || stringValue == null) {
return "";
}
return stringValue;
}

 

 

/**
* 将上传的Excel文件存入数据库
* @author minlorry
* @param file 网页上传的文件
* @param user 当前登录的用户
*/
@Transactional(readOnly = false)
public String saveFromExcel(User user,MultipartFile file){
Workbook workBook = null;//工作簿
Sheet sheet = null;//工作表
Row row = null;//工作表中的行
/**读入工作簿start*/
try{
switch(ExcelUtil.getPostfix(file.getOriginalFilename()).toLowerCase()){
case "xlsx":workBook = new XSSFWorkbook(file.getInputStream());break;//office2007及以后
case "xls":workBook = new HSSFWorkbook(file.getInputStream());break;//office2003及以前
default:
return "从Excel导入企业信息时,打开了不支持的文件格式!";
}
}catch(Exception e){
//可能是加密的文件或锁定等因素会出现异常
return "从Excel导入企业信息时,开始解析文件时出错!";
}
/**读入工作簿end*/
List<Company> companyList =new ArrayList();
try{
Company company=null;
//遍历工作簿中的工作表
for (int sheetNum= 0; sheetNum < workBook.getNumberOfSheets(); sheetNum++) {
sheet = workBook.getSheetAt(sheetNum);
if(sheet == null){continue;}
if(sheet.getPhysicalNumberOfRows()<2){continue;}
//遍历表行(第二行起)
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
row = sheet.getRow(rowNum);
if(row==null){continue;}

company = new Company();
company.setCode(ExcelUtil.getStringValue(row.getCell(1)));
company.setName(ExcelUtil.getStringValue(row.getCell(2)));
company.setAttention(ExcelUtil.getStringValue(row.getCell(3)));
company.setPhone(ExcelUtil.getStringValue(row.getCell(4)));

Date date = new Date();
company.setCreatetime(date);
company.setUpdatetime(date);
company.setCreator(user);
company.setUpdator(user);

company.setCompanyClass(null);//未分类
companyList.add(company);
}
}
}catch (Exception e) {
return "从Excel导入企业信息时,读取数据记录时出错!";
}
try{
add(companyList);
}catch (Exception e) {
return "从Excel导入企业信息时,最后保存数据时出错!";
}
return "从Excel导入企业信息成功!";
}

 

POI 读取Excel文件,将Excel数据导入数据库

标签:

原文地址:http://www.cnblogs.com/minlorry/p/4703740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!