写入现有Excel文件(Writing to an Existing Excel File)

2019-06-26 01:59发布

 package jexcel.jxl.nimit;

    import java.awt.Label;  
    import java.io.File;  
    import java.io.IOException;

    import jxl.Cell;   
    import jxl.CellType;  
    import jxl.LabelCell;  
    import jxl.NumberCell;  
    import jxl.Sheet;  
    import jxl.Workbook;  
    import jxl.read.biff.BiffException;  
    import jxl.write.WritableCell;  
    import jxl.write.WritableSheet;  
    import jxl.write.WritableWorkbook;  
    import jxl.write.WriteException;  
    import jxl.write.biff.RowsExceededException;  

    public class ExcelJxl {

    /**
     * @param args
     * @throws IOException 
     * @throws BiffException 
     * @throws WriteException 
     * @throws RowsExceededException 
     */
    public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
        // TODO Auto-generated method stub
             ExcelJxl.WriteFile("D:\nimit.xls");
    }

    public static void WriteFile(String path) throws BiffException, IOException, RowsExceededException, WriteException{

    Workbook wb=Workbook.getWorkbook(new File(path));

    WritableWorkbook copy=Workbook.createWorkbook(new File("D:\temp.xls"),wb);
    WritableSheet sheet = copy.getSheet(1); 
    WritableCell cell = sheet.getWritableCell(0,0); 
    String S="nimit";
    if (cell.getType() == CellType.LABEL) 
    { 
      LabelCell l = (LabelCell) cell; 
      l.setString(S); 
    }
    copy.write(); 
    copy.close();
    wb.close();

    }
   }

我已经编辑我的计划,现在说了setString() 方法了setString(字符串)是未定义LabelCell我读的文档类型 ,有在LabelCell类型的方法了setString。

Answer 1:

LabelCell仅仅是一个只有一个方法,即一个接口getString()你可以了解更多有关它在这里

您应该使用jxl.write.Label代替。
你应该做的究竟是如下
您应该导入以下文件

import jxl.write.Label

然后下面是在期望的位置中添加细胞到Excel文件中的代码

Workbook existingWorkbook = Workbook.getWorkbook(new File(fileToEdit.getAbsolutePath()));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(sheetName);
WritableCell cell;
Label l = new Label(currentColumn, currentRow, value);
cell = (WritableCell) l;
sheetToEdit.addCell(cell);
 workbookCopy.write();
 workbookCopy.close();
 existingWorkbook.close();

currentColumncurrentRow定义索引和值包含被放置在该电池中的字符串。

希望能帮助到你



文章来源: Writing to an Existing Excel File
标签: java jxl jxls