Export CLOB database field that exceeds 32767 char limit - java

I try to export content of table from a database to Excel.
In the table I have a column that holds imageURI data (that exceeds 32767 characters limit) or may hold line "Not Available" if the image is absent.
I do not need to export that image URI to Excel, just check if the image is present or not and export one of two lines for this particular column: "Present" (instead of the value of the cell) or "Not Available" (the value of the cell).
Here is the code that checks if the image is present or not (I'm using Apache POI to export data):
for (int i=1; i<=columnCount; i++) {
Cell dataCell = dataRow.createCell(i);
if(columnName.get(i).equals("IMAGE") && !result.getString(columnName.get(i)).equals("Not Available")){
dataCell.setCellValue("Present");
}
else{
dataCell.setCellValue(result.getString(columnName.get(i)));
}
}
The above construction gives me java.sql.SQLException.
How do I solve this?
Thanks.
UPD:
The following construction:
for (int i=1; i<=columnCount; i++) {
Cell dataCell = dataRow.createCell(i);
if(columnName.get(i).equals("IMAGE")){
dataCell.setCellValue("Present");
}
else{
dataCell.setCellValue(result.getString(columnName.get(i)));
}
}
works fine.

Related

Issue with reading blank cells in large xlsx files using apache POI/monitorjbl/excel-streaming-reader

I am working on requirement where I need to read large xlsx file contains more than one million records. The apache POI is not memory efficient when reading large files .Hence I am using below API which adds
https://github.com/monitorjbl/excel-streaming-reader which is a wrapper around that streaming API while preserving the syntax of the standard POI API. Everything working fine except reading blank cells in the row. The above API throwing null pointer if cell is blank
for(int i=0; i<=expectedColumns-1; i++) {
Cell cell = row.getCell(i);
switch (cell.getCellType()) {
}
}
java.lang.NullPointerException
at test.XLSXToCSVConverterStreamer.xlsx(XLSXToCSVConverterStreamer.java:67)
at test.XLSXToCSVConverterStreamer.main(XLSXToCSVConverterStreamer.java:164)
if a cell in row is null it is throwing null pointer at Switch case i.e cell.getCelltype. I have modified code to read null cells as blank cells but its not supporting
for(int i=0; i<=expectedColumns-1; i++) {
//Cell cell = row.getCell(i);
Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
switch (cell.getCellType()) {
}
}
if I use Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK) to read empty cells as blank I am getting below issue. Kindly help me in resolving this
com.monitorjbl.xlsx.exceptions.NotSupportedException
at com.monitorjbl.xlsx.impl.StreamingRow.getCell(StreamingRow.java:108)
Lot of methods are not supported by the streaming excel But It gives advantage of reading large excel files. You can read the blank cells from a row as follows (use Streaming Excel Reader v1.1.0)
boolean flag = false;
int lastcolno = row.getLastCellNum();
for (colno = 0; colno < lastcolno; colno++) {
colFlag = isColumnEmpty(row, colno);
if (flag == true)
break;
}
if (colFlag == true) {
System.out.println("In index row, column no: "
+ (colno + 1) + " is empty");
}
public static boolean isColumnEmpty(Row row, int colno) {
Cell c = row.getCell(colno);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK)
return true;
return false;
}

Apache POI : Update cells in a named range

I am using Apache POI library to read/write to xlsx. In my original xlsx, I have a namedrange "XYZ".
I want to update the values of cells within this name range. How can I do that?
I did this:
XSSFName name = workbook.getName("XYZ");
String formula = name.getRefersToFormula();
System.out.println("Formula = " + formula);
Now, I dont know how to get a handle to individual cell in this named range.
Can anyone please point me to the correct API that I can use?
Rgds
Sapan
There is an example from the Busy Developers' Guide for retrieving the cells in the range. Then you can use Cell.setCellValue() to update.
// setup code
String cname = "TestName";
Workbook wb = getMyWorkbook(); // retrieve workbook
// retrieve the named range
int namedCellIdx = wb.getNameIndex(cname);
Name aNamedCell = wb.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
Sheet s = wb.getSheet(crefs[i].getSheetName());
Row r = s.getRow(crefs[i].getRow());
Cell c = r.getCell(crefs[i].getCol());
// extract the cell contents based on cell type etc.
}

last row is not merged in ms word table using aspose word for java

i have created a table in ms word by aspose.word using node. Now i need table in which 2nd column will be merged except first row
I got the merge column, but first and last row is not merging. First row is okay, because it was coded for that. But I cannot find out why last row is not merged.
I have not enough reputation to attach image. So, I have to describe details of my output
Here is my code:
Document doc=new Document();
Table table = new Table(doc);
// Add the table to the document.
doc.getFirstSection().getBody().appendChild(table);//add table
Row row[]= new Row[4];Cell cell[] = new Cell[3];
for (int i = 0; i < row.length; i++) {
row[i]=new Row(doc);table.appendChild(row[i]);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);//if I set this here last row not //merge
for (int j = 0; j < cell.length; j++) {
cell[j]=new Cell(doc);
row[i].appendChild(cell[j].deepClone(false));//cell creation
// but in this position table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW) create //no problem
row[i].getLastCell().appendChild(new Paragraph(doc));
if(row[i]==table.getFirstRow())
{
}
else
{
if(j==1)
{
if(i==1)
{
row[i].getCells().get(j).getCellFormat().setVerticalMerge(CellMerge.FIRST);
}
else
{
row[i].getCells().get(j).getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
}
}
}
}
}
I am getting the following output by running your code. Kindly make sure that you are using the latest version of Aspose.Words for Java. If you are having same issue with latest version too, please share your environment details so that I can reproduce it at my side.
Download the latest version by visiting Aspose.Words for Java or directly download the Jar file from our Maven repository.
Disclosure: I am a developer at Aspose.

