Apache POI only read the first row of a large Excel file - java

Can I read only the first row of an Excel file with Apache POI?
I don't want to read the whole file because it has 50,000 rows and takes up to 10 minutes to read (performance is a disaster). I am getting the bytes via file upload. My options are a byte array or an InputStream. Right now I am doing this:
Workbook workbook = new XSSFWorkbook(excelByteStream); //This line takes a lot of time, while debugging up to 10 minutes
Sheet firstSheet = workbook.getSheetAt(0);
DataFormatter df = new DataFormatter();
List<ColumnPanel> columnPanels = new ArrayList<>();
int i = 0;
for (Cell cell : firstSheet.getRow(0))
{
columnPanels.add(new ColumnPanel(df.formatCellValue(cell), i++));
}

InputStream is = new FileInputStream(new File("/path/to/workbook.xlsx"));
Workbook workbook = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(4096)
.open(is);
Sheet sheet = workBook.getSheetAt(0);
Row firstRow = sheet.rowIterator().next();
You can use this lib:
https://github.com/monitorjbl/excel-streaming-reader
Good guide:
Apache POI Streaming (SXSSF) for Reading

Related

How to write the List data in Excel

List having data in this format
[{Row ID=7565.0, Product ID=test11223, Postal Code=98103.0}, {Row ID=7567.0, Product ID=test11213, Postal Code=98101.0}] Having more than 100 row ID record like this, I want to store that data in Excel in below format
Row Id Order ID Postal Code
7565 test11233 98103
7567 test11233 98101
Please help to share the code to write a above list data in excel. thnx
With Apache POI you can write any kind of MS Office files.
Here is a quick example how write cells with it.
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}

How can two set of values written to an Excel file in TestNG which has been taken from the script iteslf? I am using apache poi for this purpose

I have been testing an application where i have to write a set of results to the excel file, the values are taken from the script. But the problem is that first I have to set the column headings like "Sl No" and "Name" after that I have to print the values against the particular cells. The thing is that am not able to write the results under the particular box.
I am new to apache poi. Can anyone help me with this issue.
Code for setting the excel and headings are shown below,
String FileName = "C://Users//Desktop//filename.xlsx";
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("Distributor");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Sl No");
Cell cell2 = row.createCell(1);
cell2.setCellValue("Name");
FileOutputStream outputStream = new FileOutputStream(new File(FileName));
workBook.write(outputStream);
outputStream.close();

How to read from formatted cells in Excel where numbers are stored as texts in Java using Apache POI

I need to read from formatted cells in Excel where numbers are stored as text in Java using Apache POI and extract the cells' contents as strings in their original format.
This works for me (with Apache POI 3.11):
FileInputStream fileInputStream = new FileInputStream("test.xlsx");
Workbook workBook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workBook.getSheetAt(0);
CellReference cellReference = new CellReference("A1");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
System.out.println(cell.getStringCellValue());

adding cells in excel with java apache

I am reading an already existing excel file and trying to add certain cells for example(C4-C15). I am having difficult manipulating the files through java. I am using apache poi, and would appreciate any help or direction.
In order to access a cell in Apache POI, you have to get a row first, and then get cell within chosen row. Below is example:
//Input file
InputStream inp = new FileInputStream("workbook.xls");
//Create workbook instance
Workbook wb = WorkbookFactory.create(inp);
//Create sheet instance
Sheet sheet = wb.getSheetAt(0);
for(int i = 3; i <= 16; ++i){ //Rows from 4 to 15 (Apache POI is zero based)
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); //Column "C"
//Do something with cell
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Source (more examples here)

Apache POI locks all cells by default. How to make all cells unlocked?

How can i make all cells in a sheet unlocked by default?
This snippet creates a sheet with all cells locked
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
cell.setCellValue("test");
This snippet causes some corrupt data when trying to open the excel file
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setLocked(false);
cell.setCellStyle(cellStyle);
cell.setCellValue("test");
This snippet doesn't cause any problems, but it also does unlock the cells. I can modify all the cells, but if i click on "format", the "Lock Cells" link is highlighted.
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
cell.setCellValue("test");
cell.getCellStyle().setLocked(false);
Your first snippet does not produce a document with locked cells using POI 3.9.
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("test");
FileOutputStream fileOut = new FileOutputStream("/tmp/test.xlsx");
wb.write(fileOut);
fileOut.close();
Cells are editable in the resulting workbook, as expected.
SXSSF is the streaming POI API. It's optimized for writing huge amounts of data to XML based spreadsheets (the normal XSSF api keeps XML docs in memory and can use huge amounts of memory when building large documents). If you don't need to output large XML based documents, I'd recommend just using the XSSF api as SXSSF has several restrictions and gotchas and isn't as well documented.
See also:
SXSSF Documentation
POI Quick Start

Categories

Resources