当前位置: 代码迷 >> JavaScript >> JSP读取exl资料实例
  详细解决方案

JSP读取exl资料实例

热度:204   发布时间:2013-09-05 16:02:07.0
JSP读取exl文件实例
JSP读写Office文件主要用到的是POI工具,不过它现在只是对Excel支持比较好。
下载地址:http://archive.apache.org/dist/jakarta/poi/release/bin/
解压缩后得到三个类库文件,加入WEB应用的WEB-INF\lib目录下即可

package myexcel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CreateNewExcel {
    public static void main(String[] args) throws Exception {
       WriteExcel();
       ReadExcel();
    }
//写Excel文件
    public static void WriteExcel() throws Exception{
         //建立文件流
        FileOutputStream fos=new FileOutputStream("c:\\foo.xls");
       
        //创建一个Excel文件对象
        HSSFWorkbook wb=new HSSFWorkbook();
        //在文件对象上建立一个sheet,并命名为cliff
        HSSFSheet s=wb.createSheet();
        wb.setSheetName(0,"cliff");
       
        //在sheet上输入数据
        for(short i=0;i<10;i++){
            HSSFRow row=s.createRow(i);
            for(short j=0;j<10;j++){
                HSSFCell cell=row.createCell(j);
                cell.setCellValue(""+i+""+j);
            }
        }
       
        //建立第二个sheet
        HSSFSheet s1=wb.createSheet("c1");
        HSSFRow row=s1.createRow(0);
        for(short i=0;i<10;i++){
            HSSFCell cell=row.createCell(i);
            cell.setCellValue(""+i);
        }
        for(short i=1;i<10;i++){
               HSSFRow row1=s1.createRow(i);
               HSSFCell cell=row1.createCell((short)0);
                cell.setCellValue(""+i);
            for(short j=1;j<10;j++){
                HSSFCell cell1=row1.createCell(j);
                cell1.setCellValue(""+i*j);
            }
        }
        //把数据写回到磁盘,并关闭输出流
        wb.write(fos);
        fos.close();   
    }
   
    /*读取Excel,注意:
     * 1 sheet以0开始,以workbook.getNumberOfSheets()-1结束
     * 2 row以0开始,以getLastRowNum结束
     * 3 cell以0开始,以getLastCellNum结束,结束的数目不知什么原因有可能偏长
     */
   
    public static void ReadExcel(){
        DecimalFormat df=new DecimalFormat("#");
        String fileName="c:\\foo.xls";
        try{
            //创建对Excel文件的的引用
            HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(fileName));
            //获取sheet数,并循环处理所有的sheet
            for(int sheetsNum=0;sheetsNum<wb.getNumberOfSheets();sheetsNum++){
                if(null!=wb.getSheetAt(sheetsNum)){
                    //获得一个sheet
                    HSSFSheet aSheet=wb.getSheetAt(sheetsNum);
                    //获得当前sheet的行数,并循环获得所有的行
                    for(int rowNum=0;rowNum<=aSheet.getLastRowNum();rowNum++){
                        if(null!=aSheet.getRow(rowNum)){
                            HSSFRow aRow=aSheet.getRow(rowNum);
                            //获得当前行的单元数,并循环获得所有的单元
                            for(short cellNum=0;cellNum<=aRow.getLastCellNum();cellNum++){
                                if(null!=aRow.getCell(cellNum)){
                                    HSSFCell aCell=aRow.getCell(cellNum);
                                    //获得单元数据的类型
                                    int cellType=aCell.getCellType();
                                    switch(cellType){
                                        case 0://Number
                                            String strCell=df.format(aCell.getNumericCellValue());
                                            System.out.print(strCell+ "   ");
                                            break;
                                        case 1://String
                                            strCell=aCell.getStringCellValue();
                                            System.out.print(strCell+ "   ");
                                            break;
                                        default:
                                            System.out.print("数据格式不对");
                                    }
                                }
                            }
                            System.out.println(" ");
                        }
                    }
                }
            }
        }catch(Exception e){
            System.out.println("Read Excel Error" +e);
        }
    }
}
  相关解决方案