windows一般DPI=96,即每英寸有96个像素
1英寸 = 96px
1英寸 = 72pt(磅) = 1440twips(缇)
所以:
1pt = 20twips(缇)
1px = 72/96pt = 3/4pt = 15twips(缇)
POI中行高单位和Office Excel中行高单位是不一样的
/** * 设置行高,以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));
}
}
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);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/lipinganq/article/details/78081300
内容来源于网络,如有侵权,请联系作者删除!