2008-09-29 35 views

Respuesta

8

Después de jugar un poco rato más por fin encontré algo que funcionó y vio que todavía no era una solución publicado aquí todavía, así que esto es lo que encontré:

try { 
    String fileName = "file.xls"; 
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName)); 
    workbook.createSheet("Sheet1", 0); 
    workbook.createSheet("Sheet2", 1); 
    workbook.createSheet("Sheet3", 2); 
    workbook.write(); 
    workbook.close(); 
} catch (WriteException e) { 

} 
+0

Por curiosidad, alguna razón particular por la que se adhiera a JXL? – kolrie

+0

Ya está en uso en nuestro proyecto bastante extensamente y no soy el líder del equipo: P – Aaron

+0

¡Lo tengo! :) He usado POI HSSF y es muy flexible. Si estuvieras comenzando algo de los terrenos arriba, diría que sigas ese camino. Pero como estás atrapado con JXL, buena suerte :) – kolrie

0

No estoy seguro si necesita quedarse con JXL, pero la mejor biblioteca para manejar archivos de Excel es Apache's POI HSSF.

Creo que hay muchos ejemplos en el sitio web que proporcioné, pero si necesita más ayuda, háganmelo saber. Puedo tener algunos ejemplos por ahí.

Solo por curiosidad, POI significa Interfaz de Ofuscación deficiente y HSSF es Horrible SpreadSheet Format. Ver la cantidad de Apache ama formatos de Microsoft Office :-)

2

Antes que nada debes poner Jxl Api en tu directorio java, descargar JXL api de http://www.andykhan.com/ extraerlo, copiar jxl y pegar como C: \ Program Files \ Java \ jre7 \ lib \ ext.

try { 
    String fileName = "file.xls"; 
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName)); 
    WritableSheet writablesheet1 = workbook.createSheet("Sheet1", 0); 
    WritableSheet writablesheet2 = workbook.createSheet("Sheet2", 1); 
    WritableSheet writablesheet3 = workbook.createSheet("Sheet3", 2); 
    Label label1 = new Label("Emp_Name"); 
    Label label2 = new Label("Emp_FName"); 
    Label label3 = new Label("Emp_Salary"); 
    writablesheet1.addCell(label1); 
    writablesheet2.addCell(label2); 
    writablesheet3.addCell(label3); 
    workbook.write(); 
    workbook.close(); 
} catch (WriteException e) { 

} 
2

Sé que es una pregunta muy antigua. Sin embargo, creo que puedo contribuir con un ejemplo que también añade los valores de celda:

/** 
* 
* @author Almir Campos 
*/ 
public class Write01 
{ 
    public void test01() throws IOException, WriteException 
    { 
     // Initial settings 
     File file = new File("c:/tmp/genexcel.xls"); 
     WorkbookSettings wbs = new WorkbookSettings(); 
     wbs.setLocale(new Locale("en", "EN")); 
     // Creates the workbook 
     WritableWorkbook wwb = Workbook.createWorkbook(file, wbs); 
     // Creates the sheet inside the workbook 
     wwb.createSheet("Report", 0); 
     // Makes the sheet writable 
     WritableSheet ws = wwb.getSheet(0); 
     // Creates a cell inside the sheet 
     //CellView cv = new CellView(); 
     Number n; 
     Label l; 
     Formula f; 
     for (int i = 0; i < 10; i++) 
     { 
      // A 
      n = new Number(0, i, i); 
      ws.addCell(n); 
      // B 
      l = new Label(1, i, "by"); 
      ws.addCell(l); 
      // C 
      n = new Number(2, i, i + 1); 
      ws.addCell(n); 
      // D 
      l = new Label(3, i, "is"); 
      ws.addCell(l); 
      // E 
      f = new Formula(4, i, "A" + (i+1) + "*C" + (i+1)); 
      ws.addCell(f); 
     } 
     wwb.write(); 
     wwb.close(); 
    } 
} 
-1
public void exportToExcel() { 
    final String fileName = "TodoList2.xls"; 

    //Saving file in external storage 
    File sdCard = Environment.getExternalStorageDirectory(); 
    File directory = new File(sdCard.getAbsolutePath() + "/javatechig.todo"); 

    //create directory if not exist 
    if(!directory.isDirectory()){ 
     directory.mkdirs(); 
    } 

    //file path 
    File file = new File(directory, fileName); 

    WorkbookSettings wbSettings = new WorkbookSettings(); 
    wbSettings.setLocale(new Locale("en", "EN")); 
    WritableWorkbook workbook; 


    try { 
     workbook = Workbook.createWorkbook(file, wbSettings); 
     //Excel sheet name. 0 represents first sheet 
     WritableSheet sheet = workbook.createSheet("MyShoppingList", 0); 



     Cursor cursor = mydb.rawQuery("select * from Contact", null); 

     try { 
      sheet.addCell(new Label(0, 0, "id")); // column and row 
      sheet.addCell(new Label(1, 0, "name")); 
      sheet.addCell(new Label(2,0,"ff ")); 
      sheet.addCell(new Label(3,0,"uu")); 
      if (cursor.moveToFirst()) { 
       do { 
        String title =cursor.getString(0) ; 
        String desc = cursor.getString(1); 
        String name=cursor.getString(2); 
        String family=cursor.getString(3); 

        int i = cursor.getPosition() + 1; 
        sheet.addCell(new Label(0, i, title)); 
        sheet.addCell(new Label(1, i, desc)); 
        sheet.addCell(new Label(2,i,name)); 
        sheet.addCell(new Label(3,i,family)); 
       } while (cursor.moveToNext()); 
      } 
      //closing cursor 
      cursor.close(); 
     } catch (RowsExceededException e) { 
      e.printStackTrace(); 
     } catch (WriteException e) { 
      e.printStackTrace(); 
     } 
     workbook.write(); 
     try { 
      workbook.close(); 
     } catch (WriteException e) { 
      e.printStackTrace(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
Cuestiones relacionadas