Apache POI有助于以特定方式对齐单元格及其内容。无涯教程可以设置单元格的高度和宽度,并使其顶部或底部对齐。 Sheet类提供设置高度和宽度方法。看一个示例,在该示例中创建了一个单元并对其进行对齐。
package poiexample; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.HorizontalAlignment; 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.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; public class AlignExample { public static void main(String[] args) throws FileNotFoundException, IOException { Workbook wb = new HSSFWorkbook(); try (OutputStream os = new FileOutputStream("Learnfk.xls")) { Sheet sheet = wb.createSheet("New Sheet"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Javatpoint"); CellStyle cellStyle = wb.createCellStyle(); row = sheet.createRow(5); cell = row.createCell(0); //左上对齐 HSSFCellStyle style1 = (HSSFCellStyle) wb.createCellStyle(); sheet.setColumnWidth(0, 8000); cell.setCellValue("Top Left"); cell.setCellStyle(style1); row = sheet.createRow(6); cell = row.createCell(1); row.setHeight((short) 800); wb.write(os); }catch(Exception e) { System.out.println(e.getMessage()); } } }
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Python实战 · 从0到1搭建直播视频平台 -〔Barry〕