本帖最后由 heiniuyang 于 2014-09-15 19:29:49 编辑

解决方案 »

  1.   

    用JXL吧,比POI好使,
    导入Excel文件:
    public class TestExcel2 {
    public static void main(String[] args) {
            // 准备设置Excel工作表的标题
            String[] title = { "ID", "名称","停用" };
            try {
                long start = System.currentTimeMillis();// 获得开始时间
                // Excel输出路径
                String filePath = "C:\\Users\\Administrator\\Desktop\\test.xls";
                WritableWorkbook wwb;// 创建Excel工作簿
                //在filePaht目录下生成createExcel文件
                OutputStream os = new FileOutputStream(filePath);
                wwb = Workbook.createWorkbook(os);
                // 添加第一个工作表并设置第一个sheet的名字,0是工作表的索引
                WritableSheet sheet = wwb.createSheet("清单", 0);
               
                WritableCellFormat wc = new WritableCellFormat();
                // 设置居中
                wc.setAlignment(Alignment.CENTRE);
               // 设置边框线
                wc.setBorder(Border.ALL, BorderLineStyle.THIN);
                // 设置单元格的背景颜色
                wc.setBackground(jxl.format.Colour.CORAL);
                // 设置字体
                WritableFont wfont = null;
                wfont = new WritableFont(WritableFont.createFont("微软雅黑"), 13);
                WritableCellFormat font = new WritableCellFormat(wfont);
                
                Label label;
                sheet.setRowView(0, 600);//设置某行的行高(600/20)
                for (int i = 0; i < title.length; i++) {
                 sheet.setColumnView(i, 15);//设置某列(0)的列宽(20)
                    // Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是z
                    label = new Label(i, 0, title[i],wc);
                    sheet.addCell(label);// 将定义好的单元格添加到sheet表中
                }
                // 下面是填充数据
                // 填充餐厅编号
                Number num  = new Number(0, 1,2);
                sheet.addCell(num);
                label = new Label(1, 1, "张先生");
                sheet.addCell(label);
                Number num1  = new Number(0, 2,3);
                sheet.addCell(num1);
                label = new Label(1, 2, "李先生");
                sheet.addCell(label);
                
                List angerlist = new ArrayList(); 
                angerlist.add("是"); 
                angerlist.add("否");
                label = new Label(1, 3, "请选择");
                WritableCellFeatures ws = new WritableCellFeatures();
                ws.setDataValidationList(angerlist);
                label.setCellFeatures(ws);
                sheet.addCell(label);
                
                //图片
                /*
                String imgPath = "F:\\background\\123.png";
         File imgFile = new File(imgPath);
         WritableImage image = new WritableImage(14,1,2, 5, imgFile);
         sheet.addImage(image);
               */
                //sheet.mergeCells(0, 3, 2, 3); // 1列4行~3列4行
                //label = new Label(0, 3, "合并单元格从A列4行~C列4行"); // 1列4行输出
                //sheet.addCell(label);            //File fileImage = new File(System.getProperty("user.dir")+ "/logo.png");
                //WritableImage image = new WritableImage(0, 0, 2, 3, fileImage);
                // 从A1开始  跨2行3个单元格
                // 写入数据
                wwb.write();
                // 关闭文件
                wwb.close();
                long end = System.currentTimeMillis();
                System.out.println("完成该操作共用的时间是:" + (end - start));        } catch (Exception e) {
                System.out.println("-------------ERROR--------------");
                e.printStackTrace();
            }
        }
    }
    导出Excel文件:
    public class TestExcel1 {
    public static void main(String[] args) {
    Food food =  new Food();
    File  importExcel  = new File("C:\\Users\\Administrator\\Desktop\\菜品清单.xls");
    try{
    //操作Excel读出数据
    Workbook  workBook = Workbook.getWorkbook(importExcel);
    Sheet[] sheet = workBook.getSheets();
    int sheet_num = 0;
    if(sheet != null && sheet.length > 0){
    for(int sheetNum = 0;sheetNum < sheet.length;sheetNum++){
    sheet_num = sheet[sheetNum].getRows();//获取行数
    for(int rowNum = 1 ; rowNum < sheet_num ; rowNum ++ ){
    Cell[] cells = sheet[sheetNum].getRow(rowNum);
    food.setCode(cells[0].getContents());
    food.setName(cells[1].getContents());
    food.setEnName(cells[2].getContents());
    food.setCategary(cells[3].getContents());
    food.setPrice(Float.parseFloat(cells[4].getContents()));
    food.setVipPrice(Float.parseFloat(cells[5].getContents()));
    food.setUnit(cells[6].getContents());
    food.setSpell(cells[7].getContents());
    food.setIntro(cells[8].getContents());
    if(cells[9].getContents().equals("是")){
    food.setOutage(true);
    }else{
    food.setOutage(false);
    }
    if(cells[10].getContents().equals("是")){
    food.setWeigh(true);
    }else{
    food.setWeigh(false);
    }
    if(cells[11].getContents().equals("是")){
    food.setRecommend(true);
    }else{
    food.setRecommend(false);
    }
    food.setSpicy(SpicyType.valueof(cells[12].getContents().trim()));
    food.setId(Long.parseLong(cells[13].getContents()));
    food.setHotelInfoId(Integer.parseInt(cells[14].getContents()));

    System.out.println(food);
    }
    }
    }

    }catch (Exception e) {
    e.printStackTrace();
    }
    }}