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

    } 
    } 

Ho modificato il mio programma, e ora si dice che setString() Il metodo setString (String) non è definito per il tipo labelCell ho letto la documentazione, v'è un metodo setString nel tipo LabelCell.scrittura ad un Excel file esistente

+2

Non v'è alcuna definizione del metodo di 'write' nella classe cartella di lavoro. Guarda questo: http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html – Sabbath

+1

Ecco alcuni codici: http://www.vogella.com/articles/JavaExcel/article.html – Sabbath

+1

Ma questo esempio ce l'ha. [Link] (http://www.andykhan.com/jexcelapi/tutorial.html) per scrivere in un file excel. –

risposta

14

labelCell è solo un'interfaccia con un solo metodo cioè getString() si può imparare di più su di esso here

si dovrebbe usare jxl.write.Label invece.
Che cosa si dovrebbe esattamente fare è la seguente
Si dovrebbe importare il file seguente

import jxl.write.Label 

Poi seguito è riportato il codice per l'aggiunta di una cella alla posizione desiderata in un file 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 e currentRow definisce l'indice e il valore contiene la stringa da inserire in quella cella.

Speranza che aiuta

+0

Ha lavorato per me (Y) –

Problemi correlati