lundi 26 septembre 2016

How to test a writesheet by POI with JUnit

I need to create a procedure to test in different way (JUnit in Eclipse), the following code, which makes a Excel file by POI. I thought that the production of the file in Excel was proof that everything was working properly, but it doesn't.

Thank you for your help. Cheers.

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Writesheet 
{
   public static void main(String[] args) throws Exception 
   {
      @SuppressWarnings("resource")
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet( " Employee Info ");
      XSSFRow row;
      Map < String, Object[] > empinfo = new TreeMap < String, Object[] >();
      empinfo.put( "1", new Object[] {"EMP ID", "EMP NAME", "DESIGNATION" });
      empinfo.put( "2", new Object[] {"tp01", "Gopal", "Technical Manager" });
      empinfo.put( "3", new Object[] {"tp02", "Manisha", "Proof Reader" });
      empinfo.put( "4", new Object[] {"tp03", "Masthan", "Technical Writer" });
      empinfo.put( "5", new Object[] {"tp04", "Satish", "Technical Writer" });
      empinfo.put( "6", new Object[] {"tp05", "Krishna", "Technical Writer" });
      Set < String > keyid = empinfo.keySet();
      int rowid = 0;
      for (String key : keyid)
      {
         row = spreadsheet.createRow(rowid++);
         Object [] objectArr = empinfo.get(key);
         int cellid = 0;
         for (Object obj : objectArr)
         {
            Cell cell = row.createCell(cellid++);
            cell.setCellValue((String)obj);
         }
      }
      FileOutputStream out = new FileOutputStream( 
      new File("Writesheet.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println( 
      "Writesheet.xlsx written successfully" );
   }
}

Aucun commentaire:

Enregistrer un commentaire