.xls读写

 

jxl.jar

.xls读写

仅支持.xls ,不支持.xlsx

//读
//文件路径 , sheet, 列 ,行
public static String getExcelContents(String path ,int st ,int i,int j){
  String str="";    
  try {    
    Workbook book= Workbook.getWorkbook(new File(path)); 
    Sheet sheet=book.getSheet(st);
    str=sheet.getCell(i, j).getContents();
    book.close(); 
  }    
  catch(Exception e) { 
  }
  return str;
}

//写(追加)

public static void setExcelCell (String path,int st, int i ,int j ,String str) {

  File file = new File(path);
  try {
    if (!file.exists()) { // 判断文件是否已存在
    System.out.println("文件不存在");
  } else {
    Workbook wb = Workbook.getWorkbook(new File(path));
    UUID uuid =UUID.randomUUID();
    File tempfile = new File(System.getProperty("user.dir")+ "\\"+uuid+".xls");
    WritableWorkbook wwb = Workbook.createWorkbook(tempfile, wb);
    WritableSheet ws = wwb.getSheet(st);

    //字体样式
    WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"),10, WritableFont.NO_BOLD);
    //格子样式
    WritableCellFormat cf = new WritableCellFormat(font);
    cf.setBackground(Colour.RED);

    // 样式不是必选参数    
    Label label = new Label(i,j,str,cf);
    ws.addCell(label);
    wwb.write();
    wwb.close();
    wb.close();
    file.delete();
    tempfile.renameTo(file);    
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

 

posted @ 2018-03-02 15:01  AsapRun  阅读(117)  评论(0)    收藏  举报