POI Excel行高设置

来源:互联网 发布:淘宝每月刷多少会封号 编辑:程序博客网 时间:2024/04/29 06:10

1.Excel行高单位概述

  1. px是相对长度,表示pixel,像素,是屏幕上显示数据的最基本的点
  2. pt是绝对长度,表示point,磅,是印刷行业常用单位,等于1/72英寸
  3. DPI(或PPI),分辨率,pixel(dot)per inch,每英寸的像素(点)数,微软都将DPI和PPI混为一体,我们也就无须较真了。Windows系统默认是96dpi,Apple系统默认是72dpi
  4. px,pt和dpi的关系
windows一般DPI=96,即每英寸有96个像素1英寸 = 96px1英寸 = 72pt(磅) = 1440twips(缇)所以:1pt = 20twips(缇)1px = 72/96pt = 3/4pt = 15twips(缇)

2. 设置Excel行高

POI中行高单位和Office Excel中行高单位是不一样的
1. POI中的行高单位是twips(缇)
2. Office Excel行高单位是pt(磅)
3. 1pt = 20twips
4. 1px = 3/4pt
5. 1px = 15twips

    /**     * 设置行高,以twips(缇)为单位, 1twips=1/20pt(磅)     * @param height 设置的行高,单位为1/20pt     * */    public void setHeight(short height)    /**     * 设置行高,单位为磅pt     * @param height 设置的行高,单位为磅pt     * */    public void setHeightInPoints(float height) {        if(height == -1){            row.setHeight((short)(0xFF | 0x8000));            row.setBadFontHeight(false);        } else {            row.setBadFontHeight(true);            row.setHeight((short) (height * 20));        }    }

3.实例

package hssf_grid;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.util.WorkbookUtil;public class RowHeightTest {        public static void main(String[] args) throws Exception {            File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");            if (file.exists()) {                file.delete();            }            BufferedOutputStream out = null;            try {                out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xls"));                exportExcel(out);            } finally {                out.close();            }        }        private static void exportExcel(BufferedOutputStream out) throws Exception {            HSSFWorkbook workbook = new HSSFWorkbook();            // 格式化Sheet名,使其合法            String safeSheetName = WorkbookUtil.createSafeSheetName("日历");            HSSFSheet sheet = workbook.createSheet(safeSheetName);            sheet.setColumnWidth(0, 100*256);            HSSFRow row3 = sheet.createRow(3);            row3.createCell(0).setCellValue("行高16px = 12pt = 240twips");            // 单位为twips(缇)            row3.setHeight((short)(12*20));            HSSFRow row5 = sheet.createRow(5);            row5.createCell(0).setCellValue("行高32px = 24pt = 480twips");            row5.setHeight((short)(24*20));            HSSFRow row7 = sheet.createRow(7);            row7.createCell(0).setCellValue("行高48px = 36pt = 720twips");            // 单位为pt(磅)            row7.setHeightInPoints(36);            HSSFRow row9 = sheet.createRow(9);            row9.createCell(0).setCellValue("行高64px = 48pt = 1440twips");            // 单位为pt(磅)            row9.setHeightInPoints(48);            workbook.write(out);        }}

这里写图片描述