use of org.apache.poi.ss.usermodel.Row in project poi by apache.
the class SheetUtil method getColumnWidth.
/**
* Compute width of a column based on a subset of the rows and return the result
*
* @param sheet the sheet to calculate
* @param column 0-based index of the column
* @param useMergedCells whether to use merged cells
* @param firstRow 0-based index of the first row to consider (inclusive)
* @param lastRow 0-based index of the last row to consider (inclusive)
* @return the width in pixels or -1 if cell is empty
*/
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow) {
DataFormatter formatter = new DataFormatter();
int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
double width = -1;
for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
Row row = sheet.getRow(rowIdx);
if (row != null) {
double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells);
width = Math.max(width, cellWidth);
}
}
return width;
}
use of org.apache.poi.ss.usermodel.Row in project poi by apache.
the class TestXSSFSheetRowGrouping method fillData.
private void fillData(Workbook p_wb) {
Sheet sheet = p_wb.createSheet("sheet123");
sheet.setRowSumsBelow(false);
for (int i = 0; i < ROWS_NUMBER; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(i + 1);
}
int i = 1;
while (i < ROWS_NUMBER) {
int end = i + (GROUP_SIZE - 2);
// natural order
int start = i;
// int start = end - 1; // reverse order
while (start < end) {
// natural order
// while (start >= i) { // reverse order
sheet.groupRow(start, end);
//o_groupsNumber++;
boolean collapsed = isCollapsed();
//System.out.println("Set group " + start + "->" + end + " to " + collapsed);
sheet.setRowGroupCollapsed(start, collapsed);
// natural order
start++;
// start--; // reverse order
}
i += GROUP_SIZE;
}
}
use of org.apache.poi.ss.usermodel.Row in project poi by apache.
the class TestXSSFSheet method setupSheet.
private XSSFWorkbook setupSheet() {
//set up workbook
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
Row row1 = sheet.createRow(0);
Cell cell = row1.createCell(0);
cell.setCellValue("Names");
Cell cell2 = row1.createCell(1);
cell2.setCellValue("#");
Row row2 = sheet.createRow(1);
Cell cell3 = row2.createCell(0);
cell3.setCellValue("Jane");
Cell cell4 = row2.createCell(1);
cell4.setCellValue(3);
Row row3 = sheet.createRow(2);
Cell cell5 = row3.createCell(0);
cell5.setCellValue("John");
Cell cell6 = row3.createCell(1);
cell6.setCellValue(3);
return wb;
}
use of org.apache.poi.ss.usermodel.Row in project poi by apache.
the class TestXSSFSheetShiftRows method testBug53798a.
/** negative row shift causes corrupted data or throws exception */
@Test
public void testBug53798a() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
Sheet testSheet = wb.getSheetAt(0);
testSheet.shiftRows(3, 3, -1);
for (Row r : testSheet) {
r.getRowNum();
}
testSheet.shiftRows(6, 6, 1);
Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(read);
Sheet readSheet = read.getSheetAt(0);
verifyCellContent(readSheet, 0, "0.0");
verifyCellContent(readSheet, 1, "1.0");
verifyCellContent(readSheet, 2, "3.0");
verifyCellContent(readSheet, 3, null);
verifyCellContent(readSheet, 4, "4.0");
verifyCellContent(readSheet, 5, "5.0");
verifyCellContent(readSheet, 6, null);
verifyCellContent(readSheet, 7, "6.0");
verifyCellContent(readSheet, 8, "8.0");
read.close();
}
use of org.apache.poi.ss.usermodel.Row in project poi by apache.
the class TestXSSFSheetShiftRows method testBug53798.
/** negative row shift causes corrupted data or throws exception */
@Test
public void testBug53798() throws IOException {
// NOTE that for HSSF (.xls) negative shifts combined with positive ones do work as expected
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
Sheet testSheet = wb.getSheetAt(0);
// 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
// when shifted by less than -1 negative amount (try -2)
testSheet.shiftRows(3, 3, -2);
// 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
// xlsx file with unreadable data in the negative shifted row.
// NOTE it's ok to create any other row.
Row newRow = testSheet.createRow(3);
Cell newCell = newRow.createCell(0);
newCell.setCellValue("new Cell in row " + newRow.getRowNum());
// 3) once a negative shift has been made any attempt to shift another group of rows
// (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
// org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
// NOTE: another negative shift on another group of rows is successful, provided no new rows in
// place of previously shifted rows were attempted to be created as explained above.
// -- CHANGE the shift to positive once the behaviour of the above has been tested
testSheet.shiftRows(6, 7, 1);
Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(read);
Sheet readSheet = read.getSheetAt(0);
verifyCellContent(readSheet, 0, "0.0");
verifyCellContent(readSheet, 1, "3.0");
verifyCellContent(readSheet, 2, "2.0");
verifyCellContent(readSheet, 3, "new Cell in row 3");
verifyCellContent(readSheet, 4, "4.0");
verifyCellContent(readSheet, 5, "5.0");
verifyCellContent(readSheet, 6, null);
verifyCellContent(readSheet, 7, "6.0");
verifyCellContent(readSheet, 8, "7.0");
read.close();
}
Aggregations