How to speed up the formation of excel file? - java

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();
}
}

Related

Data not getting saved after copying data from one sheet to another using java

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);
}

Unable to write to an excel (Selenium WebDriver) - Apache POI

I am new to Selenium Web automation please be soft on me.
I have created a method to write an array content to an excel sheet.
I get no exception or error and I don't see data being written to excel sheet.
Name of excel sheet-mysheet.xlsx
Name of sheet within excel workbook:"FirstLevelMenu"
public class WriteExcelData {
XSSFWorkbook wb;
XSSFSheet sheet;
public void writeData(String path, String sheetName, String[] data) {
try {
File src = new File(path);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
Row row = sheet.getRow(0);
Row newRow = sheet.createRow(rowCount + 1);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell col = newRow.createCell(j);
col.setCellValue(data[j]);
}
fis.close();
FileOutputStream fout = new FileOutputStream(src);
wb.write(fout);
fout.close();
} catch (Exception e) {
e.getMessage();
}
}
public static void main(String[] args) {
WriteExcelData test=new WriteExcelData();
String[] data=new String[2];
data[0]="cat";
data[1]="cat";
test.writeData("C:\\mysheet.xlsx", "FirstLevelMenu", data);
}
}
As you are using a fresh xlsx sheet to write, please try below code...I am sure it will work :)
public static void main(String[] args) throws InvalidFormatException, IOException{
FileInputStream fis=new FileInputStream("D://Data.xlsx");
XSSFWorkbook wb= new XSSFWorkbook(fis);
//XSSFSheet sh= wb.getSheetAt(0); Or
XSSFSheet sh = wb.createSheet("Test");
XSSFRow row=sh.createRow(0);
XSSFCell cell= row.createCell(0);
//cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue("Ish Mishra");
FileOutputStream fos=new FileOutputStream("D:\\Data.xlsx");
wb.write(fos);
fos.close();
System.out.println("Excel File Written successfully");

How to recover formula cell cache result from excel file

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

I have written a program in java to read data from MS excel sheet but it is showing that (The system cannot find the file specified)

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.

Converting text into numeric in xls using Java

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());

Categories