简单excel导入导出

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 *
 * @ClassName: ExcelOperate
 * @Description: TODO(使用jxl简单实现Excel导入导出)
 * @date 2016年8月3日 下午2:12:53
 *
 */

public class ExcelOperate {
    public static void main(String[] args) {
        //writeToFile();
//        /this.readFromFile();
    }
    int col=100,row =100;
    
    /**
     *
     * @Title: ExcelOperate
     * @Description: TODO(读取excel文件里的内容)
     * @param @param path    设定文件
     * @return @param path    返回类型
     * @throws
     */
    public boolean readExcel(String path){
        boolean flag=false;
        File file = new File(path);
//        File file = new File("C:/Users/Administrator/Desktop/mmonitor.xls");
        StringBuffer sb = new StringBuffer();
        try {
            Workbook book = Workbook.getWorkbook(file);//拿到excel工作空间
            try{
                Sheet sheet = book.getSheet(0);
                col = book.getSheet(0).getColumns();//获取excel文件有列
                row = book.getSheet(0).getRows();//获取excel文件有行
                System.out.println(col+"=="+row);
                for(int i = 1 ; i < col ; i++){
                    for(int j = 1 ; j <row ; j++){
                        //第一个参数代表列,第二个参数代表行。(默认起始值都为0)
                        try{
                            Cell jj =sheet.getCell(j, i);//获取excel单个格子
                            if(jj!=null){
                                String cont=jj.getContents();//获取格子的内容
                                sb.append(cont+" ");
                                System.out.println(cont);
                            }
                        }catch(Exception e){
                            System.out.println("这个表格没有值");
                        }
                    }
                    sb.append(" ");
                }
                flag=true;
                //System.out.println(sb);
            }finally{
                if(book != null){
                    book.close();
                }
            }
        } catch (BiffException e) {
            System.err.println(e+"");
        } catch (IOException e) {
            System.err.println(e+"文件读取错误");
        }
        return flag;
    }//end readFromFile
    
    //witeToFile
    public  void writeToFile(){
        File file = new File("C:/Users/Administrator/Desktop/mmonitor.xls");
        try {
            
            WritableWorkbook book = Workbook.createWorkbook(file);
            //创建一个工作区。(默认的excel文件有三个sheet,在excel的左下角可以看到sheet1/sheet2/sheet3)
            WritableSheet sheet = book.createSheet("第一页", 0);
            //在工作区上面添加内容
            try {
                for(int i = 0; i < 10 ; i ++ ){
                    for(int j = 0 ; j < 10 ; j++){
                        Label newLabel;
                        if(0 == i){
                            //第一个参数代表列,第二个参数代表行(默认起始值都为0),第三个参数是要在单元格里面填写的内容发
                            newLabel = new Label(j,i,String.valueOf("的"));
                        }else if(0 == j){
                            newLabel = new Label(j,i,String.valueOf("的"));
                        }else{
                            newLabel = new Label(j,i,String.valueOf("的"));
                        }
                        //在单元格上面添加注释
//                        WritableCellFeatures cellFeatures = new WritableCellFeatures();
//                        cellFeatures.setComment("这里是"+i+"*"+j+"的值");
//                        System.out.println("这里是"+i+"*"+j+"的值");
//                        newLabel.setCellFeatures(cellFeatures);
                        sheet.addCell(newLabel);
                    }
                }
            } catch (RowsExceededException e) {
                System.err.println(e+"行或列参数错误!");
            } catch (WriteException e) {
                System.err.println(e+"写入失败");
            }finally{
                if(book != null){
                    book.write();
                    try {
                        book.close();
                    } catch (WriteException e) {
                        System.err.println(e+"文件关闭失败!");
                    }
                }
            }
            
        } catch (IOException e) {
            System.err.println(e+"创建文件失败!");
        }
    }
    
    
}

原文地址:https://www.cnblogs.com/dreamwf/p/5732950.html