Apache POI autoSizeColumn() not working right [duplicate] - java

This question already has answers here:
Apache POI autoSizeColumn Resizes Incorrectly
(10 answers)
Closed 9 years ago.
I am creating a program that writes information to an excel file using the apache poi. After I enter all of my data into the file I call the autoSizeColumn method on every column of the file. But it resizes the columns to the width of the last cell entered which is sometime not as big as the other cells in the column. I know I'm using it right and unfortunately I don't have Internet right now to post any code but I will update when I can.
Ok I hope I am using the code tags right but here it is :
public void writeFile() {
Workbook wb = new HSSFWorkbook();
Sheet s = wb.createSheet();
try {
FileOutputStream out = new FileOutputStream(
this.getSelectedFile());
// create a new workbook
// create a new sheet
// declare a row object reference
Row r = null;
// declare a cell object reference
Cell c = null;
// in case of plain ascii
wb.setSheetName(0, "Street Light Report");
// create a sheet with 30 rows (0-29)
// int rownum;
// short rownum;
// PrintWriter printOut = new PrintWriter(
// this.getSelectedFile());
String[] colName = { "Light Id", "Flagged",
"Malfunction", "Comments", "Location", "Date" };
// printOut.println("Light Id,Flagged,Malfunction,Comments,Location,Date");
Connections.connect();
String[][] data = Connections.searchForFlagged();
for (int i = 0; i < data.length; i++) {
r = s.createRow(i);
}
r.setRowNum(0);
for (int j = 0; j < 6; j++) {
c = r.createCell(j);
switch (j) {
case 0:
c.setCellValue(colName[j]);
break;
case 1:
c.setCellValue(colName[j]);
break;
case 2:
c.setCellValue(colName[j]);
break;
case 3:
c.setCellValue(colName[j]);
break;
case 4:
c.setCellValue(colName[j]);
break;
case 5:
c.setCellValue(colName[j]);
break;
}
}
for (int i = 0; i < data.length; i++) {
r.setRowNum(i + 1);
for (int j = 0; j < data[i].length; j++) {
c = r.createCell(j);
// System.out.println(data[i][j]);
switch (j) {
case 0:
c.setCellType(Cell.CELL_TYPE_NUMERIC);
c.setCellValue(Integer.parseInt(data[i][j]));
break;
case 1:
if (data[i][j].equals("true"))
c.setCellValue("Yes");
else
c.setCellValue("No");
break;
case 2:
if (data[i][j].equals("I"))
c.setCellValue("Intermittent");
else
c.setCellValue("Not Functional");
break;
case 3:
c.setCellValue(data[i][j]);
break;
case 4:
c.setCellValue(data[i][j]);
break;
case 5:
c.setCellValue(data[i][j]);
break;
}
}
}
wb.getSheetAt(0).setPrintGridlines(true);
for (int j = 0; j < 6; j++) {
s.autoSizeColumn(j);
}
wb.write(out);
out.close();
} catch (Exception ex) {
}
};
I would post an image here but I need rep. point and I just started here.
** Note that the Connections class is the JDBC class so that java can pull the information from th mySQL database it returns a 2D array of strings **

You just need to move the call to
sheet.autoSizeColumn(columnNumber);
to a point in your code after the data has been added.
Here is a link to the API
That's the only way to do it. I had same problem, I was calling the method autoSizeColumn() before I entered the data. Hop it helped

Move the for loop :
for (int j = 0; j < 6; j++) {
s.autoSizeColumn(j);
}
to be just before you write in the workbook and close your out object.
wb.write(out);

Related

Sort rows in XLSX file with Apache POI

