Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package qctomantis.tasks;
- import java.awt.Cursor;
- import java.awt.Toolkit;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import javax.swing.JLabel;
- import jxl.CellView;
- import jxl.read.biff.BiffException;
- import jxl.write.WriteException;
- import org.jdesktop.application.SingleFrameApplication;
- import org.jdesktop.application.Task;
- import jxl.Workbook;
- import jxl.WorkbookSettings;
- import jxl.format.Alignment;
- import jxl.format.Border;
- import jxl.format.BorderLineStyle;
- import jxl.format.Colour;
- import jxl.format.UnderlineStyle;
- import jxl.format.VerticalAlignment;
- import jxl.write.Label;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import qctomantis.QCtoMantisView;
- import qctomantis.utils.Defect;
- /**
- * Classe permettant de créer un fichier Excel à partir des données sélectionnées par l'utilisateur
- * @author rogoncalves
- */
- public class ExcelWriter extends Task<Void, Void>
- {
- private HashMap<Integer, Defect> defects;
- private String filepath;
- private QCtoMantisView view;
- public ExcelWriter(QCtoMantisView view, String filepath, HashMap<Integer, Defect> defects)
- {
- super(view.getApplication());
- this.view = view;
- this.filepath = filepath;
- this.defects = defects;
- view.getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
- }
- @Override
- protected synchronized Void doInBackground() throws IOException, WriteException, BiffException
- {
- // Logger.getLogger(ExcelWriter.class.getName()).info("Creating file");
- File test = new File(filepath);
- if(test.exists())
- {
- // Logger.getLogger(ExcelWriter.class.getName()).info("File already exists, overwriting it");
- test.delete();
- }
- WorkbookSettings ws = new WorkbookSettings();
- ws.setLocale(new Locale("fr", "FR"));
- FileOutputStream os = null;
- InputStream is = null;
- File f = null;
- try
- {
- is = ExcelWriter.class.getResource("test2.xls").openStream();
- f= File.createTempFile("import", ".xls");
- os = new FileOutputStream(f);
- byte[] temp = new byte[32768];
- int rc;
- while((rc = is.read(temp)) > 0)
- {
- os.write(temp, 0, rc);
- }
- is.close();
- os.close();
- }
- catch(IOException ex)
- {
- // Logger.getLogger(WordWriter.class.getName()).error("IO Exception");
- }
- Workbook model = Workbook.getWorkbook(f);
- System.out.println(filepath);
- WritableWorkbook workbook = Workbook.createWorkbook(new File(filepath), model, ws);
- WritableSheet sheet = workbook.getSheet(0);
- int cpt_row = 1155;
- int cpt_col = 3;
- int nb_cells = defects.size() * 35; //Nb lignes * 35 colonnes
- for(Defect d : defects.values())
- {
- Label lab;
- for(cpt_col = 3; cpt_col < 37; cpt_col++)
- {
- String val = cpt_col+""+cpt_row;
- lab = new Label(cpt_col, cpt_row, val);
- sheet.addCell(lab);
- int p = (int) Math.floor(100 * ((cpt_row - 1155)*35 + cpt_col-2) / nb_cells);
- System.out.println("progress="+p);
- setProgress(p);
- }
- cpt_row++;
- }
- workbook.write();
- workbook.close();
- return null;
- }
- @Override
- protected void succeeded(Void result)
- {
- // Logger.getLogger(ExcelWriter.class.getName()).info("Excel file creation successful");
- //On avertit l'utilisateur par un signal sonore
- Toolkit.getDefaultToolkit().beep();
- //On remet le curseur d'origine
- view.getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- File file = new File(filepath);
- view.doneExcelWriting(file);
- super.succeeded(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement