码迷,mamicode.com
首页 > 其他好文 > 详细

使用jxl,poi读取excel文件

时间:2015-11-27 17:08:22      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

作用:在java后台添加一个方法,读取导入的excel内容,根据需要返回相应的sql语句,以完成对临时表的插入操作。

使用jxl读取excel文件

package com.sixthf.bi.sapp.util;

import java.io.IOException;
import java.io.InputStream;

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

import org.apache.struts.upload.FormFile;

import com.got.util.uid.UUIDGener;

public class PhoneUtil{
    
    /**
     * 读取xls文件内容
     * 
     * @param file 想要读取的文件对象,importfilesn;
     * @return  sql语句
     */
public static String readPhoneExcel(FormFile formFile,String importfilesn) throws IOException, BiffException  {
      InputStream stream = formFile.getInputStream();
        Workbook rwb = Workbook.getWorkbook(stream);
        Sheet[] sheets = rwb.getSheets(); 
        StringBuffer SBbuffer = new StringBuffer(); 
        
         for(int i=0;i<sheets.length;i++) {
            Sheet rs = rwb.getSheet(i);
            String period = rs.getCell(0, 1).getContents().replace("年", "-").replace("月", "");//得到当前工作表的第二行第一列即账务周期
            for(int j=1;j<rs.getRows();j++) {
                String first = rs.getCell(0, j).getContents();
                String phone = rs.getCell(1, j).getContents();//得到当前工作表的当前行的第二列即接入号码
                if(!first.contains("合计")) {
                    if(!phone.equals("") && !phone.equals("小计:")) {
                        SBbuffer
                        .append("insert into CMMU_PAPERANSWER_TMP (SUBIMPORTFILESN,Importfilesn,answer1,answer2, answer5)values(")
                        .append("‘")
                        .append(UUIDGener.getUUID().replaceAll("-", ""))
                        .append("‘,‘") 
                        .append(importfilesn)
                        .append("‘,‘")
                        .append(period)
                        .append("‘,‘")
                        .append(phone)
                        .append("‘,‘");
                        
                        for(int k=j+1;k<rs.getRows(); k++) {
                        phone = rs.getCell(1, k).getContents();
                            if(phone.contains("小计")) {
                                String costnum = rs.getCell(3, k).getContents();//得到当前工作表的当前行的第四列即费用
                                
                                SBbuffer
                                .append(costnum)
                                .append("‘);")
                                .append("\n");
                                j = k;
                                break;
                            }
                            else continue;
                        }
                    }
                else continue;
                }
                else break;
            }
        }
        String string = SBbuffer.toString();
        return string;
    } 
        
   }

使用poi读取excel

package com.sixthf.bi.sapp.util;

import java.io.IOException;
import java.io.InputStream;

import jxl.read.biff.BiffException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.upload.FormFile;

import com.got.util.uid.UUIDGener;

public class PhoneUtil{
    
    /**
     * 读取xls文件内容
     * 
     * @param file 想要读取的文件对象,importfilesn;
     * @return  sql语句
     */
public static String readPhoneExcel(FormFile formFile,String importfilesn) throws IOException, BiffException  {
       InputStream stream = formFile.getInputStream();
        HSSFWorkbook rwb = new HSSFWorkbook(stream);
        int numSheet = rwb.getNumberOfSheets();
        StringBuffer SBbuffer = new StringBuffer(); 
        
         for(int i=0;i<numSheet;i++) {
            HSSFSheet rs = rwb.getSheetAt(i);
            String period = rs.getRow(1).getCell((short)0).toString().replace("年", "-").replace("月", "");
            for(int j=1;j<rs.getPhysicalNumberOfRows();j++) {
                String first = rs.getRow(j).getCell((short) 0).toString();
                String phone = rs.getRow(j).getCell((short) 1).toString();//得到当前工作表的当前行的第二列即接入号码
                if(!first.contains("合计")) {
                    if(!phone.equals("") && !phone.equals("小计:")) {
                        SBbuffer
                        .append("insert into CMMU_PAPERANSWER_TMP (SUBIMPORTFILESN,Importfilesn,answer1,answer2, answer5)values(")
                        .append("‘")
                        .append(UUIDGener.getUUID().replaceAll("-", ""))
                        .append("‘,‘") 
                        .append(importfilesn)
                        .append("‘,‘")
                        .append(period)
                        .append("‘,‘")
                        .append(phone)
                        .append("‘,‘");
                        
                        for(int k=j+1;k<rs.getPhysicalNumberOfRows(); k++) {
                        phone = rs.getRow(k).getCell((short) 1).toString();
                            if(phone.contains("小计")) {
                                String costnum = rs.getRow(k).getCell((short) 3).toString();//得到当前工作表的当前行的第四列即费用
                                
                                SBbuffer
                                .append(costnum)
                                .append("‘);")
                                .append("\n");
                                j = k;
                                System.out.println(k);
                                break;
                            }
                            else continue;
                        }
                    }
                else continue;
                }
                else break;
            }
        }
        String string = SBbuffer.toString();
        return string;
    } 
        
    }

jxl.jar包下载链接: http://pan.baidu.com/s/1i3k6ccP

poi.jar包下载链接: http://pan.baidu.com/s/1jGrVBCi

 

使用jxl,poi读取excel文件

标签:

原文地址:http://www.cnblogs.com/hdzcz/p/5000924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!