程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java解析Excel內容的辦法

Java解析Excel內容的辦法

編輯:關於JAVA

Java解析Excel內容的辦法。本站提示廣大學習愛好者:(Java解析Excel內容的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java解析Excel內容的辦法正文


本文實例講述了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法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved