日期:2014-05-16  浏览次数:20470 次

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();   
    }
&n