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)
Related
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
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();
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());
I'm developing an app in java using POI, which writes data into excell sheet.
I want to write a new row when I have a new data (in order to assist the user to follow the data).
I dont want to close and reopen the excell file each time I have a new data to write.
The initilize code is:
FileOutputStream fileOut = new FileOutputStream(new File (excellFileName));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
rowNum = 0;
HSSFRow rowhead = sheet.createRow((short)rowNum);
createRowColumns(rowhead, rowNum);
workbook.write(fileOut);
rowNum++;
Each time I have a new data, I'm using this code:
HSSFRow rowMsg = sheet.createRow((short)rowNum);
createRowColumns(rowMsg, rowNum);
workbook.write(fileOut);
rowNum++;
(createRowColumns mehtod sets the data (in seperate cells in a new: rowMsg)
The problem is that I cant see any rows in the excell file, except the first row (rowhead , row #0).
What Am I missing ?
(Pay attention that I dont want to close and reopen the file each time I have data to write)
Thanks
To write all rows you have to write to worksheet only once. So, you need to update your code as follows.
for(int index = 0; index < rowNum; rowNum++) {
HSSFRow rowMsg = sheet.createRow((short)rowNum);
createRowColumns(rowMsg, rowNum);
}
workbook.write(fileOut);
I have a situation where I will be reading multiple lines and after some logic I need to write the lines in an Excel Sheet. I am using Apache POI for this purpose. However, the problem that I am facing is that, only the last line (from the loop) is being written to the Excel
Can someone please help me on this or provide some code snippet?
Thanks
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn((short)2);
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
Using newlines in cells