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();

    }
   }

I have edited my program, and now it says that setString() The method setString(String) is undefined for the type LabelCell I read the Documentation, there is a method setString in the LabelCell type.

Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
Nimit_ZZ
  • 495
  • 4
  • 10
  • 21
  • 2
    There is no definition of method 'write' in the Workbook Class . Watch this : http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html – Sabbath Jul 05 '12 at 05:51
  • 1
    Here are some codes : http://www.vogella.com/articles/JavaExcel/article.html – Sabbath Jul 05 '12 at 05:54
  • 1
    But this example has it. [Link](http://www.andykhan.com/jexcelapi/tutorial.html) for writing into an excel file. – Nimit_ZZ Jul 05 '12 at 05:57
  • You're calling a write() method with Workbook instance wk while the example is using WritableWorkbook instance ... – Sabbath Jul 05 '12 at 06:03
  • Perhaps you should read the document or web more carefully ... – Sabbath Jul 05 '12 at 06:05
  • You should probably have a look at this: [http://stackoverflow.com/questions/3605923/modifying-existing-excel-using-jxl?rq=1](http://stackoverflow.com/questions/3605923/modifying-existing-excel-using-jxl?rq=1) – Nimit_ZZ Jul 05 '12 at 06:21
  • Where is the method setString? which documentation you went through? http://www.jarvana.com/jarvana/view/net/sourceforge/jexcelapi/jxl/2.6.12/jxl-2.6.12-javadoc.jar!/jxl/LabelCell.html gothrought the above doc – pavi Jul 05 '12 at 06:47
  • Since you have edited your question , there is nothing I can answer now ... – Sabbath Jul 05 '12 at 06:56

1 Answers1

15

LabelCell is just an interface with only one method i.e getString() you can learn more about it here

You should use jxl.write.Label instead.
What you should exactly do is as follows
You should import the following file

import jxl.write.Label

Then following is the code for adding a cell at desired location to an excel file

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 and currentRow define the index and value contains the String to be placed in that cell.

Hope it helps

moCap
  • 969
  • 1
  • 14
  • 31