利用java操作Excel文件

    技术2022-05-11  65

     转自:http://blog.csdn.net/chensheng913/archive/2007/03/02/1519050.aspx

    整理一下,以后用得着。

    package  sample; 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.WritableImage; import  jxl.write.WritableSheet; import  jxl.write.WritableWorkbook; import  jxl.write.WriteException; import  jxl.write.biff.RowsExceededException; public   class  ExcelTest  {    /**     * http://sourceforge.net/project/showfiles.php?group_id=79926     */        public static void main(String[] args) {                File file = new File("C:/test1.xls");        WritableWorkbook workbook = null;        try {            workbook = Workbook.createWorkbook(file);            WritableSheet imgSheet = workbook.createSheet("Images",0);                   File imgFile = new File("C:/1.png");                insertImg(imgSheet,0,0,10,30,imgFile);            workbook.write();            workbook.close();        } catch (WriteException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }     }        public static String readExcel(File file){                 StringBuffer sb = new StringBuffer();         Workbook wb = null;                try {            wb=Workbook.getWorkbook(file);        } catch (BiffException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }                if(wb == nullreturn null;                Sheet[] sheet = wb.getSheets();        if(sheet!=null&&sheet.length>0){            for(int i = 0; i < sheet.length; i++){                int rowNum = sheet[i].getRows();                    for(int j = 0;j < rowNum; j++){                    Cell[] cells = sheet[i].getRow(j);                    if(cells!=null&&cells.length>0){                        for(int k = 0; k < cells.length; k++){                            String cellValue = cells[k].getContents();                            sb.append(cellValue+" ");                        }                    }                    sb.append(" ");                }                sb.append(" ");                }        }        wb.close();        return sb.toString();    }        public static void writeExcel(String fileName){                WritableWorkbook wwb = null;        try {            wwb = Workbook.createWorkbook(new File(fileName));        } catch (IOException e) {            e.printStackTrace();        }        if(wwb!=null){            WritableSheet ws = wwb.createSheet("sheet1"0);            for(int i=0;i<10;i++){                for(int j=0;j<5;j++){                    Label labelC = new Label(j, i, "ROW:"+(i+1)+",CELL:"+(j+1));                    try {                        ws.addCell(labelC);                    } catch (RowsExceededException e) {                        e.printStackTrace();                    } catch (WriteException e) {                        e.printStackTrace();                    }                }            }        }        try {            wwb.write();            wwb.close();            } catch (IOException e) {            e.printStackTrace();        } catch (WriteException e) {            e.printStackTrace();        }    }        public static boolean searchKeyWord(File file,String keyWord){                boolean res = false;        Workbook wb = null;                try {            wb=Workbook.getWorkbook(file);        } catch (BiffException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }                if(wb == nullreturn res;                Sheet[] sheet = wb.getSheets();                boolean breakSheet = false;        if(sheet!=null&&sheet.length>0){            for(int i = 0; i < sheet.length; i++){                if(breakSheet) break;                int rowNum = sheet[i].getRows();                boolean breakRow = false;                for(int j = 0;j < rowNum; j++){                    if(breakRow) break;                    Cell[] cells = sheet[i].getRow(j);                    if(cells!=null&&cells.length>0){                        boolean breakCell = false;                        for(int k = 0; k < cells.length; k++){                            if(breakCell) break;                             String cellValue = cells[k].getContents();                             if(cellValue == nullcontinue;                             if(cellValue.contains(keyWord)){                                 res = true;                                     breakCell = true;                                     breakRow = true;                                     breakSheet = true;                             }                        }                    }                }              }        }        wb.close();        return res;      }        public static void insertImg(WritableSheet dataSheet, int col, int row, int width, int height, File imgFile){            WritableImage img = new WritableImage(col, row, width, height, imgFile);            dataSheet.addImage(img);        }

    最新回复(0)