//上传位置(与操作excel无关,可不看)                                                                                                                              

 public String getUploadPath() {
         File theWebFolder = XMPPServer.getInstance().getAdminFolder();
         String path = theWebFolder.getAbsolutePath() + File.separator
                 + "ucstar_plugins/ucyingjiyanlian/upload/";

         return path;
     }

     public String getUploadMediaPath() {
         File theWebFolder = XMPPServer.getInstance().getAdminFolder();
         String path = theWebFolder.getAbsolutePath() + File.separator
                 + "ucstar_plugins/ucyingjiyanlian/upload/media/";

         return path;
     }

// excel2003跟2007操作差不多、但是也有区别可以通过判断后缀名区别然后分别调用方法处理

//2003处理

public boolean exportExcelData(String _fileName, String _incidentUri) {
        boolean flag = false;
        if (JiveGlobals.isEmpty(_fileName) || JiveGlobals.isEmpty(_incidentUri)) {
            return flag;
        }
        try {
            String savePath = YingjiyanlianBaseManager.getInstance()
                    .getUploadPath();
            File saveFile = new File(savePath, _fileName);
            if (saveFile.exists()) {

                InputStream in = new BufferedInputStream(new FileInputStream(
                        saveFile));
                List<UcstarYingjiSend> fileto = new ArrayList<UcstarYingjiSend>();
                POIFSFileSystem fs = new POIFSFileSystem(in);
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                HSSFCell cell = null;
                // sheet循环
                for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
                    // 第一张表的值
                    HSSFSheet st = wb.getSheetAt(sheetIndex);
                    // 获取第二行以下的信息
                    for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
                        HSSFRow row = st.getRow(rowIndex);
                        if (row == null) {
                            continue;
                        }
                        UcstarYingjiSend poom = new UcstarYingjiSend();
                        poom.setIncidentUri(_incidentUri);
                        for (short columnIndex = 0; columnIndex <= row
                                .getLastCellNum(); columnIndex++) {
                            String value = "";
                            cell = row.getCell(columnIndex);

                            if (cell != null) {
                                // 注意:一定要设成这个,否则可能会出现乱码
                                cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                                switch (cell.getCellType()) {
                                case HSSFCell.CELL_TYPE_STRING:
                                    value = cell.getStringCellValue();
                                    break;
                                // 公式类型转换
                                case HSSFCell.CELL_TYPE_FORMULA:
                                    value = new DecimalFormat("0").format(cell
                                            .getNumericCellValue());

                                    break;
                                case HSSFCell.CELL_TYPE_NUMERIC:

                                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                        value = new java.text.SimpleDateFormat(
                                                "HH:mm").format(cell
                                                .getDateCellValue());
                                    } else {
                                        value = new DecimalFormat("0")
                                                .format(cell
                                                        .getNumericCellValue());
                                    }
                                    break;

                                }
                            }

                            if (rowIndex != 0 && rowIndex != 1) {
                                if (columnIndex == 0) {
                                    if (JiveGlobals.isEmpty(value)
                                            || !value.matches("[0-9]+")) {
                                        continue;
                                    }
                                    poom.setTaskId(value);
                                }
                                if (columnIndex == 1) {
                                    if (JiveGlobals.isEmpty(value)) {
                                        continue;
                                    }
                                    // poom.setCreatetime(value);
                                    poom.setCreatetime("00:00");
                                }
                                if (columnIndex == 2) {
                                    if ("否".equals(value)) {
                                        poom.setSameTime(0);
                                    } else if ("是".equals(value)) {
                                        poom.setSameTime(1);
                                    }
                                }
                                if (columnIndex == 3) {
                                    poom.setScriptstr(value);
                                }
                                if (columnIndex == 4) {
                                    poom.setCreator(value);
                                }
                                if (columnIndex == 5) {
                                    poom.setReceivers(value);
                                }
                                if (columnIndex == 6) {
                                    poom.setDetailTime(value);
                                }
                                if (columnIndex == 7) {
                                    poom.setDetail(value);
                                }
                                if (columnIndex == 8) {
                                    poom.setTitle(value);
                                }
                                if (columnIndex == 9) {
                                    poom.setMemo(value);
                                }
                            }
                        }
                        // 需要判断字段的值是否都为空,等 其他验证条件
                        if (!JiveGlobals.isEmpty(poom.getTaskId())
                                && !JiveGlobals.isEmpty(poom.getReceivers())
                                && !JiveGlobals.isEmpty(poom.getCreatetime())) {
                            fileto.add(poom);
                        } else {
                            Log.console(">>演练脚本导入数据有误,行:" + rowIndex);
                        }
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
                }
                if (fileto.size() > 0
                        && UcstarYingjiSendServiceUtil.getFactory()
                                .getUcstarYingjiSendService().saveScriptDatas(
                                        fileto, _incidentUri)) {
                    flag = true;
                } else {
                    Log.error(">>202读取演练脚本数据为空");
                }
            } else { //文件不存在提示用户
                Log.error(">>203演练脚本不存在");
            }
        } catch (Exception e) {
            Log.error(">>应急演练Excel文件数据异常:" + e);
        }
        return flag;
    }
}