Excel formula not updating on row delete from java application using Apache POI

I'm using Apache POI in my application to write data to an excel file. I've an excel file template and a few formulas are also there in it. In my application, i use the excel template, write into it ,then delete unused rows and calculate formulas in the end. I'm using SUM formula in the file. The problem is when rows are deleted, the SUM formula is not updating,due to which error values are coming up in excel.
Example : the formula being used is : for cell B215 : SUM(B15:B214). in the application,after writing to the file i delete unused rows. now I've data till 70th row in the file.All other rows have been deleted. So my formula should get updated to : SUM(B15:B69) for cell B70. But in the file it's still showing the formula as SUM(B15:B214). Hence the value of that cell is "VALUE#
Code snippet :
File file = new File(path)
InputStream is = new FileInputStream(file)
POIFSFileSystem fs = new POIFSFileSystem(is)
HSSFWorkbook wb = new HSSFWorkbook(fs)
HSSFSheet excelSheet
int[] indexArray = populateSheet(excelSheet)
//indexArray is array with 3 values as startrow, lastrow, and first empty row.
removeBlankRows(excelSheet,indexArray)
//evaluate formula
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator()
for(HSSFRow r : excelSheet) {
for(HSSFCell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
String formula = c.getCellFormula();
evaluator.evaluateFormulaCell(c)
}
}
}
private void removeBlankRows(HSSFSheet sheet, int[] shiftInfo){
for(int i = shiftInfo[2]; i <= shiftInfo[1]; ++i) {
sheet.removeRow(sheet.getRow(i))
}
//Shift up the rows
def startRow = shiftInfo[1]+1
def endRow = sheet.getLastRowNum()
def rowCount = -1* (shiftInfo[1] - shiftInfo[2] + 1)
sheet.shiftRows(startRow, endRow, rowCount)
}
This is an Excel bug. I've dealt with this in the past by doing the following:
Label the sum cell BSUM
We need a stable range that won't be affected by inserts/deletes.
Add a formula to a safe (one that won't get deleted) cell, for this example D15: ="B15:B"&ROW(BSUM)-1
This will produce a stable range.
Use INDIRECT in the BSUM cell like so:
=SUM(INDIRECT(D15))

sheet.getLastRowNum() function gives invalid count with Apache POI 3.9 [duplicate]

This question already has answers here:
How to get row count in an Excel file using POI library?
(7 answers)
Closed 6 years ago.
I am using Apache POI 3.9 for xls and xlsx file processing.
As per the logic, I want to iterate over each row and want to collect data for further processing. For that I am using sheet.getLastRowNum() to retrieve number of rows in the xls sheet.
But it seems that sheet.getLastRowNum() gives wrong count if number of records are more than 10.
It works fine if total number of rows are 10. Otherwise it gives result deducted by one.
i.e If there are 15 row in the sheet then It gives 14 as Last Row number.
Can Anyone suggest me,How to solve this problem ??
Here is the code that i am using to collect the data ...
public static List<LinkedList<String>> populateExcelSheetContentWithHeader(Sheet sheet,int columnsCount,int rowsCount) throws Exception{
Row row = null;
List<LinkedList<String>> excelFileRecordsList = new LinkedList<LinkedList<String>>();
int emptyColCount = 0;
String freeTextData = null;
LinkedList<String> excelFileDataList =null;
int actualRows = sheet.getLastRowNum();
if (actualRows < rowsCount) {
rowsCount = actualRows;
}
///ITERATE OVER EACH ROW AND POPULATE THE DATA
for(int index=0;index<rowsCount;index++){
row = sheet.getRow(index);
if(row!=null){
emptyColCount = 0;
excelFileDataList =new LinkedList<String>();
for(int colIndex=0;colIndex<columnsCount;colIndex++){
freeTextData = "";
if(isEmptyCell(row.getCell(colIndex))){
emptyColCount++;
}else{
freeTextData = getCellValue(row.getCell(colIndex),false);
}
//ADD TEXT DETILS TO THE LIST
excelFileDataList.add(freeTextData);
}
//CHECK FOR END OF FILE
if(emptyColCount != columnsCount){
excelFileRecordsList.add(excelFileDataList);
}else{
break;
}
}
}
return excelFileRecordsList;
}
Any suggestion is appreciated .
it is possible that you are converting from CSV or you did any kind of modifications (copy or paste) inside your xls? I had the same exact behaviour when I did some changes like I mentioned. To fix it I had to copy the original again. Sorry for not providing a real solution, I just say what happened.
Maybe that helped

Categories