欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java解析Excel内容的方法

程序员文章站 2024-03-03 19:25:46
本文实例讲述了java解析excel内容的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: import java.io.file; ...

本文实例讲述了java解析excel内容的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

import java.io.file; 
import java.io.fileinputstream; 
import java.io.inputstream; 
import java.util.arraylist; 
import org.apache.poi.hssf.usermodel.hssfworkbook; 
import org.apache.poi.ss.usermodel.cell; 
import org.apache.poi.ss.usermodel.row; 
import org.apache.poi.ss.usermodel.sheet; 
import org.apache.poi.ss.usermodel.workbook; 
import org.apache.poi.xssf.usermodel.xssfworkbook; 
public class test { 
    /**
     * @param args
     */ 
    public static void main(string[] args) { 
        meetquery("403", "e:\\excel\\1火灾三级预案处置流程.xlsx"); 
    } 
    private static arraylist<meetbean> meetquery(string level, string filename) { 
        arraylist<meetbean> list = new arraylist<meetbean>(); 
        try { 
            file file = new file(filename); 
            inputstream is = new fileinputstream(file); 
            workbook workbook = null;// 一个workbook对象,就是一个excel文件 
            sheet sheet = null;// sheet页,因excel总有多个sheet页,需要判断具体取值哪一个 
            row row1 = null;// sheet页数中的某一行 
            int colnum = 0;// sheet总行数 
            cell cell = null;// 第一列内容 
            cell cell1 = null;// 第二列内容 
            string meetname = null;// 要点名称 
            string meetid = null;// 要点编号 
            string meethine = null;// 要点提示内容 
            string meettime = null;// 处置时间 
            meetbean meet = null; 
            // 判断文件是什么格式 2003/2007 根据版本不同 处置对象也不同 
            if (filename.endswith(".xls")) { 
                workbook = new hssfworkbook(is);// excel 2003 
            } else if (filename.endswith(".xlsx")) { 
                workbook = new xssfworkbook(is);// excel 2007 
            } else { 
                return null; 
            } 
            // 判断处理那个sheet页,共有三个用户,分别处置不同的处置要点
            if (level == "401") { 
                // 值班站长的处置要点内容、处置提示 
                sheet = workbook.getsheetat(0); 
                colnum = sheet.getlastrownum();// 总行数 不包括标题内容 
                system.out.println("共有:" + colnum + "行"); 
                for (int i = 3; i <= colnum; i++) { 
                    meet = new meetbean(); 
                    row1 = sheet.getrow(i);// 要解析的行数 
                    cell = row1.getcell((short) 2);// 要解析要点名称的列数 
                    cell1 = row1.getcell((short) 4);// 要解析要点提示内容的列数 
                    if (cell != null && cell1 != null) { 
                        meetname = cell.getstringcellvalue(); 
                        meethine = cell1.getstringcellvalue(); 
                        meetid = "yd" + i; 
                        // 如果处置要点名称为空,则是循环到了最后一个处置要点,则返回。。。 
                        if (!meetname.equals("")) { 
                            string intstr = string 
                                    .valueof((int) (math.random() * 10 + 1));// 生成1-10的随机数 
                            // 如果是1-9随机数,则需要自动补零 时间格式为00:00:00 
                            if (intstr.length() < 2) { 
                                string min = "0" + intstr; 
                                meettime = "00:" + min + ":00"; 
                            } else { 
                                meettime = "00:" + intstr + ":00"; 
                            } 
                            meet.setmeetid(meetid);// 处置要点编号 
                            meet.setmeetname(meetname);// 处置要点名称 
                            meet.setmeethint(meethine);// 处置0要点提示内容 
                            meet.setmeettime(meettime);// 处置时间 
                            meet.setmeetlevel("401");// 处置要点级别 
                            list.add(meet); 
                        } else { 
                            return list; 
                        } 
                    } else { 
                        return list; 
                    } 
                } 
            } else if (level == "402") { 
                sheet = workbook.getsheetat(1);// occ调度员的处置要点内容、处置提示 
                colnum = sheet.getlastrownum();// 总行数 不包括标题内容 
                system.out.println("共有:" + colnum + "行"); 
                for (int i = 3; i <= colnum; i++) { 
                    meet = new meetbean(); 
                    row1 = sheet.getrow(i);// 要解析的行数 
                    cell = row1.getcell((short) 2);// 要解析要点名称的列数 
                    cell1 = row1.getcell((short) 4);// 要解析要点提示内容的列数 
                    if (cell != null && cell1 != null) { 
                        meetname = cell.getstringcellvalue(); 
                        meethine = cell1.getstringcellvalue(); 
                        meetid = "yd" + i; 
                        // 如果处置要点名称为空,则是循环到了最后一个处置要点,则返回。。。 
                        if (!meetname.equals("")) { 
                            string intstr = string 
                                    .valueof((int) (math.random() * 10 + 1));// 生成1-10的随机数 
                            // 如果是1-9随机数,则需要自动补零 时间格式为00:00:00 
                            if (intstr.length() < 2) { 
                                string min = "0" + intstr; 
                                meettime = "00:" + min + ":00"; 
                            } else { 
                                meettime = "00:" + intstr + ":00"; 
                            } 
                            meet.setmeetid(meetid);// 处置要点编号 
                            meet.setmeetname(meetname);// 处置要点名称 
                            meet.setmeethint(meethine);// 处置要点提示内容 
                            meet.setmeettime(meettime);// 处置时间 
                            meet.setmeetlevel("402");// 处置要点级别 
                            list.add(meet); 
                        } else { 
                            return list; 
                        } 
                    } else { 
                        return list; 
                    } 
                } 
            } else if (level == "403") { 
                sheet = workbook.getsheetat(2);// 控制中心的处置要点内容、处置提示 
                colnum = sheet.getlastrownum();// 总行数 不包括标题内容 
                system.out.println("共有:" + colnum + "行"); 
                int nameint = 0; 
                int hineint = 0; 
                for (int j = 0; j <= colnum; j++) { 
                    row1 = sheet.getrow(3);// 要解析的行数 只有第三行中存在处置要点名称,提示内容 
                    cell = row1.getcell((short) 1);// 要解析要点名称的列数 
                    cell1 = row1.getcell((short) 2);// 要解析要点提示内容的列数 
                } 
                for (int i = 3; i <= colnum; i++) { 
                    meet = new meetbean(); 
                    row1 = sheet.getrow(i);// 要解析的行数 
                    cell = row1.getcell((short) 1);// 要解析要点名称的列数 
                    cell1 = row1.getcell((short) 2);// 要解析要点提示内容的列数 
                    if (cell != null && cell1 != null) { 
                        meetname = cell.getstringcellvalue(); 
                        meethine = cell1.getstringcellvalue(); 
                        meetid = "yd" + i; 
                        // 如果处置要点名称为空,则是循环到了最后一个处置要点,则返回。。。 
                        if (!meetname.equals("")) { 
                            string intstr = string 
                                    .valueof((int) (math.random() * 10 + 1));// 生成1-10的随机数 
                            // 如果是1-9随机数,则需要自动补零 时间格式为00:00:00 
                            if (intstr.length() < 2) { 
                                string min = "0" + intstr; 
                                meettime = "00:" + min + ":00"; 
                            } else { 
                                meettime = "00:" + intstr + ":00"; 
                            } 
                            meet.setmeetid(meetid);// 处置要点编号 
                            meet.setmeetname(meetname);// 处置要点名称 
                            meet.setmeethint(meethine);// 处置要点提示内容 
                            meet.setmeettime(meettime);// 处置时间 
                            meet.setmeetlevel("403");// 处置要点级别 
                            list.add(meet); 
                        } else { 
                            return list; 
                        } 
                    } else { 
                        return list; 
                    } 
                } 
            }   
            is.close(); 
        } catch (exception e) { 
            e.printstacktrace(); 
        } 
        return list; 
    } 
}

希望本文所述对大家的java程序设计有所帮助。