I try to sort sheet with use of Apache POI by a given column. As I know there isn't any built-in solution for this purpose. First row of sheet contains merged cells and grouped columns. I try to sort cells by second column.
public static void sortSheet(XSSFWorkbook workbook, Sheet sheet) {
//copy all rows to temp
List<Row> rows = Lists.newArrayList(sheet.rowIterator());
//sort rows in the temp
rows.sort(Comparator.comparing(cells -> cells.getCell(2).getStringCellValue()));
//remove all rows from sheet
removeAllRows(sheet);
//create new rows with values of sorted rows from temp
for (int i = 0; i < rows.size(); i++) {
Row newRow = sheet.createRow(i);
Row sourceRow = rows.get(i);
// Loop through source columns to add to new row
for (int j = 0; j < sourceRow.getLastCellNum(); j++) {
// Grab a copy of the old/new cell
Cell oldCell = sourceRow.getCell(j);
Cell newCell = newRow.createCell(j);
// If the old cell is null jump to next cell
if (oldCell == null) {
newCell = null;
continue;
}
// Copy style from old cell and apply to new cell
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(newCellStyle);
// If there is a cell comment, copy
if (oldCell.getCellComment() != null) {
newCell.setCellComment(oldCell.getCellComment());
}
// If there is a cell hyperlink, copy
if (oldCell.getHyperlink() != null) {
newCell.setHyperlink(oldCell.getHyperlink());
}
// Set the cell data type
newCell.setCellType(oldCell.getCellType());
// Set the cell data value
switch (oldCell.getCellType()) {
case BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
CellRangeAddress cellRangeAddress = sheet.getMergedRegion(j);
if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
(newRow.getRowNum() +
(cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
)),
cellRangeAddress.getFirstColumn(),
cellRangeAddress.getLastColumn());
sheet.addMergedRegion(newCellRangeAddress);
}
}
}
}
private static void removeAllRows(Sheet sheet) {
for (int i = 0; i < sheet.getLastRowNum(); i++) {
sheet.removeRow(sheet.getRow(i));
}
}
Original source: Apache-POI sorting rows in excel
I got following error message during execution:
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.getR(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:378)
at WriteToExistingFile.sortSheet(WriteToExistingFile.java:234)
at WriteToExistingFile.write(WriteToExistingFile.java:145)
Exception is thrown at this line:
if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {

unable to get numeric values from excel sheet in selenium webdriver with java

My Java
public class Readexcel {
public void readfile(String filepath, String filename, String sheetname) throws IOException
{
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
//find the total rows in sheet
int rowcount = sh.getLastRowNum()-sh.getFirstRowNum();
// create a loop to create
for(int i=0;i<rowcount+1;i++)
{
Row row= sh.getRow(i);
// create a loop to print cell values
for(int j=0;j<row.getLastCellNum();j++)
{
Cell cell= row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(row.getCell(j).getNumericCellValue() + " ");
break;
}
System.out.print(row.getCell(j).getStringCellValue()+"||");
}
System.out.println();
}
}
public static void main(String...strings) throws IOException
{
Readexcel re = new Readexcel();
String filepath = "F://Excelsheet";
re.readfile(filepath,"Book1.xlsx","Sheet1");
}
}
By using above code I am getting an error "cannot get text value from numeric cell". Any Help? Also My Output is not properly alligned. All the String are showing one under one. output should be like
Username Password
john 123
rambo 456
but i am getting output like
Username
password
john
Change your for loop after // create a loop to print cell values comment for this:
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
break;
}
}
Switch is to recognise type of cell. For numeric cells you have to use getNumericCellValue() instead of getStringCellValue()
For the second problem use System.out.print() instead System.out.println() which is used to print what is between the double quotes and move the printing cursor to the next line.
EDIT:
This how my readFile() function looks:
public void readfile(String filepath, String filename, String sheetname) throws IOException {
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
// find the total rows in sheet
int rowcount = sh.getLastRowNum() - sh.getFirstRowNum();
// create a loop to create
for (int i = 0; i < rowcount + 1; i++) {
Row row = sh.getRow(i);
// create a loop to print cell values
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
break;
}
}
System.out.println();
}
}
EDIT 2
Changed System.out.print(row.getCell(j).getNumericCellValue() + " "); for
System.out.print((int)row.getCell(j).getNumericCellValue() + " "); in case Cell.CELL_TYPE_NUMERIC

Reading blank xls cells through POI

I'm trying to read the empty cells of an xls file and port them to my object xls as null entries. But I'm not identifying these cells as blank. Can someone can tell me what I'm doing wrong?
private boolean lerArquivo(String dir) throws IOException {
HSSFWorkbook wb = null;
HSSFRow row = null;
HSSFCell cell = null;
String path = dir;
boolean flag = false;
InputStream inp = new FileInputStream(path);
wb = new HSSFWorkbook(inp);
HSSFSheet sheet = wb.getSheetAt(0);
for (Iterator rit = (Iterator) sheet.rowIterator(); rit.hasNext();) {
row = (HSSFRow) rit.next();
for (Iterator cit = (Iterator) row.cellIterator(); cit.hasNext();) {
cell = (HSSFCell) cit.next();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
xls.add(cell.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_BLANK:
xls.add(null);
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
xls.add(cell.getDateCellValue().toString());
} else {
xls.add(String.valueOf(cell.getNumericCellValue()));
}
break;
default:
}
}
}
EDIT 01 -
Hello Gagravarr, thanks for your help.
I Tried your suggest, and read the documentation.
following what i understood, i changed my code, reaching this:
even this way, the code isn't reading the blank cell. any guess ?
for (int rowNum = 0; rowNum < 4; rowNum++) {
row = sheet.getRow(rowNum);
for (int celNum = 0; celNum < row.getLastCellNum(); celNum++) {
cell = row.getCell(celNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
xls.add(cell.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_BLANK: {
xls.add("");
break;
}
case HSSFCell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
xls.add(cell.getDateCellValue().toString());
} else {
xls.add(String.valueOf(cell.getNumericCellValue()));
}
break;
default:
}
}
}
Apache POI has pretty detailed and explicit documentation on iterating over rows and cells, you'd be very well advised to read it!
Copying and pasting the example from there, you can see how to handle / detect missing rows and cells, along with blank cells:
// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
}
}
}

Java: Issue With Storing Data from Excel in an ArrayList of ArrayLists