//2007处理

// 2007版本
    public boolean exportExcelDate2007(String _fileName, String _incidentUri) {
        boolean flag = false;
        if (JiveGlobals.isEmpty(_fileName) || JiveGlobals.isEmpty(_incidentUri)) {
            return flag;
        }
        try {
            String savePath = YingjiyanlianBaseManager.getInstance()
                    .getUploadPath();
            // String _fileNamm = _fileName +"x";
            File saveFile = new File(savePath, _fileName);
            if (saveFile.exists()) {
                String sb250 = saveFile.toString();
                // OPCPackage s1 = OPCPackage.open(in)
                // InputStream in = new BufferedInputStream(new
                // FileInputStream(saveFile));
                List<UcstarYingjiSend> fileto = new ArrayList<UcstarYingjiSend>();
                // XSSFWorkbook wb = new XSSFWorkbook(s1);
                XSSFWorkbook wb = new XSSFWorkbook(sb250);
                // wb = new XSSFWorkbook();
                // sheet循环
                for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
                    // 第一张表的值
                    XSSFSheet st = wb.getSheetAt(numSheet);
                    // 获取第二行以下的信息
                    for (int rowNum = 0; rowNum <= st.getLastRowNum(); rowNum++) {
                        XSSFRow row = st.getRow(rowNum);
                        if (row == null) {
                            continue;
                        }
                        UcstarYingjiSend poom = new UcstarYingjiSend();
                        poom.setIncidentUri(_incidentUri);
                        for (short cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
                            String value = "";
                            XSSFCell cell = row.getCell(cellNum);
                            if (cell != null) {
                                // 注意:一定要设成这个,否则可能会出现乱码
                                switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_STRING:
                                    value = cell.getStringCellValue();
                                    break;
                                // 公式类型转换
                                case Cell.CELL_TYPE_FORMULA:
                                    value = new DecimalFormat("0").format(cell
                                            .getNumericCellValue());

                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    /*
                                     * if (DateUtil.isD(cell)){ value = new
                                     * java.text.SimpleDateFormat("HH:mm").format(cell.getDateCellValue()); }
                                     * else { value = new
                                     * DecimalFormat("0").format(cell.getNumericCellValue()); }
                                     */
                                    // String ss =
                                    // cell.getCellStyle().getDataFormatString();
                                    value = new java.text.SimpleDateFormat(
                                            "HH:mm").format(cell
                                            .getDateCellValue());
                                    break;
                                }
                            }
                            if (rowNum != 0 && rowNum != 1) {
                                if (cellNum == 0) {
                                    if (JiveGlobals.isEmpty(value)
                                            || !value.matches("[0-9]+")) {
                                        continue;
                                    }
                                    poom.setTaskId(value);
                                }
                                if (cellNum == 1) {
                                    if (JiveGlobals.isEmpty(value)) {
                                        continue;
                                    }
                                    // poom.setCreatetime(value);
                                    poom.setCreatetime("00:00");
                                }
                                if (cellNum == 2) {
                                    if ("否".equals(value)) {
                                        poom.setSameTime(0);
                                    } else if ("是".equals(value)) {
                                        poom.setSameTime(1);
                                    }
                                }
                                if (cellNum == 3) {
                                    poom.setScriptstr(value);
                                }
                                if (cellNum == 4) {
                                    poom.setCreator(value);
                                }
                                if (cellNum == 5) {
                                    poom.setReceivers(value);
                                }
                                if (cellNum == 6) {
                                    poom.setDetailTime(value);
                                }
                                if (cellNum == 7) {
                                    poom.setDetail(value);
                                }
                                if (cellNum == 8) {
                                    poom.setTitle(value);
                                }
                                if (cellNum == 9) {
                                    poom.setMemo(value);
                                }
                            }
                        }
                        // 需要判断字段的值是否都为空,等 其他验证条件
                        if (!JiveGlobals.isEmpty(poom.getTaskId())
                                && !JiveGlobals.isEmpty(poom.getReceivers())
                                && !JiveGlobals.isEmpty(poom.getCreatetime())) {
                            fileto.add(poom);
                        } else {
                            Log.console(">>演练脚本导入数据有误,行:" + rowNum);
                        }
                    }
                }
                /*
                 * if(in != null){ try{ in.close(); }catch(IOException ioe){ } }
                 */
                if (fileto.size() > 0
                        && UcstarYingjiSendServiceUtil.getFactory().getUcstarYingjiSendService().saveScriptDatas(fileto, _incidentUri)) {
                    flag = true;
                } else {
                    Log.error(">>202读取演练脚本数据为空");
                }
            } else { // 文件不存在提示用户
                Log.error(">>203演练脚本不存在");
            }
        } catch (Exception e) {
            Log.error(">>应急演练Excel文件数据异常:" + e);
        }
        return flag;
    }

