When I create excel sheet through java ,the column which has number datatype in the oracle table, get converted to text format in excel.I want it to remain in the number format.Below is my code snippet for excel creation.
FileWriter fw = new FileWriter(tempFile.getAbsoluteFile(),true);
//BufferedWriter bw = new BufferedWriter(fw);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
//Column Size of excel
for(int i=0;i<10;i++)
{
sheet.setColumnWidth((short) i, (short)8000);
}
String userSelectedValues=result;
HSSFCellStyle style = wb.createCellStyle();
///HSSFDataFormat df = wb.createDataFormat();
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//style.setDataFormat(df.getFormat("0"));
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight((short) 700);
style.setFont(font);
int selecteditems=userSelectedValues.split(",").length;
// HSSFRow rowhead = sheet.createRow((short)0);
//System.out.println("**************selecteditems************" +selecteditems);
for(int k=0; k<selecteditems;k++)
{
HSSFRow rowhead = sheet.createRow((short)k);
if(userSelectedValues.contains("O_UID"))
{
HSSFCell cell0 = rowhead.createCell((short) k);
cell0.setCellValue("O UID");
cell0.setCellStyle(style);
k=k+1;
}
///some columns here..
}
int index=1;
for (int i = 0; i<dataBeanList.size(); i++)
{
odb=(OppDataBean)dataBeanList.get(i);
HSSFRow row = sheet.createRow((short)index);
for(int j=0;j<selecteditems;j++)
{
if(userSelectedValues.contains("O_UID"))
{
HSSFCell row1=row.createCell((short)j);
row1.setCellType(row1.CELL_TYPE_NUMERIC);
row1.setCellValue(odb.getUID());
j=j+1;
}
}
index++;
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(path.toString()+"/temp.xls");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
wb.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this:
Cell cell = row.createCell(...);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html
In your example it will be:
Cell cell = row.createCell((short)j);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(odb.getUID());
Related
I have such a code that generates using the POI library. The list contains 250,000 lines. The formation of an Excel file takes 30-40 minutes. Reading about POI, I realized that this is not normal. How to speed up the process?
private void create(List<User> list) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Statistic");
var rowCount = 0;
for (int i = 0; i < list.size(); i++) {
sheet.autoSizeColumn(i);
XSSFRow row = sheet.createRow(++rowCount);
XSSFCell loginCell = row.createCell(0);
loginCell.setCellValue(list.get(i).getLogin());
//...
XSSFCell amountSpecCell = row.createCell(9);
amountSpecCell.setCellValue(list.get(i).getLevel());
}
try {
FileOutputStream fileOutputStream = new FileOutputStream("myfilenew.xlsx");
workbook.write(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
workbook.close();
}
}
I am using below code to copy couple of columns from one sheet to another in excel. But data is getting copied to new sheet but when i open that file and try to close it, it asks do you want to save it? What should i do.
FileInputStream fis;
try {
fis = new FileInputStream("C:\\Banks\\SBI.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet Sheet1 = wb.getSheet("new sheet");
XSSFSheet nfrntSheet = wb.createSheet("Original");
int n = Sheet1.getLastRowNum();
System.out.println(n);
for(int i = 0;i<n+11;i++)
{
XSSFRow r = nfrntSheet.createRow(i);
Cell c = r.createCell(0);
c.setCellFormula("'new sheet'!A"+(i-9));
nfrntSheet.autoSizeColumn(0);
}
for(int i = 0;i<n+11;i++)
{
XSSFRow r = nfrntSheet.getRow(i);
Cell c = r.createCell(1);
c.setCellFormula("'new sheet'!C"+(i-9));
nfrntSheet.autoSizeColumn(1);
}
for(int i = 0;i<=7;i++)
{
nfrntSheet.setColumnWidth(i, 5000);
}
nfrntSheet.setColumnWidth(1, 12000);
nfrntSheet.setColumnWidth(17, 30000);
fis.close();
FileOutputStream stream= new FileOutputStream(new File("C:\\Banks\\SBI.xlsx"));
wb.write(stream);
stream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(CommandLineApp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CommandLineApp.class.getName()).log(Level.SEVERE, null, ex);
}
I'm trying to retrieve the cached result of a formula cell in excel, the entire column is comprised of formula cells and i want to store the cached results of the columns' cells in an arraylist, but i get the error.
Apologies, i pasted the wrong code earlier, its fixed now.
public static void main(String[] args) throws Exception {
String filename = "C:/Users/L30902/Desktop/eclipse folder/FeaturesTest/student.xlsx";
FileInputStream fis = null;
int cellvalue = 0;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector<String> cellStoreVector = new Vector<String>();
while (cellIter.hasNext()) {
XSSFCell myCell = (XSSFCell) cellIter.next();
try {
cellvalue = myCell.getCachedFormulaResultType();
} catch (Exception e) {
}
cellStoreVector.addElement(Integer.toString(cellvalue));
}
String secondcolumnValue = null;
int i = 0;
secondcolumnValue = cellStoreVector.get(i).toString();
insertQuery(secondcolumnValue);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
// showExelData(sheetData);
}
private static void insertQuery(String secondcolumnvalue) {
System.out.println(secondcolumnvalue);
}
I should return values like 500, 33 but i only return 0, 0
public class Excel {
public static void main(String[] args) throws IOException, FileNotFoundException {
try {
InputStream input = new BufferedInputStream(new FileInputStream("D:/one"));
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow next = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = next.cellIterator();
while (cells.hasNext()) {
HSSFCell next2 = (HSSFCell) cells.next();
if (HSSFCell.CELL_TYPE_NUMERIC == next2.getCellType()) {
System.out.println(next2.getNumericCellValue() + "");
} else if (HSSFCell.CELL_TYPE_STRING == next2.getCellType()) {
System.out.println(next2.getStringCellValue());
} else if (HSSFCell.CELL_TYPE_BOOLEAN == next2.getCellType()) {
System.out.println(next2.getBooleanCellValue() + "");
} else if (HSSFCell.CELL_TYPE_BLANK == next2.getCellType()) {
System.out.println("BLANK ");
} else {
System.out.println("unknown cell type");
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
you have not given the file extension in your code for your file "D:/one" is it an xls ot xlsx or csv .
This line:
InputStream input = new BufferedInputStream(new FileInputStream("D:/one"));
...should be something like:
InputStream input = new BufferedInputStream(new FileInputStream("D:/folder/filename.xls"));
...depending on your file location and extension of course.
As an aside, I highly recommend JExcelAPI and this tutorial by Lars Vogel.
Here is the code that generates .xls file using JXL:
public void generateXls(String fileName, int sheets, int cols, int rows) {
if (cols > 256) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
}
if (rows > 65536) {
throw new IllegalArgumentException("Error: number of rows per sheet must be < 65536");
}
String fullName = fileName + ".xls";
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(new File(fullName));
} catch (IOException e) {
e.printStackTrace();
}
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
WritableSheet sheet = workbook.createSheet("Sheet" + s, 0);
for (int i = 0; i < cols ; i++) {
for (int j = 0; j < rows; j++) {
Number number = new Number(i, j, random.nextDouble()*1000);
try {
sheet.addCell(number);
} catch (RowsExceededException e) {
throw new RuntimeException("Error: too many rows in a sheet");
} catch (WriteException e) {
throw new RuntimeException("Error occured while adding cell to sheet", e);
}
}
}
}
try {
workbook.write();
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the code that generates .xls and .xlsx files using POI:
public void generateXlsx(String fileName, int sheets, int cols, int rows) {
if (cols > 16383) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 16383");
}
XSSFWorkbook workbook = new XSSFWorkbook();
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
XSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < rows ; i++) {
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < cols; j++) {
XSSFCell cell = row.createCell(j);
cell.setCellValue(random.nextDouble()*1000);
}
}
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void generateXls(String fileName, int sheets, int cols, int rows) {
if (cols > 256) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
}
HSSFWorkbook workbook = new HSSFWorkbook();
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
HSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < rows ; i++) {
HSSFRow row = sheet.createRow(i);
for (int j = 0; j < cols; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(random.nextDouble()*1000);
}
}
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I wrote a performance test:
public static void main(String[] args) {
int sheets =1;
int cols = 255;
int rows = 20000;
long a1 = System.currentTimeMillis();
ExcelFileGenerator generator = new ExcelFileGenerator();
generator.generateXls("xlsJXL.xls", sheets, cols, rows);
long xls = System.currentTimeMillis()-a1;
System.out.println("xlsJXL: " + xls);
ExcelFileGeneratorPOI generatorPOI = new ExcelFileGeneratorPOI();
long a2 = System.currentTimeMillis();
generatorPOI.generateXls("xlsPOI.xls", sheets, cols, rows);
long xlsPoi = System.currentTimeMillis()-a2;
System.out.println("xlsPOI: " + xlsPoi);
long a3 = System.currentTimeMillis();
generatorPOI.generateXlsx("xlsxPOI.xlsx", sheets, cols, rows);
long xlsx = System.currentTimeMillis()-a3;
System.out.println("xlsxPOI: " + xlsx);
}
The results are:
sheets =1
cols = 255
rows = 10
xlsJXL: 133
xlsPOI: 162
xlsxPOI: 645
sheets =1
cols = 10
rows = 255
xlsJXL: 130
xlsPOI: 140
xlsxPOI: 650
sheets =10
cols = 255
rows = 255
xlsJXL: 611
xlsPOI: 784
xlsxPOI: 16228
sheets =2
cols = 100
rows = 10000
xlsJXL: 2755
xlsPOI: 3270
xlsxPOI: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
any reason by creating .xlsx with POI is much slower than creating .xls?
xls is a binary based format, xlsx is an XML based format and requires more work to read/write.
xlsx might also require an in memory document model to parse/build the XML which could be more complex.
Finally, xls could be optimised better because it has been supported longer.