I'm trying to store data from excel in ArrayLists, which will then be stored in a main ArrayList. The reason being that each row of the excel sheet has data that is particular to that row. I keep having an issue because I believe when I erase the inner ArrayList before looping back, it deletes the data in the master ArrayList as well.
import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.ArrayList;
public class StoreData {
ArrayList<ArrayList<String>> shapes;
ArrayList<String> innerMembers;
ArrayList<String> test;
int start;
int end;
public StoreData(){
this.shapes = new ArrayList<>();
this.innerMembers = new ArrayList<>();
this.test = new ArrayList<>();
start = 0;
end = 0;
}
public ArrayList<ArrayList<String>> storeValues(String shapeType){
//get the shape
switch(shapeType){
case "A":
start = 1;
end = 273;
break;
case "B":
start = 274;
end = 291;
break;
case "C":
start = 292;
end = 319;
break;
case "D":
start = 320;
end = 340;
break;
case "E":
start = 341;
end = 372;
break;
case "F":
start = 373;
end = 412;
break;
case "G":
start = 413;
end = 539;
break;
case "H":
start = 540;
end = 814;
break;
case "I":
start = 1464;
end = 1958;
}
try
{
FileInputStream x = new FileInputStream(new File("/Users/JohnDoe/Documents/File.xls"));
//Create Workbook instance
Workbook workbook = new HSSFWorkbook(x);
//Get first/desired sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
for (int i = start; i <= end; i ++){
Row row = sheet.getRow(i++);
//iterate through each cell in row: max number of cells is 76
for (int j = 0; j < 77; j++) {
Cell cell = row.getCell(j);
cell.setCellType(1);
innerMembers.add(cell.getStringCellValue());
}
/*******************/
//Add to master ArrayList and clear inner ArrayList to get repopulated
/*******************/
shapes.add(innerMembers);
innerMembers.clear();
}
x.close();
}
catch (Exception e)
{
e.printStackTrace();
}
/***TEST STATEMENT***/
System.out.println(shapes.get(8).get(20));
/***TEST STATEMENT***/
return shapes;
}
}
You might want to try:
shapes.add(new ArrayList<String>(innerMembers)); // shallow copy
innerMembers.clear();

Can't get Apache POI autosizeColumn() to work right

Ok so I know you have to add the autoSizeColumns() method after you enter all your data into the sheet and workbook. And that is what I am doing but it is only resizing to the width of the last Cell entered??
public void writeFile() {
Workbook wb = new HSSFWorkbook();
Sheet s = wb.createSheet();
try {
FileOutputStream out = new FileOutputStream(
this.getSelectedFile());
// create a new workbook
// create a new sheet
// declare a row object reference
Row r = null;
// declare a cell object reference
Cell c = null;
// in case of plain ascii
wb.setSheetName(0, "Street Light Report");
// create a sheet with 30 rows (0-29)
// int rownum;
// short rownum;
// PrintWriter printOut = new PrintWriter(
// this.getSelectedFile());
String[] colName = { "Light Id", "Flagged",
"Malfunction", "Comments", "Location", "Date" };
// printOut.println("Light Id,Flagged,Malfunction,Comments,Location,Date");
Connections.connect();
String[][] data = Connections.searchForFlagged();
for (int i = 0; i < data.length; i++) {
r = s.createRow(i);
}
r.setRowNum(0);
for (int j = 0; j < 6; j++) {
c = r.createCell(j);
switch (j) {
case 0:
c.setCellValue(colName[j]);
break;
case 1:
c.setCellValue(colName[j]);
break;
case 2:
c.setCellValue(colName[j]);
break;
case 3:
c.setCellValue(colName[j]);
break;
case 4:
c.setCellValue(colName[j]);
break;
case 5:
c.setCellValue(colName[j]);
break;
}
}
for (int i = 0; i < data.length; i++) {
r.setRowNum(i + 1);
for (int j = 0; j < data[i].length; j++) {
c = r.createCell(j);
// System.out.println(data[i][j]);
switch (j) {
case 0:
c.setCellType(Cell.CELL_TYPE_NUMERIC);
c.setCellValue(Integer.parseInt(data[i][j]));
break;
case 1:
if (data[i][j].equals("true"))
c.setCellValue("Yes");
else
c.setCellValue("No");
break;
case 2:
if (data[i][j].equals("I"))
c.setCellValue("Intermittent");
else
c.setCellValue("Not Functional");
break;
case 3:
c.setCellValue(data[i][j]);
break;
case 4:
c.setCellValue(data[i][j]);
break;
case 5:
c.setCellValue(data[i][j]);
break;
}
}
}
wb.getSheetAt(0).setPrintGridlines(true);
for (int j = 0; j < 6; j++) {
s.autoSizeColumn(j);
}
wb.write(out);
out.close();
} catch (Exception ex) {
}
};
It seems that you do not have too many columns. Can you try defining each column like this:
sheet.autoColumnWidth(column number); //0 for the first column, 1 for the second, etc.
If that works then there is something wrong with your loop.

Categories