package tat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XLSFile {
public HSSFSheet sheet1;
private HSSFWorkbook wb;
private HSSFCellStyle styleRows, styleInt;
private HSSFFont fontRows, fontInt;
private final MyLog myLog;
private String fileName;
public int rowIndex;
public XLSFile(MyLog myLog, String fileName) {
this.myLog = myLog;
this.fileName = fileName;
this.defineSheet(fileName);
this.rowIndex = 1;
}
void defineSheet(String fileName){
String msg = "Popolo file " + fileName;
this.myLog.addText(msg);
this.wb = new HSSFWorkbook();
this.fontRows = this.wb.createFont();
this.fontRows.setFontName("Verdana");
this.fontInt = this.wb.createFont();
this.fontInt.setFontName("Verdana");
this.fontInt.setBoldweight((short)700);
// Fonts are set into a style so create a new one to use.
this.styleRows = this.wb.createCellStyle();
this.styleRows.setFont(fontRows);
this.styleInt = this.wb.createCellStyle();
this.styleInt.setFont(fontInt);
this.sheet1 = this.wb.createSheet("Riepilogo");
this.sheet1.setDefaultColumnWidth(12);
}
void closeSheet(){
// Write the output to a file
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(this.fileName);
} catch (FileNotFoundException e) {
this.myLog.errorException(e);
this.myLog.addText("Errore!!! Percorso immesso non valido\n");
}
try {
this.wb.write(fileOut);
} catch (IOException e) {
this.myLog.errorException(e);
this.myLog.addText("Errore!!! Percorso immesso non valido\n");
}
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
this.myLog.errorException(e);
}
}
public void header(String stringa, HSSFSheet sheet){
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
int cellIndex = 0;
StringTokenizer st = new StringTokenizer(stringa, ";");
while (st.hasMoreTokens()) {
HSSFCell cell = row.createCell(cellIndex);
cell.setCellValue(st.nextToken().trim());
cell.setCellStyle(styleInt);
cellIndex++;
}
}
public void newRow(String stringa, HSSFSheet sheet){
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(this.rowIndex);
int cellIndex = 0;
StringTokenizer st = new StringTokenizer(stringa, ";");
while (st.hasMoreTokens()) {
HSSFCell cell = row.createCell(cellIndex);
cell.setCellValue(st.nextToken().trim());
cell.setCellStyle(styleRows);
cellIndex++;
}
this.rowIndex++;
}
}