Advertisement
Guest User

Untitled

a guest
Jun 7th, 2011
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package qctomantis.tasks;
  6.  
  7. import java.awt.Cursor;
  8. import java.awt.Toolkit;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.LinkedHashMap;
  16. import java.util.List;
  17. import java.util.Locale;
  18. import java.util.Map;
  19. import javax.swing.JLabel;
  20. import jxl.CellView;
  21. import jxl.read.biff.BiffException;
  22. import jxl.write.WriteException;
  23.  
  24. import org.jdesktop.application.SingleFrameApplication;
  25. import org.jdesktop.application.Task;
  26. import jxl.Workbook;
  27. import jxl.WorkbookSettings;
  28. import jxl.format.Alignment;
  29. import jxl.format.Border;
  30. import jxl.format.BorderLineStyle;
  31. import jxl.format.Colour;
  32. import jxl.format.UnderlineStyle;
  33. import jxl.format.VerticalAlignment;
  34. import jxl.write.Label;
  35. import jxl.write.WritableCellFormat;
  36. import jxl.write.WritableFont;
  37. import jxl.write.WritableSheet;
  38. import jxl.write.WritableWorkbook;
  39. import qctomantis.QCtoMantisView;
  40. import qctomantis.utils.Defect;
  41.  
  42. /**
  43.  * Classe permettant de créer un fichier Excel à partir des données sélectionnées par l'utilisateur
  44.  * @author rogoncalves
  45.  */
  46. public class ExcelWriter extends Task<Void, Void>
  47. {
  48.     private HashMap<Integer, Defect> defects;
  49.     private String filepath;
  50.     private QCtoMantisView view;
  51.  
  52.     public ExcelWriter(QCtoMantisView view, String filepath, HashMap<Integer, Defect> defects)
  53.     {
  54.         super(view.getApplication());
  55.         this.view = view;
  56.         this.filepath = filepath;
  57.         this.defects = defects;
  58.         view.getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  59.     }
  60.  
  61.     @Override
  62.     protected synchronized Void doInBackground() throws IOException, WriteException, BiffException
  63.     {
  64.  
  65. //        Logger.getLogger(ExcelWriter.class.getName()).info("Creating file");
  66.         File test = new File(filepath);
  67.  
  68.         if(test.exists())
  69.         {
  70. //            Logger.getLogger(ExcelWriter.class.getName()).info("File already exists, overwriting it");
  71.             test.delete();
  72.         }
  73.  
  74.         WorkbookSettings ws = new WorkbookSettings();
  75.         ws.setLocale(new Locale("fr", "FR"));
  76.  
  77.  
  78.         FileOutputStream os = null;
  79.         InputStream is = null;
  80.         File f = null;
  81.         try
  82.         {
  83.             is = ExcelWriter.class.getResource("test2.xls").openStream();
  84.  
  85.             f= File.createTempFile("import", ".xls");
  86.             os = new FileOutputStream(f);
  87.             byte[] temp = new byte[32768];
  88.             int rc;
  89.             while((rc = is.read(temp)) > 0)
  90.             {
  91.                 os.write(temp, 0, rc);
  92.             }
  93.  
  94.             is.close();
  95.             os.close();
  96.         }
  97.         catch(IOException ex)
  98.         {
  99. //            Logger.getLogger(WordWriter.class.getName()).error("IO Exception");
  100.         }
  101.         Workbook model = Workbook.getWorkbook(f);
  102.         System.out.println(filepath);
  103.         WritableWorkbook workbook = Workbook.createWorkbook(new File(filepath), model, ws);
  104.  
  105.         WritableSheet sheet = workbook.getSheet(0);
  106.  
  107.         int cpt_row = 1155;
  108.         int cpt_col = 3;
  109.         int nb_cells = defects.size() * 35; //Nb lignes * 35 colonnes
  110.  
  111.         for(Defect d : defects.values())
  112.         {
  113.  
  114.             Label lab;
  115.             for(cpt_col = 3; cpt_col < 37; cpt_col++)
  116.             {
  117.                 String val = cpt_col+""+cpt_row;
  118.                
  119.                 lab = new Label(cpt_col, cpt_row, val);
  120.                 sheet.addCell(lab);
  121.                 int p = (int) Math.floor(100 * ((cpt_row - 1155)*35 + cpt_col-2) / nb_cells);
  122.                 System.out.println("progress="+p);
  123.                 setProgress(p);
  124.             }
  125.             cpt_row++;
  126.         }
  127.  
  128.         workbook.write();
  129.         workbook.close();
  130.  
  131.         return null;
  132.     }
  133.  
  134.  
  135.     @Override
  136.     protected void succeeded(Void result)
  137.     {
  138. //        Logger.getLogger(ExcelWriter.class.getName()).info("Excel file creation successful");
  139.         //On avertit l'utilisateur par un signal sonore
  140.         Toolkit.getDefaultToolkit().beep();
  141.         //On remet le curseur d'origine
  142.         view.getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  143.         File file = new File(filepath);
  144.         view.doneExcelWriting(file);
  145.         super.succeeded(result);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement