zoukankan      html  css  js  c++  java
  • poi 导入/导出 工具类

    package com.holy.util;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.text.DecimalFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class ExcelExporterOrImp {
        /**
         * 导出数据
         * @param os
         * @param data
         * @throws IOException
         */
        public static void exportToExcel(OutputStream os, List<List<String>> data) throws IOException {
            HSSFWorkbook wb=null;
            try {
                wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("Data");
    
                for (int r = 0 ; r < data.size() ; r++) {
                    HSSFRow row = sheet.createRow(r);
                    
                    List<String> cols = data.get(r);
                    for (int c = 0 ; c < cols.size() ; c++) {
                        HSSFCell cell = row.createCell(c);
                        cell.setCellValue(new HSSFRichTextString(cols.get(c)));
                    }
                }
    
                
            } catch (Exception e) {
                
                e.printStackTrace();
            }finally{
                if(wb!=null){
                    wb.write(os);
                }
                os.flush();
                os.close();
            }
    }
        /**
         * 数据导入
         * @param is
         * @return
         * @throws IOException
         */
        public static List<List<String>> importFromExcel(InputStream is) throws IOException {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<List<String>> data = new ArrayList<List<String>>();
            try {
                for (int r = sheet.getFirstRowNum() ; r <= sheet.getLastRowNum() ; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
    
                    List<String> cols = new ArrayList<String>(); 
                    for (int c = row.getFirstCellNum() ; c < row.getLastCellNum() ; c++) {
                        HSSFCell cell = row.getCell(c);
                        if (cell == null) {
                            cols.add("");
                        } else {
                            switch(cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_STRING:
                                cols.add(cell.getStringCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                cols.add(""+cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                                    cols.add(sdf.format(cell.getDateCellValue()));
                                } else {
                                    cols.add(""+cell.getNumericCellValue());
                                }
    
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                cols.add(cell.getCellFormula());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                cols.add("");
                                break;
                            }                        
                        }
                    }
    
                    data.add(cols);
                }
            } finally {
                is.close();
            }
            return data;
        }
        /**
         * 
         * @param is
         * @param firstCellNum
         * @param lastCellNum
         * @return
         * @throws IOException
         */
        public static List<List<String>> importFromExcelWithCell(InputStream is,int firstCellNum,int lastCellNum) throws IOException {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<List<String>> data = new ArrayList<List<String>>();
            try {
                for (int r = sheet.getFirstRowNum() ; r <= sheet.getLastRowNum() ; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
    
                    List<String> cols = new ArrayList<String>(); 
                    for (int c = firstCellNum ; c < lastCellNum ; c++) {
                        HSSFCell cell = row.getCell(c);
                        if (cell == null) {
                            cols.add("");
                        } else {
                            switch(cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_STRING:
                                cols.add(cell.getStringCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                cols.add(""+cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                                    cols.add(sdf.format(cell.getDateCellValue()));
                                } else {
                                    cols.add(""+cell.getNumericCellValue());
                                }
    
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                cols.add(cell.getCellFormula());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                cols.add("");
                                break;
                            }                        
                        }
                    }
    
                    data.add(cols);
                }
            } finally {
                is.close();
            }
            return data;
        }
        /**
         * 该方法把num类型强转成了string
         * @param is
         * @param firstCellNum
         * @param lastCellNum
         * @return
         * @throws IOException
         */
        public static List<List<String>> importFromExcelWithCellWithNUm(InputStream is,int firstCellNum,int lastCellNum) throws IOException {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            DecimalFormat df = new DecimalFormat("#");
            List<List<String>> data = new ArrayList<List<String>>();
            try {
                for (int r = sheet.getFirstRowNum() ; r <= sheet.getLastRowNum() ; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
    
                    List<String> cols = new ArrayList<String>(); 
                    for (int c = firstCellNum ; c < lastCellNum ; c++) {
                        HSSFCell cell = row.getCell(c);
                        if (cell == null) {
                            cols.add("");
                        } else {
                            switch(cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_STRING:
                                cols.add(cell.getStringCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                cols.add(""+cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                                    cols.add(sdf.format(cell.getDateCellValue()));
                                } else {
    
                                    cols.add( ""+df.format(cell.getNumericCellValue()));
    //                                cols.add(""+cell.getNumericCellValue());
                                }
    
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                cols.add(cell.getCellFormula());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                cols.add("");
                                break;
                            }                        
                        }
                    }
    
                    data.add(cols);
                }
            } finally {
                is.close();
            }
            return data;
        }
    
        /***
         * 该方法把导入excel时对于数字类型的数据进行处理
         * 一般情况下excel中数字太长会转化成科学计数法
         * @param is
         * @return
         * @throws IOException
         */
        public static List<List<String>> importExcelWithCellWithNum(InputStream is) throws IOException {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            DecimalFormat df = new DecimalFormat("#");
            List<List<String>> data = new ArrayList<List<String>>();
            try {
                for (int r = sheet.getFirstRowNum() ; r <= sheet.getLastRowNum() ; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
    
                    List<String> cols = new ArrayList<String>(); 
                    for (int c = row.getFirstCellNum() ; c < row.getLastCellNum() ; c++) {
                        HSSFCell cell = row.getCell(c);
                        if (cell == null) {
                            cols.add("");
                        } else {
                            switch(cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_STRING:
                                cols.add(cell.getStringCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                cols.add(""+cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                                    cols.add(sdf.format(cell.getDateCellValue()));
                                } else {
                                    cols.add( ""+df.format(cell.getNumericCellValue()));
                                }
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                cols.add(cell.getCellFormula());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                cols.add("");
                                break;
                            }                        
                        }
                    }
    
                    data.add(cols);
                }
            } finally {
                is.close();
            }
            return data;
        }
        
         
        public static List<List<String>> importExcelWithNullCell(InputStream is) throws IOException {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            DecimalFormat df = new DecimalFormat("#");
            List<List<String>> data = new ArrayList<List<String>>();
            
            try {
                int lastCellNum = sheet.getRow(sheet.getFirstRowNum()).getLastCellNum();
                
                for (int r = sheet.getFirstRowNum() ; r <= sheet.getLastRowNum() ; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
    
                    List<String> cols = new ArrayList<String>(); 
                    for (int c = 0 ; c < lastCellNum ; c++) {
                        HSSFCell cell = row.getCell(c);
                        if (cell == null) {
                            cols.add("");
                        } else {
                            switch(cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_STRING:
                                cols.add(cell.getStringCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                cols.add(""+cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                                    cols.add(sdf.format(cell.getDateCellValue()));
                                } else {
                                    cols.add( ""+df.format(cell.getNumericCellValue()));
                                }
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                cols.add(cell.getCellFormula());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                cols.add("");
                                break;
                            }                        
                        }
                    }
    
                    data.add(cols);
                }
            } finally {
                is.close();
            }
            return data;
        }
        
            
        public static void main(String[] args) throws IOException {
            
            FileOutputStream os = new FileOutputStream(new File("d:/test.xls"));
            List<List<String>> data = new ArrayList<List<String>>();
            List<String> s = new ArrayList<String>();
            s.add("11");
            s.add("22");
            s.add("33");
            data.add(s);
            List<String> s1 = new ArrayList<String>();
            s1.add("11");
            s1.add("22");
            s1.add("33");
            data.add(s1);
            exportToExcel(os, data);
            System.out.println("data:"+data);
            
    //        FileInputStream is = new FileInputStream(new File("d:/bank.xlsx"));
    //        List<List<String>> data = importFromExcel(is);
    //        System.out.println("data:"+data);
        }
          
    
    }
  • 相关阅读:
    可爱的中国电信 请问我们的电脑还属于我们自己吗?
    了解客户的需求,写出的代码或许才是最优秀的............
    DELPHI DATASNAP 入门操作(3)简单的主从表的简单更新【含简单事务处理】
    用数组公式获取字符在字符串中最后出现的位置
    在ehlib的DBGridEh控件中使用过滤功能(可以不用 MemTableEh 控件 适用ehlib 5.2 ehlib 5.3)
    格式化json返回的时间
    ExtJs中使用Ajax赋值给全局变量异常解决方案
    java compiler level does not match the version of the installed java project facet (转)
    收集的资料(六)ASP.NET编程中的十大技巧
    收集的资料共享出来(五)Asp.Net 权限解决办法
  • 原文地址:https://www.cnblogs.com/aGboke/p/5531894.html
Copyright © 2011-2022 走看看