Increase the maximum length of HSSFCell in java - java

Actually I tried to store some data in HSSFCell using java but i got an error like
java.lang.IllegalArgumentException: The maximum length of cell contents (text) i
s 32,767 characters
at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:559
)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:533
)
at application.ExtractUI.datatoexcel(ExtractUI.java:272)
at application.ExtractUI$3.getData(ExtractUI.java:208)
at application.ExtractUI$3.handle(ExtractUI.java:198)
at application.ExtractUI$3.handle(ExtractUI.java:1)
can anyone suggest me a method to increase the cell length ie more than 32767 characters???
I used the following code for which I got the above error
public void datatoexcel(ResultSet rs) {
try {
int iter = 0;
ResultSetMetaData rmeta = rs.getMetaData();
int col = rmeta.getColumnCount();
HSSFWorkbook workbook = new HSSFWorkbook();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy HHmmss");
String pa = pth + "\\" + sdf.format(date) + ".xlsx";
System.out.println(pa);
FileOutputStream out = new FileOutputStream(new File(pa));
HSSFSheet sheet = workbook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
// Font style for headers
HSSFFont boldFont = workbook.createFont();
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
boldFont.setColor(HSSFFont.COLOR_RED);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(boldFont);
while (rs.next()) {
// limit the data to 1000 anad create a new sheet
if (iter == 1000) {
sheet = workbook.createSheet();
iter = 0;
}
// Adding header to the first row
if (iter == 0) {
myRow = sheet.createRow(iter);
for (int k = 1, j = 0; k <= col && j < col; k++) {
myCell = myRow.createCell( j);
myCell.setCellValue(rmeta.getColumnName(k));
// set style to font
myCell.setCellStyle(cellStyle);
j++;
}
iter++;
}
// Adding data from 2nd Row
myRow = sheet.createRow(iter);
for (int k = 1, j = 0; k <= col && j < col; k++) {
myRow.createCell( j).setCellValue(
rs.getString(rmeta.getColumnName(k)));
j++;
}
iter++;
}
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
any suggestions??

Your only option is to switch file formats. There's a hard limit in both the .xls and .xlsx file formats of 32,767 characters. Apache POI is simply enforcing the file format + Excel limit. You can see details of those limits in the Microsoft documentation, and also captured nicely in this Apache POI javadoc page
If you really need text that long, you'll need to switch to another file format such as CSV

Or you can truncate your string length to the specified maximum length minus one

private void writeIssueDataForEachRow(Issue issue, Row row, CellStyle style,
List<ColumnIndex> customFieldDefinitions) {
Cell cell = row.createCell(0);
cell.setCellValue(issue.getId()); // 編號
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(issue.getSubject()); // 主旨
cell.setCellStyle(style);
// substring 的原因是要避開 The maximum length of cell contents (text) is 32,767 characters
cell = row.createCell(2);
cell.setCellValue(StringUtils.substring(issue.getDescription(), 0, 32767)); // 敘述
cell.setCellStyle(style);
}

Related

POI XSSF overwriting specific cells in excel file

I am trying to write out to an existing excel file. I don't want to create new rows or cells, I just want to write out the value from my array into the value at row x column y. Every time I have tried this so far I can only get it to work if I create a new row. Please help!!!
Integer columns = DataImport.columns_in_sheet[0];
Integer rowNum = learnerRow + 2;
try {
FileInputStream inp = new FileInputStream("D:/location/update.xlsx");
XSSFWorkbook wb = null;
wb = (XSSFWorkbook) WorkbookFactory.create(inp);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(18);//places the start row
XSSFCell cell = null;//places the start column
cell = row.getCell(0);
//#########################################################################################
//#########################################################################################
for (int j = 0; j < exportData.length; j++) {
//sheet.createRow(rowNum+j);
//row = sheet.getRow(rowNum+j);
//row = sheet.getRow(rowNum+j);
for (int i=0; i < columns;i++){
cell.setCellType(CellType.STRING);
cell.setCellValue(exportData[j][i]);
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("D:/location/update.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
this code throws a null pointer because of row being null, I can only seem to get rid of the error by creating new rows. I am using XSSF formatting.
The logic of your code snippet is not clear. It looks not logically to me.
But to avoid NPE while using rows and cells from present sheets, one always needs check whether the row or cell was present already or needs to be new created. This is necessary because for not present rows Sheet.getRow will return null. Also Row.getCell will return null for not present cells.
So we can do:
Sheet sheet = ...
Row row = sheet.getRow(rowIdx); if (row == null) row = sheet.createRow(rowIdx);
Cell cell = row.getCell(cellIdx); if (cell == null) cell = row.createCell(cellIdx);
Now row either is a row which was already present or it is a new created row. And cell either is a cell which was already present or it is a new created cell. Neither row nor cell will be null. And at first present rows/cells will be got before they were new created if not present. So present rows and cells will not be destroyed.
The same is needed in loops:
Sheet sheet = ...
Row row;
Cell cell;
for (int rowIdx = 0; rowIdx < 10; rowIdx++) {
row = sheet.getRow(rowIdx); if (row == null) row = sheet.createRow(rowIdx);
for (int cellIdx = 0; cellIdx < 10; cellIdx++) {
cell = row.getCell(cellIdx); if (cell == null) cell = row.createCell(cellIdx);
// do something with cell
}
}

Getting font size from excel spreadsheet using Java

I'm trying to get the font size of the header on an excel spreadsheet but I haven't been able to get it. I tried using the following to get the size but I haven't been able to get the size. None of the following worked for me because it doesn't return the correct font size.
headerFont.getFontHeight ();
headerFont.getFontHeightInPoints ();
Any suggestion?
Below is the code that I have:
try {
FileInputStream file = new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(1);
int numRows = sheet.getLastRowNum() + 1;
int numCols = sheet.getRow(0).getLastCellNum();
Iterator<Row> rowIterator = sheet.iterator();
for (int i = 0; i < 1; i++) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
for (int j = 0; j < numCols; j++) {
Cell cell = cellIterator.next();
Font headerFont = workbook.createFont();
headerFontFamily = headerFont.getFontName();
headerFont.getFontHeight();
headerFont.getFontHeightInPoints();
}
}
file.close();
} catch (Exception e) {
}
You need to get the fonts from the cells. Fonts are part of the cell styles. Cell styles can be got via Cell.getCellStyle. Then the index of the used font can be got as a short via CelStyle.getFontIndex or as int via CelStyle.getFontIndexAsInt or as int via CelStyle.getFontIndex dependig of apache poi version used. The latter works using current 5.0.0 version.
Complete example:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
class ReadExcel {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelExample.xlsx"));
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
DataFormatter dataFormatter = new DataFormatter();
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
String value = dataFormatter.formatCellValue(cell, evaluator);
System.out.println(value);
CellStyle style = cell.getCellStyle();
//short fontIdx = style.getFontIndex(); // depends on apache poi version
//int fontIdx = style.getFontIndexAsInt(); // depends on apache poi version
int fontIdx = style.getFontIndex(); // depends on apache poi version
Font font = workbook.getFontAt(fontIdx);
System.out.println(font.getFontName() + ", " + font.getFontHeightInPoints());
}
}
workbook.close();
}
}
Note: This only works if the cell only has one font. If the cell contains rich text strings, then there are fonts for each formatting text run. Then RichTextString needs to be got and traversed. This is much more complex and needs to be done different for HSSF and XSSF.

POI HSSF not iterating full set of rows

Cannot iterate the full extent of rows in my XLS spreadsheet. The code {sheet.getPhysicalNumberOfRows} returns 33492 without raising exception, but there are about 43,000 rows??.
If I supply the right number manually the loop executes without complaint.
It must be simple answer but I cannot find one anywhere. can somebody help?
try {
POIFSFileSystem fs = new POIFSFileSystem(new
FileInputStream(fileLocation));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows(); //Gets => 33494
int cols = 1; // No of columns
int tmp = 0;
for (int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if (row != null) {
cell = row.getCell(0);
if (cell != null) {
System.out.println("Line number: " + r " = " + cell);
}
}
}
} catch(Exception ioe) {
ioe.printStackTrace();
}

Creating multiple sheets using Apache poi and servlets

When i am creating multiple sheets using Apache poi and servlets. It is creating the sheet but not writing the data to file. I am trying to write the first 1000 records to sheet1 and next 1000 to sheet2 through below code, but not working
private void writeDataToExcelFile(String string,
ArrayList<ArrayList<String>> excelData, OutputStream outputStream) {
HSSFWorkbook myWorkBook = new HSSFWorkbook();
String sheetName = "";
sheetName = "Document-" + 0;
HSSFSheet mySheet = myWorkBook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
for (int rowNum = 0; rowNum < excelData.size(); rowNum++) {
ArrayList<String> rowData = excelData.get(rowNum);
if(rowNum>0 && rowNum%1000 == 0)
{
sheetName = "Document-" + (rowNum/1000);
mySheet = myWorkBook.createSheet();
}
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < rowData.size(); cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(rowData.get(cellNum));
}
}
System.out.println("Last row:" + mySheet.getLastRowNum());
System.out.println("Row number:" + mySheet.rowIterator().next().getRowNum());
try {
myWorkBook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
What is wrong with my logic.Please do the needful help.
Thanks
When you loop through the dataset, you are wanting to split at row 1000 to start a new sheet, which is fine, however when you start the new sheet, the next row you create is row 1001 (the outer loop index variable)
myRow = mySheet.createRow(rowNum);
To get the effect you wish, change the loop to be something like this:
int currentRow = 0;
for (int rowNum = 0; rowNum < excelData.size(); rowNum++)
{
ArrayList<String> rowData = excelData.get(rowNum);
if(currentRow == 1000)
{
sheetName = "Document-" + (rowNum/1000);
mySheet = myWorkBook.createSheet();
currentRow = 0;
}
myRow = mySheet.createRow(currentRow);
for (int cellNum = 0; cellNum < rowData.size(); cellNum++)
{
myCell = myRow.createCell(cellNum);
myCell.setCellValue(rowData.get(cellNum));
}
currentRow++;
}
I haven't compiled this, so I don't know if it'll work right away, but it should point you in the right direction.
HTH
Edit
Thinking about this further, you could get the same effect from making a 1 line change to the original application (albeit losing a little bit of clarity):
myRow = mySheet.createRow(rowNum%1000);

POI Appending .0 while reading numeric data from excel

I am using POI HSSF to read excel data and I am using JUnit to check the data against database proc RefCursor.
The Junit test fails as the numeric data from the Refcursor for example 100 are compared against the data in the excel sheet 100 but it fails as the POI reads it as 100.0.
InputStream fileInputStream = Testdb.class.getClassLoader().getResourceAsStream(fileName);
//retrieve number of columns and rows
int numRows=0, numCols=0, i, j, minColIndex=0, maxColIndex=0;
POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
HSSFSheet hssfSheet = workBook.getSheetAt(0);
Iterator rowIterator = hssfSheet.rowIterator();
while (rowIterator.hasNext())
{
numRows++;
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
Iterator iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();
if (numRows == 1)
{
minColIndex = hssfRow.getFirstCellNum();
maxColIndex = hssfRow.getLastCellNum();
numCols = maxColIndex;
}
for(int colIndex = minColIndex; colIndex < maxColIndex; colIndex++)
{
HSSFCell hssfCell = hssfRow.getCell(colIndex);
cellTempList.add(hssfCell);
}
cellDataList.add(cellTempList);
}
String expected[][] = new String[numRows][numCols];
String[] tableColumns = new String[numCols];
System.out.println("Rows : " + numRows + "Columns : " + numCols);
System.out.println("Min Col Index : " +minColIndex + "Max Col Index : " + maxColIndex);
for (i=0; i<numRows; i++)
{
List cellTempList = (List) cellDataList.get(i);
for (j=0; j < numCols; j++)
{
HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
if (i == 0)
{
tableColumns[j] = hssfCell.toString();
System.out.print(tableColumns[j] + "\t");
}
else
{
if(hssfCell != null)
{
expected[i-1][j] = hssfCell.toString();
}
else
{
expected[i-1][j] = null;
}
System.out.print(expected[i-1][j] + "\t");
}
}
System.out.println();
}
This is a generic framework program which I am building so the framework should be intelligent enough to disregard the ".0".
Any inputs on how to resolve this?
This is virtually identical to a number of other questions here, such as returning decimal instead of string (POI jar)
The answer is the same as the one I gave here:
POI is giving you the exact value that Excel has stored in the File. Generally, if you write a number in an Excel cell, Excel will store that as a number with formatting. POI provides support to do that formatting for you if you want it (most people don't - they want the numbers as numbers so they can use them)
The class you're looking for is DataFormatter. Your code would be something like
DataFormatter fmt = new DataFormatter();
for (Row r : sheet) {
for (Cell c : r) {
CellReference cr = new CellRefence(c);
System.out.println("Cell " + cr.formatAsString() + " is " +
fmt.formatCellValue(c) );
}
}
Hi my solution was just to put the symbol:
'
in front of every number. Then the number is processed as text.
After you do that you would see little green triangle and warning:
For me this is not a problem, because it works.

Categories

Resources