Java 金蝶中用POI和JXL读取Excel文件

package com.kingdee.eas.shwater.vchImport;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi4cp.hssf.usermodel.HSSFWorkbook;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadXlsToGrid {
	File iffile=null;
	String[][] tascellgrid;
	private InputStream tinsfile=null;
	private boolean tbxls;//true xls2003 false xlsx2007
	private String tsat="";
	private boolean bbindFileSuc_nomoused(String psfileurl)
	{
		
		iffile=new File(psfileurl);
    	try {
			tinsfile = new FileInputStream(iffile);
	    	if (isExcel2003_abandonered(tinsfile))
	    		tbxls=true;
	    	else
	    		if (isExcel2007_abandonered(tinsfile))
	    			tbxls=false;
	    		else
	    			return false;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return true;
	}


	private boolean isExcel2003_abandonered(InputStream tinsfile)
	{
		try {
			new HSSFWorkbook(tinsfile);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

//https://blog.csdn.net/hekuilove/article/details/84604348
	private boolean isExcel2007_abandonered(InputStream tinsfile) 
	{
		try {
			new XSSFWorkbook(tinsfile);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	//https://www.cnblogs.com/muliu/p/6812347.html
	private boolean obfileFormatMatch=false;//如果一种文件格式正确,就不再尝试另一种
	public String readSheet(String psfileurl,String pssheetName)
	{
		String tryReadSheet="";
		obfileFormatMatch=false;
		if (psfileurl.endsWith(".xlsx"))
		{
			tryReadSheet=poiExcel_xlsx(psfileurl,pssheetName);
			if ("".equals(tryReadSheet)==false&&obfileFormatMatch==false)
				tryReadSheet=jlxExcel_xls(psfileurl,pssheetName);
		}
		else 
		{
			tryReadSheet=jlxExcel_xls(psfileurl,pssheetName);
			if ("".equals(tryReadSheet)==false&&obfileFormatMatch==false)
				tryReadSheet=poiExcel_xlsx(psfileurl,pssheetName);
		}
		return tryReadSheet;
		 
	}
	private String jlxExcel_xls(String psfileurl,String pssheetName)
	{
		int fnprogress=0;
        //===============jlx方法=================
		boolean lbreadxlsfileSuc=true;
		Workbook rwb=null;
        try{
        	iffile=new File(psfileurl);
        // 构造Workbook(工作薄)对象
	        tsat="读取文件";
	        rwb=Workbook.getWorkbook(iffile);
	        obfileFormatMatch=true;
	        tsat="读取工作页";
	        Sheet rs=rwb.getSheet(pssheetName);//获取第一张工作表
	        if (rs==null)
	        	return "没有找到工作页:"+pssheetName;
	        int rsRows=rs.getRows();//获取Sheet表中所包含的总行数
	        int rsCols=rs.getColumns();//获取Sheet表中所包含的总列数
	        this.tascellgrid=new String[rsRows][rsCols];  
	        tsat="读取单元格";
	        for(int i=0;i<rsRows;i++)
	        {//读取行            
	            for(int j=0;j<rsCols;j++)
	            {                
	                Cell coo=rs.getCell(j, i);//单元格定位列,再定位行                
	                String strc=coo.getContents();//读取内容
	                tascellgrid[i][j]=strc;
	            }
	        }
 
	        }catch (FileNotFoundException e) {
	        	lbreadxlsfileSuc=false;
	        	return tsat;
	             //e.printStackTrace();  
	        } catch (BiffException e) {
	        	lbreadxlsfileSuc=false;
	        	return tsat;
	        	//e.printStackTrace();  
	        } catch (IOException e) {  
	        	lbreadxlsfileSuc=false;
	        	return tsat;
	         //e.printStackTrace();
	        
	    } finally
	    {
	    	if (rwb!=null)
	    		rwb.close();
	    }
	        
        if (lbreadxlsfileSuc==false)
        	return "读取文件发生错误,请联系管理员";
        	

        return "";
	}
	private String poiExcel_xlsx(String psfileurl,String pssheetName)
	{
		int vnrow=0,vncol=0;
		File vffile=new File(psfileurl);
    	FileInputStream vfis = null;
        XSSFWorkbook xwb;
        try{
        	tsat="文件流";
        	vfis=new FileInputStream(vffile);
        	tsat="文件以xlsx读取";
            xwb = new XSSFWorkbook(vfis);  //利用poi读取excel文件流
            obfileFormatMatch=true;
            tsat="读取sheet";
            XSSFSheet st = xwb.getSheet(pssheetName);  //读取sheet的第一个工作表
            if (st==null)
            	return "没有名为"+pssheetName+"的工作页";
            int rows=st.getLastRowNum();//总行数
            int cols;//总列数
            int vnmaxcolcnt=0;
            Integer[] anrowlen=new Integer[rows];
            for(vnrow=0;vnrow<rows;vnrow++)
            {
            	tsat="正在预读第"+vnrow+"行";
                XSSFRow row=st.getRow(vnrow);//读取某一行数据
                int fnrowlen=row.getLastCellNum();
                anrowlen[vnrow]=fnrowlen;
                if (fnrowlen>vnmaxcolcnt)
                	vnmaxcolcnt=fnrowlen;
            }
            tascellgrid=new String[rows][vnmaxcolcnt];
            for(vnrow=0;vnrow<rows;vnrow++)
            {
            	XSSFRow row=st.getRow(vnrow);//读取某一行数据
            	//tsat="正在读第"+vnrow+"行";
                if(row!=null)
                {
	                    //获取行中所有列数据
	                cols=anrowlen[vnrow];
	                for(vncol=0;vncol<cols;vncol++)
	                {
	                    XSSFCell cell=row.getCell(vncol);
	                    if(cell==null)
	                    	tascellgrid[vnrow][vncol]="";
	                    else{
	                    //判断单元格的数据类型
	                    switch (cell.getCellType()) { 
	                        case XSSFCell.CELL_TYPE_NUMERIC: // 数字 
	                            tascellgrid[vnrow][vncol]=""+cell.getNumericCellValue();
	                            break; 
	                        case XSSFCell.CELL_TYPE_STRING: // 字符串 
	                        	tascellgrid[vnrow][vncol]=cell.getStringCellValue(); 
	                            break; 
	                        case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean 
	                            //System.out.println(cell.getBooleanCellValue() + "   "); 
	                            break; 
	                        case XSSFCell.CELL_TYPE_FORMULA: // 公式 
	                            //System.out.print(cell.getCellFormula() + "   "); 
	                            break; 
	                        case XSSFCell.CELL_TYPE_BLANK: // 空值 
	                            break; 
	                        case XSSFCell.CELL_TYPE_ERROR: // 故障  
	                            break; 
	                        default: 
	                            break; 
	                        } 
	                    }
	                }
                }
            }
        }catch(IOException e){
        	e.printStackTrace();  
        	return "读第"+vnrow+"行第"+vncol+"列时候出错";
            
        }
        catch(Exception e){
        	e.printStackTrace();  
            
        }
        
        finally
        {
        	//xwb.close();
        	try {
        		if (vfis!=null)
        			vfis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
        }
        return "";
    }
}

金蝶的业务读取只需要读取文字包括数字就可以了,其他问题不大。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值