Hi All,
About this subject, started yesterday...
I still continue having problem...
When class is running, file size is increasing while data are writing (I
supose) into this one.
At the end of the running class, I open the file (with MS Excel to see the
content) and the only thing i see the outuput generated by the method
createFileStructure().
After close this one, file size back to a minimum KB and no data exist !!!!
Have you ever had this problem ?
I join java code to have a look.
I really really aprecite help !
Thanks,
Carlos
Java code:
/*
*************************************************************************************
*/
import java.io.*;
import java.io.File;
import java.io.DataOutputStream;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.*;
public class myClass {
private FileOutputStream fileOut = null;
private HSSFWorkbook workBook = null;
private HSSFSheet sheet = null;
private String fileDirectory="C:\\MIGprojects\\tmp\\";
private static int fileNumber=1;
private final static int MAX_ROWS_EXCEL = 65536;
private static int currentNoOfExcelRows=0;
public myClass() {
try{
initialiseFile();
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method constructor. "+e);
}
}
public void main_method() {
try {
myClassIL obja = new myClassIL(); // this class connect to DB
myClassBLTbl[] myClassTblEntries = null;
boolean moreMyClassEntriesInDb = true;
while (moreMyClassEntriesInDb) {
myClassTblEntries = obja.getMyClasstblData(); // getMyClasstblData method
get all records from table
if (myClassTblEntries == null ||
myClassTblEntries.length <= 0) {
moreMyClassEntriesInDb = false;
break;
}
for (int i = 0; i < myClassTblEntries.length; i++) {
writeMyClassToExcelFile(myClassTblEntries[i]);
workBook.write(fileOut);
}
obja.deleteMyClasstbl(myClassTblEntries);
}
closeFile();
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method main_method. "+e);
}
}
private void initialiseFile() {
try {
String beginFileName = "my_file_";
String fileName=null;
boolean atLeastOneFileExistsOnFileSystem=false;
int tmpFileNo = 0;
closeFile();
while (new File(fileDirectory+beginFileName + (tmpFileNo+1) +
".xls").exists()) {
fileNumber = tmpFileNo+1;
tmpFileNo++;
atLeastOneFileExistsOnFileSystem=true;
}
fileName = beginFileName+fileNumber+".xls";
if(atLeastOneFileExistsOnFileSystem) {
POIFSFileSystem fs = new POIFSFileSystem(new
FileInputStream(fileDirectory+fileName));
workBook = new HSSFWorkbook(fs);
sheet = workBook.getSheetAt(0);
if(sheet.getLastRowNum() >= MAX_ROWS_EXCEL) {
fileNumber++;
fileName = beginFileName + fileNumber + ".xls";
workBook = new HSSFWorkbook();
sheet = workBook.createSheet("My Sheet");
fileOut = new FileOutputStream(fileDirectory+fileName);
createFileStructure();
workBook.write(fileOut);
}
else {
fileOut = new FileOutputStream(fileDirectory+fileName);
}
}
else {
workBook = new HSSFWorkbook();
sheet = workBook.createSheet("My Sheet");
fileOut = new FileOutputStream(fileDirectory+fileName);
createFileStructure();
workBook.write(fileOut);
}
currentNoOfExcelRows = sheet.getLastRowNum();
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method initialiseFile. "+e);
}
}
private void createFileStructure() {
try {
HSSFRow header = sheet.createRow( (short) 0);
HSSFCell c0 = header.createCell( (short) 0);
HSSFCell c1 = header.createCell( (short) 1);
HSSFCell c2 = header.createCell( (short) 2);
c0.setCellValue("table name");
c1.setCellValue("ID");
c2.setCellValue("name");
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method createFileStructure.
"+e);
}
}
private void writeMyClassToExcelFile(myClassTblBL inc) {
try {
currentNoOfExcelRows++;
if(currentNoOfExcelRows >= MAX_ROWS_EXCEL){ initialiseFile(); }
HSSFRow row = sheet.createRow( (short) currentNoOfExcelRows);
HSSFCell c0 = row.createCell( (short) 0);
HSSFCell c1 = row.createCell( (short) 1);
HSSFCell c2 = row.createCell( (short) 2);
c0.setCellValue("table_name");
c1.setCellValue(inc.getID());
c2.setCellValue(inc.getName());
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method
writeMyClassToExcelFile."+e);
}
}
private void closeFile(){
try{
if(fileOut !=null){
fileOut.close();
}
}
catch (Exception e) {
Trace.error("Exception thrown in class myClass, method closeFile. "+e);
}
}
}
/*
*************************************************************************************
*/