【问题标题】:Modifying existing excel using jxl使用 jxl 修改现有的 excel
【发布时间】:2010-08-31 03:56:56
【问题描述】:

我无法使用 jxl 编辑现有的 Excel 工作表。 它总是创造一个新的。 任何人都可以帮我解决它。 请给出一个小示例代码。

【问题讨论】:

    标签: java jxl


    【解决方案1】:

    jxl 旨在提高读取效率(因为这是 API 的主要用途)。为了提高性能,在读取电子表格时不会解释与输出信息相关的数据(例如字体等所有格式信息),因为在查询原始数据值时这是多余的。

    但是,如果我们需要修改此电子表格,则需要各种写入接口的句柄,可以使用复制方法获得。

    Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
    WritableWorkbook copy = Workbook.createWorkbook(new File("temp.xls"), workbook);
    

    这会复制已读入的信息,并执行附加处理以解释编写电子表格所需的字段。这种读取优化策略的缺点是我们在内存中保存了两个电子表格,而不仅仅是一个,因此内存需求增加了一倍。

    但是在这之后,你可以做任何你想做的事。喜欢:

    WritableSheet sheet2 = copy.getSheet(1); 
    WritableCell cell = sheet2.getWritableCell(1, 2); 
    
    if (cell.getType() == CellType.LABEL) 
    { 
      Label l = (Label) cell; 
      l.setString("modified cell"); 
    }
    copy.write(); 
    copy.close();
    workbook.close();
    

    注意:这是直接取自Andy Khan's tutorial page

    【讨论】:

    • 嗨@Lalli 我正在使用你的答案,但它不起作用我在这条线上遇到错误WritableWorkbook copy = Workbook.createWorkbook(new File("temp.xls"), workbook); ERROR is java.io.FileNotFoundException: temp.xls: open failed: EROFS (Read-only file system)
    【解决方案2】:

    我知道这是一个很老的问题,但如果有人会遇到同样的问题,那么要保留正确的格式(字体类型、颜色等) 您应该在将其转换为标签之前保存单元格格式,然后强制单元格为以前的格式。 代码:

    CellFormat cfm = cell.getCellFormat();
    Label l = (Label) cell; 
    l.setString("modified cell");
    cell.setCellFormat(cfm);
    

    【讨论】:

      【解决方案3】:
      //there is god example of it, you can copy in ur project and check it out, to 
      //understand how  it works
      
      Workbook wk = Workbook.getWorkbook(new File("ex.xls"));
      // 
      WritableWorkbook wkr = Workbook.createWorkbook(new File("modifed.xls"), wk);
      /* second line makes copy of wk excel file object /creates a readable spreadsheet.
      both are now similar and i can Modify exiting wkr spreadsheets */
      
      
      
       //next 2 line retrieve sheet number 0  and cell (1,1)
      
      
       WritableSheet getsht = wkr.getSheet(0);
       WritableCell getcl = getsht.getWritableCell(1, 1);
      
      
       //making own font 
      
      WritableFont ft = new WritableFont(WritableFont.ARIAL, 20 , WritableFont.BOLD, true , UnderlineStyle.SINGLE); 
      //making Format, which uses font
      WritableCellFormat   form   =   new WritableCellFormat( ft);
      
      Number nb = ( Number ) getcl ;          
      nb.setCellFormat( form );    
      
      
      wkr.write();
      wkr.close();
      

      【讨论】:

      • 虽然有时只用代码回答问题是可以的,但大多数时候通过添加解释来改进帖子。您可以编辑您的答案以包含一个。
      【解决方案4】:

      我个人使用此代码附加 xls 文件,如果不存在则创建一个。
      使用 jxl 2.6:

          public class Excel {
      
              private String fileName = "excel_file.xls";
              private String sheetName = "sheet1";
              private WritableWorkbook writableWorkbook;
              private int rowCount;
              private Workbook wb;
      
      // assigns checks if file exists or not, both cases we assign it to a WritableWorkbook // object so that we can write to it.
              private void assignWorkBook() throws IOException, BiffException {
          //        File f = new File(System.getProperty("user.dir") +"\\"+fileName);
                  File inp = new File(fileName);
                  try{
                      wb = Workbook.getWorkbook(inp);
                      writableWorkbook = Workbook.createWorkbook(inp, wb);
                  } catch (FileNotFoundException e){
                      writableWorkbook = Workbook.createWorkbook(inp); //Create a new one
                  }
              }
      
              public int getRowCount() {
                  return rowCount;
              }
      
      // this function writes a vector to an excel file, checks if there is already a sheet 
      // with that name or not, and uses it. then we have to close the Workbook object before 
      // we could write to the file, and then we save the file.
      // That is, the file is always saved after writing to it.
      
              public void writeRow(Vector<String> playerVector) throws WriteException, IOException, BiffException {
                  assignWorkBook();
                  WritableSheet excelSheet;
                  if(writableWorkbook.getNumberOfSheets() == 0) {
                      excelSheet = writableWorkbook.createSheet(sheetName, 0);
                  }
                  else {
                      excelSheet = writableWorkbook.getSheet(sheetName);
                  }
                  rowCount = excelSheet.getRows();
                  int colCount = 0;
                  for(String playerStat:playerVector) {
                      Label label = new Label(colCount++, rowCount, playerStat);
                      excelSheet.addCell(label);
                  }
                  if(wb != null) {
                      wb.close();
                  }
                  writableWorkbook.write();
                  writableWorkbook.close(); //everytime save it.
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-09
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多