Problem:
As i write a big project using smartXLS library (Java + Excel) becuse i dont know that its not free, so, i have a problem with rebuilding my application with Apache POI which is quite different. I have decided a good idea - to create my own WorkBook class, which i will use like a bridge, or interface. This class will have method names like SmartXLS and will "translate" this methods to such in Apache POI. Thanks to Java Patterns which help us to think more abstract :) . So i dont want to create a full SmartXLS implementation under POI, i need just several functions. Here my code, so if you want, you can modify it and use under GPL liscense :) .
As i write a big project using smartXLS library (Java + Excel) becuse i dont know that its not free, so, i have a problem with rebuilding my application with Apache POI which is quite different. I have decided a good idea - to create my own WorkBook class, which i will use like a bridge, or interface. This class will have method names like SmartXLS and will "translate" this methods to such in Apache POI. Thanks to Java Patterns which help us to think more abstract :) . So i dont want to create a full SmartXLS implementation under POI, i need just several functions. Here my code, so if you want, you can modify it and use under GPL liscense :) .
/**
*
* The Bridge between SmartXLS and Apache POI
* @author Khalilov Vusal aka Maxima aka Rendorf
*/
import java.io.*;
import org.apache.poi.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WorkBook {
Workbook wb;
Sheet sheet;
Row row;
Cell cell;
public WorkBook()
{
}
public void readXLSX(String filename)
{
try {
InputStream inp = new FileInputStream(filename);
wb = WorkbookFactory.create(inp);
} catch (Exception e) {
System.out.println("Exception in WorkBook readXLSX. "+e);
}
}
public void setSheet(int number)
{
sheet = wb.getSheetAt(number);
}
public double getNumber(int x,int y)
{
if (sheet==null)
{
sheet = wb.getSheetAt(0);
}
row = sheet.getRow(x);
cell = row.getCell(y);
return cell.getNumericCellValue();
}
public String getText(int x,int y)
{
if (sheet==null)
{
sheet = wb.getSheetAt(0);
}
row = sheet.getRow(x);
cell = row.getCell(y);
if (cell.getCellType()==1)
{
System.out.println(cell.getStringCellValue());
return cell.getStringCellValue();
}
else
{
System.out.println(cell.getNumericCellValue()+"");
return cell.getNumericCellValue()+"";
}
}
public void setNumber(int x,int y,double num)
{
if (sheet==null)
{
sheet = wb.getSheetAt(0);
}
row = sheet.getRow(x);
cell = row.getCell(y);
cell.setCellValue(num);
}
public void setText(int x,int y,String text)
{
if (sheet==null)
{
sheet = wb.getSheetAt(0);
}
row = sheet.getRow(x);
cell = row.getCell(y);
cell.setCellValue(text);
}
public String getCellValue(int x,int y)
{
if (sheet==null)
{
sheet = wb.getSheetAt(0);
}
row = sheet.getRow(x);
cell = row.getCell(y);
return cell.getCellType()+"-";
}
public void writeXLSX(String filename)
{
try {
FileOutputStream fos1 = new FileOutputStream(filename);
wb.write(fos1);
fos1.close();
} catch (Exception e) {
System.out.println("Exception in writeXLS(). "+e);
}
}
public static void main(String args[])
{
WorkBook w = new WorkBook();
w.readXLSX("e:\\database1.xlsx");
w.setSheet(0);
w.writeXLSX("e:\\.xlsx");
}
}
I've faced the same problem, and you saved my day <3
ОтветитьУдалить