}
/**
- * set the degree of rotation. (I've not actually seen this used anywhere)
+ * set the degree of rotation.
*
*
* @param rotation the degree of rotation
}
/**
- * get the degree of rotation. (I've not actually seen this used anywhere)
+ * get the degree of rotation.
*
*
* @return rotation - the degree of rotation
/**
* set the degree of rotation for the text in the cell
+ *
+ * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
+ * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
+ * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+ * of Excel file-format that this CellStyle is applied to.
+ *
* @param rotation degrees (between -90 and 90 degrees, of 0xff for vertical)
*/
@Override
//The 4th quadrant (-1 to -90) is stored as (91 to 180)
rotation = (short)(90 - rotation);
}
- else if ((rotation < -90) ||(rotation > 90)) {
+ else if (rotation > 90 && rotation <= 180) {
+ // stay compatible with the range used by XSSF, map from ]90..180] to ]0..-90]
+ // we actually don't need to do anything here as the internal value is stored in [0-180] anyway!
+ }
+ else if ((rotation < -90) || (rotation > 90)) {
//Do not allow an incorrect rotation to be set
throw new IllegalArgumentException("The rotation must be between -90 and 90 degrees, or 0xff");
}
short getVerticalAlignment();
/**
- * set the degree of rotation for the text in the cell
- * @param rotation degrees (between -90 and 90 degrees)
+ * set the degree of rotation for the text in the cell.
+ *
+ * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
+ * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
+ * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+ * of Excel file-format that this CellStyle is applied to.
+ *
+ * @param rotation degrees (see note above)
*/
-
void setRotation(short rotation);
/**
- * get the degree of rotation for the text in the cell
- * @return rotation degrees (between -90 and 90 degrees)
+ * get the degree of rotation for the text in the cell.
+ *
+ * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
+ * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
+ * value-range as used by the type of Excel file-format that this CellStyle is applied to.
+ *
+ * @return rotation degrees (see note above)
*/
-
short getRotation();
/**
* <code>[degrees below horizon] = 90 - textRotation.</code>
* </p>
*
+ * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
+ * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
+ * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+ * of Excel file-format that this CellStyle is applied to.
+ *
* @param rotation - the rotation degrees (between 0 and 180 degrees)
*/
@Override
* <code>[degrees below horizon] = 90 - textRotation.</code>
* </p>
*
+ * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
+ * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
+ * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+ * of Excel file-format that this CellStyle is applied to.
+ *
* @param rotation - the rotation degrees (between 0 and 180 degrees)
*/
public void setTextRotation(long rotation) {
+ if(rotation < 0 && rotation >= -90) {
+ rotation = 90 + ((-1)*rotation);
+ }
cellAlignement.setTextRotation(rotation);
}
wb.close();
}
+
+ private void createXls() throws IOException
+ {
+ Workbook workbook = new HSSFWorkbook();
+ FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xls");
+ Sheet sheet1 = workbook.createSheet();
+ Row row1 = sheet1.createRow((short) 0);
+
+ Cell cell1 = row1.createCell(0);
+
+ cell1.setCellValue("Successful rotated text.");
+
+ CellStyle style = workbook.createCellStyle();
+ style.setRotation((short) -90);
+
+ cell1.setCellStyle(style);
+
+ workbook.write(fileOut);
+ fileOut.close();
+ workbook.close();
+ }
+
+ private void createXlsx() throws IOException {
+ Workbook workbook = new XSSFWorkbook();
+ FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xlsx");
+ Sheet sheet1 = workbook.createSheet();
+ Row row1 = sheet1.createRow((short) 0);
+
+ Cell cell1 = row1.createCell(0);
+
+ cell1.setCellValue("Unsuccessful rotated text.");
+
+ CellStyle style = workbook.createCellStyle();
+ style.setRotation((short) -90);
+
+ cell1.setCellStyle(style);
+
+ workbook.write(fileOut);
+ fileOut.close();
+ workbook.close();
+ }
+
+ @Ignore("Creates files for checking results manually, actual values are tested in Test*CellStyle")
+ @Test
+ public void test58043() throws Exception {
+ createXls();
+ createXlsx();
+ }
}
target.close();
reference.close();
}
+
+ @Test
+ public void test58043() {
+ assertEquals(0, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)89);
+ assertEquals(89, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)90);
+ assertEquals(90, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)179);
+ assertEquals(179, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)180);
+ assertEquals(180, cellStyle.getRotation());
+
+ // negative values are mapped to the correct values for compatibility between HSSF and XSSF
+ cellStyle.setRotation((short)-1);
+ assertEquals(91, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-89);
+ assertEquals(179, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-90);
+ assertEquals(180, cellStyle.getRotation());
+ }
}
for (int i = 0; i < data.length; i++)
assertEquals("At offset " + i, data[i], recordBytes[i + 4]);
}
+
+ public void testRotation() {
+ ExtendedFormatRecord record = createEFR();
+ assertEquals(0, record.getRotation());
+
+ record.setRotation((short)1);
+ assertEquals(1, record.getRotation());
+
+ record.setRotation((short)89);
+ assertEquals(89, record.getRotation());
+
+ record.setRotation((short)90);
+ assertEquals(90, record.getRotation());
+
+ // internally values below zero are stored differently
+ record.setRotation((short)-1);
+ assertEquals(255, record.getRotation());
+
+ record.setRotation((short)-89);
+ assertEquals(-77, 90-record.getRotation());
+
+ record.setRotation((short)-90);
+ assertEquals(-76, 90-record.getRotation());
+ }
}
import java.util.Calendar;
import java.util.Date;
-import junit.framework.TestCase;
-
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.TempFile;
+import org.junit.Test;
+
+import junit.framework.TestCase;
/**
* Class to test cell styling functionality
// out.close();
// }
}
+
+
+ @Test
+ public void test58043() throws IOException {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFCellStyle cellStyle = wb.createCellStyle();
+
+ assertEquals(0, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)89);
+ assertEquals(89, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)90);
+ assertEquals(90, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-1);
+ assertEquals(-1, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-89);
+ assertEquals(-89, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-90);
+ assertEquals(-90, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)-89);
+ assertEquals(-89, cellStyle.getRotation());
+
+ // values above 90 are mapped to the correct values for compatibility between HSSF and XSSF
+ cellStyle.setRotation((short)179);
+ assertEquals(-89, cellStyle.getRotation());
+
+ cellStyle.setRotation((short)180);
+ assertEquals(-90, cellStyle.getRotation());
+
+ wb.close();
+ }
}