Java poi导出让行高和样式和某一行一样
要让导出的行高和样式和某一行一样,可以使用Java poi中的CellStyle和Row对象的方法来实现。
首先,获取某一行的样式和行高:
Row sourceRow = sheet.getRow(rowIndex);
CellStyle sourceStyle = sourceRow.getRowStyle();
float sourceHeight = sourceRow.getHeightInPoints();
然后,将这些样式和行高应用到导出的行中:
Row newRow = sheet.createRow(newRowIndex);
newRow.setHeightInPoints(sourceHeight);
if (sourceStyle != null) {
CellStyle newStyle = workbook.createCellStyle();
newStyle.cloneStyleFrom(sourceStyle);
newRow.setRowStyle(newStyle);
}
完整的代码示例:
// 获取源行样式和行高
Row sourceRow = sheet.getRow(rowIndex);
CellStyle sourceStyle = sourceRow.getRowStyle();
float sourceHeight = sourceRow.getHeightInPoints();
// 创建新行并应用样式和行高
Row newRow = sheet.createRow(newRowIndex);
newRow.setHeightInPoints(sourceHeight);
if (sourceStyle != null) {
CellStyle newStyle = workbook.createCellStyle();
newStyle.cloneStyleFrom(sourceStyle);
newRow.setRowStyle(newStyle);
}
``
原文地址: http://www.cveoy.top/t/topic/fV4v 著作权归作者所有。请勿转载和采集!