Excel导入模板

public class ExcelImport {

    
    public static AjaxMsg importPerson(HttpServletRequest request, String fileId, File file,
            IFileService fileService, IPersonService psersonService) throws IOException {
        style = null;//每次导入都将style置为null
        InputStream is = new FileInputStream(file);
        Workbook workbook = new HSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        boolean error = false;// 标记excel格式是否有误
        AjaxMsg msg = new AjaxMsg(true, "");
        DecimalFormat df = new DecimalFormat("#"); // 防止号码变成数值类型
        Map<String, List<TPerson>> maps = new HashMap<String, List<TPerson>>();
        
        if (true) {
            List<TPerson> personList = new ArrayList<TPerson>();
            for (int i = 3; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                TPerson person = new TPerson();
                // name
                Cell cell1 = row.getCell(0);
                if (cell1 != null) {
                    if (cell1.getCellType() == Cell.CELL_TYPE_STRING) {
                        person.setName(cell1.getStringCellValue());
                    } else if (cell1.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        person.setName(cell1.getNumericCellValue() + "");
                    }
                } else {
                    person.setName("");
                }
                if (person.getName() == null || person.getName().trim() == "") {//为空,则将单元格标红
                    cell1.setCellStyle(createRedStyle(workbook,cell1.getCellStyle()));
                    error = true;
                }
                // oldname
                Cell cell2 = row.getCell(1);
                if (cell2 != null) {
                    if (cell2.getCellType() == Cell.CELL_TYPE_STRING) {
                        person.setOldName(cell2.getStringCellValue());
                    } else if (cell2.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        person.setOldName(cell2.getNumericCellValue() + "");
                    }
                } else {
                    person.setOldName("");
                }
                // shortname
                Cell cell3 = row.getCell(2);
                if (cell3 != null) {
                    if (cell3.getCellType() == Cell.CELL_TYPE_STRING) {
                        person.setShortName(cell3.getStringCellValue());
                    } else if (cell3.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        person.setShortName(cell3.getNumericCellValue()+ "");
                    }
                } else {
                    person.setShortName("");
                }
                personList.add(person);
            }
            maps.put(person.getName(), personList);
        }
        if (is != null) {
            is.close();
        }

        try {
            if (error) {
                maps.clear();
                msg.setSuccess(false);
                String fUUID = createErrorFile(fileId, file, fileService, workbook);
                msg.setResult(fUUID);//文件的唯一标识,用于下载
                msg.setMsg("valiError");
            } else {
                AjaxMsg result = personService.insertPersonData(request, maps);//将导入的数据写入数据库
                if (!result.isSuccess()) { 
                    msg.setMsg("synError"); 
                    msg.setSuccess(false); 
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMsg("导入数据出错");
            msg.setSuccess(false);
        }
        return msg;
    }

    private static CellStyle style;
    
    // 单元格标红
    private static CellStyle createRedStyle(Workbook workbook,
            CellStyle oldStyle) {
        if(style!=null){
            return style;
        }
        style = workbook.createCellStyle();
        style.cloneStyleFrom(oldStyle);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFillForegroundColor(IndexedColors.RED.index);
        return style;
    }
    
    private static boolean isPhoneNumber(String input) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(input);
        b = m.matches();
        return b;
    }

    private static boolean isEmail(String input) {
        boolean a = false;
        a = input
                .matches("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _][email protected]([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
        return a;
    }

    /**
     * Excel格式有错,则创建含错误信息的Excel供下载
     */
    private static String createErrorFile(String fileId, File file,
            IFileService fileService, Workbook workbook) throws IOException,
            FileNotFoundException {
        TFile oldFile = fileService.find(fileId);
        OutputStream os = new FileOutputStream(file);
        workbook.write(os);
        if (os != null) {
            os.flush();
            os.close();
        }
        return oldFile.getUuid();
    }
}
时间: 2024-08-30 18:20:52

Excel导入模板的相关文章

java poi excel导入模板设置下拉框

import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFFo

项目产品化——Excel数据库模板化导入(java)

Excel导入可能是代码开发中比较常见的功能,一个项目如果有多个地方需要excel导入数据库,那么开发的工作量也将比较大,项目产品化过程中,将这么一个类似的功能进行封装合并也是必要的,封装好的代码只需要 使用方法: 第一步进行数据库关于excel的配置 第二步在jsp页面引入excel导入界面即可 拿到excel模板,不需要自己写后台代码,直接在数据库配置excel列与数据库字段的关系,然后在需要excel导入的地方,引入一个连接,即可生成excel导入功能.数据库配置一定要正确 //客户信息批

Excel导入导出的业务进化场景及组件化的设计方案(转)

1:前言 看过我文章的网友们都知道,通常前言都是我用来打酱油扯点闲情的. 自从写了上面一篇文章之后,领导就找我谈话了,怕我有什么想不开. 所以上一篇的(下)篇,目前先不出来了,哪天我异地二次回忆的时候,再分享分享. 话说最近外面IT行情飞涨还咋的,人都飞哪去了呢,听说各地的军情都进入紧急状态了. 回归下正题,今天就抽点时间,写写技术文,和大伙分享一下近年在框架设计上的取的一些技术成果. 2:项目背景 在针对运营商(移动.联通.电信.铁塔)的信息类的系统中,由于相关的从业人员习惯于Excel的办公

excel导入导出优化

对于上一篇excel中出现的问题,在excel导入导出中都做了优化.还是eclipse+jdk1.8,但是这个项目是一个web项目,需要配合Tomcat服务器,并且使用了SSH框架, I/O操作过多 首先,对于I/O操作过多,那么就不像之前一样,一条一条的添加或者更新;而且凑齐一堆,也就是一个list集合,然后统一的批量保存. 使用SessionFactory获取当前的session,然后调用session的persist方法,保存实体.只是设置了一个批量的量值.每到30条数据,就将缓存同步到数

java通过POI技术操作Excel(2)----模板读取,录入数据

先来回顾下通常把java对Excel的操作分为以下功能:1.生成模板,导出模板:2.填充模板,录入数据:3:读取数据库数据,导出数据:在上一篇博文中,我简单记录了模板生成和导出,在这篇博文中,主要来记录--Excel文件导入,数据录入(仍然是以jsp+servlet为例) 既然要解决这个问题,那首先来分析下我们需要面对的有哪些需求需要实现: 1.Excel文件导入(这是最基础的,巧妇难为无米之炊,导入环节也是查了好久才完成的); 2.Excel文件中数据的格式判定,你要读取文件,如果文件中其实没

Silverlight将Excel导入到SQLserver数据库

最近纠结于读取Excel模板数据,将数据导入SQLServer的Silverlight实现,本文将实现代码贴出,作为一个简单的例子,方便各位: 1.先设计前台界面新建Silverlight5.0应用程序,出现MainPage.xaml,代码如下所示: <UserControl x:Class="Excel导入SQLServer数据库.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present

Dynamic CRM 2013学习笔记(三十一)自定义用excel导入实体数据

有一个实体的子表数据量太大,于是客户想用execel来导入实体数据.首先想到的是用系统自带的Import Data,客户嫌太麻烦,比如lookup字段要做map等. 下面是具体的实现步骤: 一.定义excel数据模板 1. 利用系统自带的Download Template For Import下载系统自带的模板 2. 去掉不需要的列,比如有些列是自动计算,自动赋值 3. 保存为excel文件,并copy到crm server里的isv目录下 4. 定义一个按钮,并指定调用下面的js: // ex

解析大型.NET ERP系统 设计通用Microsoft Excel导入功能

做企业管理软件很难避免与Microsoft Excel打交道,常常是软件做好了,客户要求说再做一个Excel导入功能.导入Excel数据的功能的难度不大,从Excel列数据栏位的取值,验证值,再导入到数据库表中.然而一直是在做重复工作,写过不计其数的Excel导入程序,每次只是满足于问题解决,后来终于找到一个方法,实现通用的Excel数据导入. 设计通用的Excel导入功能,第一个实现要求是不能依赖Excel,客户的电脑或服务器很有可能没有安装Excel,所以微软的Office Interop一

Java中Excel导入功能实现、excel导入公共方法_POI -

这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> materialDataImport2(byte[] fileBytes, String fileName) { //return DataImport(fileBytes, fileName, "inv_m"); File file = FileUtils.getFileFromByte