【java】: 操作excel2007/2003的更多相关文章

  1. 解析xlsx文件---Java读取Excel2007

    关于Java读取Excel2007的文章在Google.百度上搜索一下,没有太好的例子,实现的也不算太好.查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2 ...

  2. Java操作Excle(基于Poi)

    有一次有个同事问我会不会有java操作Excle,回答当然是不会了!感觉被嘲讽了,于是开始寻找度娘,找到个小例子,结果越写越有意思,最后就成就了这个工具类. import java.io.Buffer ...

  3. java操作excel 工具类

    java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...

  4. Java操作Sqlite数据库-jdbc连接

    Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...

  5. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  6. Java操作Oracle

    public class DBCon { // 数据库驱动对象 public static final String DRIVER = "oracle.jdbc.driver.OracleD ...

  7. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  8. JAVA操作MongoDB数据库

    1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...

  9. Java操作Session与Cookie

    1,Java操作Session Java操作Session非常简单,步骤如下 1.1,在servlet中通过request获取session HttpSession session = request ...

随机推荐

  1. Linux查看系统资源使用情况(转)

    概述: 用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O ...

  2. ASP.NET MVC+EF5 开发常用代码

      Asp.Net Mvc,EF 技术常用点总结 1.Asp.Net MVC a)获得当前控制器名和当前操作的名称(action) 1.Action 中 RouteData.Values[" ...

  3. [Tex学习]WinEdit 常用软件快捷键

    WinEdit 常用软件快捷键 编辑: Alt+C:在剪贴板原有复制文本后增加新的被选择的文本. Ctrl+Shift+Alt+Right/Left:对选中文本增加或者删除Comment标记. Ctr ...

  4. yum install nginx

    先安装nginx的yum源 http://nginx.org/en/linux_packages.html#stable 找到链接,安装: rpm -ivh http://nginx.org/pack ...

  5. jquery.easypiechart.js简介

    此插件主要是用来统计新的访问.跳出率.服务器负载.使用的RAM等,功能很强大,带有HTML5的动画效果,效果非常炫,看效果吧easyPieChart一款新型的EASY饼图数据统计Jquery插件截图: ...

  6. Python从题目中学习:range()和xrange()

    近期给公司培训Python,好好啃了啃书本,查了查资料,总结一些知识点. --------------------------------------------------------------- ...

  7. Linux ToolChain (二) --- Linker (1)链接选项 -L -rpath -rpath-link

    一.动态库的链接和链接选项-L,-rpath-link,-rpath (1). 现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开, 用 ...

  8. android开发--(SimpleAdapter)运用

    SimpleAdapter的参数说明  第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要  第二个参数表示生成一个Map(String ,Object)列表选项    第三个 ...

  9. Android IOS WebRTC 音视频开发总结(七一)-- H265/H264有何不同

    本文整理自自网络,非原创,喜欢相关文章请关注我们的微信公众号:blackerteam H.265 H.265是ITU-TVCEG继H.264之后所制定的新的视频编码标准.H.265标准围绕着现有的视频 ...

  10. VHDL 学习

    近期在接触 VHDL,首先要本好书,个人觉得 1)<VHDL for engineer>  VHDL 大学实用教程 (这个名字翻译的无语...) 2)估计verilog的作者的 bhask ...