2012-07-05 10 views
7
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(); 

    } 
    } 

He editado mi programa, y ​​ahora se dice que setString() El setString (String) no está definida para el tipo LabelCell leí la documentación, hay una setString método en el tipo LabelCell.escritura a un Excel archivo existente

+2

No existe una definición del método 'escribir' en la Clase de libro. Mira esto: http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html – Sabbath

+1

Aquí hay algunos códigos: http://www.vogella.com/articles/JavaExcel/article.html – Sabbath

+1

Pero este ejemplo lo tiene. [Enlace] (http://www.andykhan.com/jexcelapi/tutorial.html) para escribir en un archivo de Excel. –

Respuesta

14

LabelCell es simplemente una interfaz con un único método getString() es decir que se puede aprender más sobre él here

Debe utilizar jxl.write.Label lugar.
Lo que debe hacer exactamente es el siguiente
debe importar el archivo siguiente

import jxl.write.Label 

Luego siguiente es el código para añadir una celda en la ubicación deseada a un archivo de 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(); 

currentColumn y currentRow define el índice y el valor contiene la cadena que se colocará en esa celda.

Espero que ayude

+0

Funcionó para mí (Y) –

Cuestiones relacionadas