-
2021-01-28 14:15:34
package com.java.poi;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
/**
* 字体设置
* @author nidegui
* @create 2019-06-17 11:30
*/
public class Test7 {
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet页");
Row row=sheet.createRow(1);
//创建一个字体
Font font=wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("Courier New ");
font.setItalic(true);
font.setStrikeout(true);
CellStyle style=wb.createCellStyle();
style.setFont(font);
Cell cell = row.createCell((short) 1);
cell.setCellValue("this is test of fonts");
cell.setCellStyle(style);
FileOutputStream is=new FileOutputStream("E:\\3.xls");
wb.write(is);
is.close();
}
}
更多相关内容 -
POI入门基础
2020-12-24 13:37:151.POI的基础知识:HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中,主要部分包括Excell对象,样式和格式,还有辅助操作。有以下几种对象:HSSFWorkbook excell的文档对象HSSFSheet excell的表单...1.POI的基础知识:
HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中,主要部分包括Excell对象,样式和格式,还有辅助操作。有以下几种对象:
HSSFWorkbook excell的文档对象
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
在poi1.7中才有以下2项:
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
2.POI的基础功能点:
1.创建sheet:
写道
public void createSheet(String title) {
workBook = new HSSFWorkbook();
sheet = workBook.createSheet(title);
sheet.setVerticallyCenter(true);
}
2.创建row:
写道
public HSSFRow createRow(HSSFCellStyle style, int currentRow, int colNum) {
HSSFRow row = sheet.createRow(currentRow);
for (short cellIndex = 0; cellIndex < colNum; cellIndex++) {
HSSFCell cell = row.createCell(cellIndex);
cell.setCellValue("");
cell.setCellStyle(style);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
return row;
}
注意:在创建row的时候,如果正行的cell的样式一样的话,最好在这个时候就把样式一起设置给cell。因为合并单元格后,合并后的cell的样式是由合并前多个cell的样式组合成的。(- -。有点绕)
3.合并单元格:
写道
public void mergedRegion(int rowFrom, int colFrom, int rowTo, int colTo) {
Region region = new Region(rowFrom, (short) colFrom, rowTo,
(short) colTo);
sheet.addMergedRegion(region);
}
3例子:
写道
/**
* 生成表头
*
* @param sheet
* @param header
* @param title
* @param rowNum
*/
private void createInterfaceSheetHeader(String[] header, String title,
int rowNum) {
HSSFSheet sheet = getSheet();
for (int i = 0; i < header.length; i++) {
if ("说明".equals(header[i])) {
sheet.setColumnWidth(i, 5000);
} else {
sheet.setColumnWidth(i, 3000);
}
}
short length = (short) (header.length - 1);
// row 1
HSSFRow row = null;
row = createRow(getCellStyle1(), 0, header.length);
row.setHeightInPoints(50);
mergedRegion(0, (short) 0, 0, length);
HSSFCell cell = row.getCell(0);
cell.setCellValue(title);
// row2-4
row = createRow(getCellStyle2(), 1, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("版本号");
mergedRegion(1, (short) 1, 1, length);
cell = row.getCell(1);
row = createRow(getCellStyle2(), 2, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("定制日期");
mergedRegion(2, (short) 1, 2, length);
cell = row.getCell(1);
//生成当前日期
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
row = createRow(getCellStyle2(), 3, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("定制者");
mergedRegion(3, (short) 1, 3, length);
cell = row.getCell(1);
// row5
row = createRow(getCellStyle3(), 4, header.length);
row.setHeightInPoints(20);
mergedRegion(4, (short) 0, 4, length);
cell = row.getCell(0);
cell.setCellValue("修订记录");
// row6-8
row = createRow(getCellStyle2(), 5, header.length);
row.setHeightInPoints(15);
mergedRegion(5, (short) 2, 5, length);
cell = row.getCell(0);
cell.setCellValue("修订日期");
cell = row.getCell(1);
cell.setCellValue("修订者");
cell = row.getCell(2);
cell.setCellValue("修订说明");
row = createRow(getCellStyle2(), 6, header.length);
row.setHeightInPoints(15);
mergedRegion(6, (short) 2, 6, length);
row = createRow(getCellStyle2(), 7, header.length);
row.setHeightInPoints(15);
mergedRegion(7, (short) 2, 7, length);
// row9
row = createRow(getCellStyle4(), 8, header.length);
row.setHeightInPoints(15);
mergedRegion(8, (short) 0, 8, length);
createSheetHeader(sheet, header, 9);
}
/**
* row 1
*
* @return
*/
public HSSFCellStyle getCellStyle1() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 2-4 6-8
*
* @return
*/
public HSSFCellStyle getCellStyle2() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 5
*
* @return
*/
public HSSFCellStyle getCellStyle3() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 9
*
* @return
*/
public HSSFCellStyle getCellStyle4() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 10
*
* @return
*/
public HSSFCellStyle getCellStyle5() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setFillForegroundColor(HSSFColor.LAVENDER.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 11-n
*
* @return
*/
public HSSFCellStyle getCellStyle6() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10.5);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
1
顶
0
踩
分享到:
2011-04-10 13:19
浏览 4721
评论
-
poi 一个单元格不同字体
2020-12-24 13:37:12poi 一个单元格不同字体try{HSSFWorkbookworkbook=newHSSFWorkbook();HSSFSheetsheet=workbook.createSheet();HSSFRowrow=sheet.createRow((short)0);HSSFCellcell=row.createCell((short)0);sheet.setColumnWi...poi 一个单元格不同字体
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
sheet.setColumnWidth(0,(short)(381 * 40 ));
row.setHeight((short) (156.75 * 20 ));
cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_TOP); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行
cell.setCellStyle(cellStyle);
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 24); // 字体高度
font.setFontName("宋体"); // 字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
HSSFFont font1 = workbook.createFont();
font1.setFontHeightInPoints((short) 8); // 字体高度
font1.setFontName("宋体"); // 字体
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
HSSFRichTextString ts= new HSSFRichTextString(" 入库标签\r\n 物料名称:火灾报警探头探测器(含底座、过渡板;电气柜内安装)\r\n发运计划号:20130731-01-01\r\n\r\n 图号:CCDZ120A-220-001\r\n\r\nSAP物料号:CNR0000009938\r\n\r\n 项目名称:上海地铁6号线增购车\r\n\r\n 数量:100");
ts.applyFont(0,10,font);
ts.applyFont(10,ts.length(),font1);
cell.setCellValue(ts);
FileOutputStream fOut = new FileOutputStream("d:\\test.xls");
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println("文件生成...");
}
13418747879
38篇文章,6W+人气,0粉丝
-
(六)POI-操作Excel的poi的字体设置
2020-12-24 13:37:15原文链接:...import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.*;import java.io.FileOutputStream;...原文链接:https://blog.csdn.net/class157/article/details/92817286
package com.java.poi;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
/**
* @program: IdeaProjects
* @description:
* @author: Lxy
* @create: 2019-06-19 09:45
**/
public class Test7 {
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet页");
Row row=sheet.createRow(1);
//创建一个字体
Font font=wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("Courier New ");
font.setItalic(true);
font.setStrikeout(true);
CellStyle style=wb.createCellStyle();
style.setFont(font);
Cell cell = row.createCell((short) 1);
cell.setCellValue("this is test of fonts");
cell.setCellStyle(style);
FileOutputStream is=new FileOutputStream("D:\\3.xls");
wb.write(is);
is.close();
}
}
-
解决POI Excel设置自适应宽度,中文字体不起作用(两种方法)
2020-11-11 22:29:39解决POI Excel设置自适应宽度,中文字体不起作用 之前遇到的一个问题,发现了新的解决方法,记录一下,问题描述图: 一、原来的解决方案: 在设置自适应宽度后,将自适应后的宽度按 7/4 的比例重新设置,代码如下... -
poi 一个单元格不同字体,poi单元格字体
2021-01-14 09:24:24poi 一个单元格不同字体,poi单元格字体try {HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row = sheet.createRow((short)0);HSSFCell cell = row.createCell(... -
Apache POI Word - 字体样式和对齐方式
2020-12-24 13:37:13本章介绍如何使用Java在Word文档中应用不同的字体样式和对齐方式。 通常,字体样式包含:字体大小,类型,粗体,斜体和下划线。 对齐分为左,中,右,对齐。...import org.apache.poi.xwpf.usermodel.VerticalAlign... -
使用poi处理ppt 转为图片乱码问题解决
2020-12-24 13:37:16rtruns[l].setFontName("宋体"); } } Dimension pgsize = ppt.getPageSize(); BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.... -
poi使用方法
2013-03-06 20:56:14poi使用方法及介绍,是个很不错的导入导出excl很不错 -
Java POI HSLF PPT中文字体问题
2021-02-02 21:29:29最近项目中需要用到POI的HSLF。遇到一个很头疼的问题。生成PPT的中文字体全部都是宋体。设置字体根本不好用。英文字体没问题。publicstaticvoidmain(String[]args)throwsException{Sl...最近项目中需要用到POI的HSLF... -
java实现在线预览--poi实现word、excel、ppt转html
2019-07-31 18:39:43java实现在线预览- -之poi实现word、excel、ppt转html -
POI读写EXCEL文件
2020-12-24 13:37:12// 设置字体宋体 大小 加粗 HSSFFont columnHeadFont = workbook.createFont(); columnHeadFont.setFontName("宋体"); columnHeadFont.setFontHeightInPoints((short) 10); columnHeadFont.setBoldweight(HSSFFont.... -
Poi实现Excel导出
2021-09-15 22:04:19Poi实现Excel导出1. 导出Excel文件1.1 场景11.2 场景21.3 场景31.4 场景41.5 场景51.6 场景61.7 场景71.8 场景82. 公共方法2.1 下载EXCEL文件2.2 封装数据行2.3 封装标题行2.4 获取文件下载路径2.5 删除下载目录临时... -
POI生成word
2020-11-27 11:11:46POI生成word 项目中,经常需要导出word文件,而有些项目导出时没有模板,需要自动生成文件并导出。 在此背景下,本人调研了两个技术来自动生成:spiredoc和poi spiredoc的具体使用在我之前的一篇博客里具体讲到了,... -
java使用POI操作excel
2022-02-23 21:51:24在java掌握poi,操作excel或者是word 文章目录学习目标:一、POI 是什么?二、Excel包名称说明二、使用步骤1.引入库2.使用方式基础流程设置单元格(cell)样式设置单元格文本样式合并单元格单元格自动填充总结 一、... -
Java poi怎么导入
2021-02-12 23:18:32展开全部本篇面向32313133353236313431303231363533e59b9ee7ad9431333264643066对象为Java的初学者,从下载必备软件到创建一个空白的POI工程,已经熟练掌握环境搭建的请跳过此文。开发环境为windowsXP-SP2,Eclipse... -
POI写入xlsx文件
2022-01-12 21:15:16我们在开发中经常会遇到将信息打印到xlsx文件中的功能,可以使用POI来完成。 创建Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(); 创建工作表sheet HSSFSheet sheet = workbook.createSheet(); ... -
POI导出详解
2020-05-28 16:18:11最为常见的POI导出方式有3种:HSSF,XSSF,SXSSF 其中HSSF操作的是2003版本之前的Excel,扩展名是xls XSSFworkbook:操作Excel2007版本,扩展名为xlsx SXSSFworkbook :用于大数据量导出 使用HSSF导出的工具类 ... -
POI 字体Font
2017-09-29 11:32:27"宋体" ); // 设置文字为斜体 font1.setItalic( false ); // 使用水平删除线 font1.setStrikeout( true ); // 设置字体颜色为默认黑色 font1.setColor(Font.COLOR_RED); style.setFont(font1); /***... -
POI 技术实战
2018-08-14 00:43:43POI 技术如何实现对 Word 和 Excel 的读写操作?POI 技术相对其他同类型技术的优劣势又是哪些?怎样实现复杂的 Excel 读写操作?POI 对于 Word 和 Excel 有足够友好吗?这个 Chat,带领大家使用免费却实用的 POI ... -
Apache POI导入导出Excel
2021-12-22 13:39:38Apache POI原生导出 工具类 public class ExportExcelUtil { public static void createHeadTittle(XSSFWorkbook wb, XSSFSheet sheet, String headString, int col) { // 创建Excel工作表的行 XSSFRow row = ... -
POI 多图片导出
2022-01-18 15:55:21poi的多图片导出 -
poi操作word中英文问题
2021-04-20 17:16:38介绍使用poi对中英文使用不同字体的功能能 -
POI生成Word多级标题格式
2021-11-24 16:15:18一、使用模板文件生成标题 1、创建模板文件 新建一个doc文件,如format.doc,创建标题,如标题1、...XWPFDocument template = new XWPFDocument(new FileInputStream("D:\\test\\poi\\word\\format.docx")); // 获得模 -
关于POI的使用与小结
2022-01-26 11:13:11java中poi的使用 -
Java使用POI生成带验证数据的Excel(POI创建带下拉数据的单元格)
2022-02-14 10:48:42POI设置单元格下拉数据 -
使用POI生成文章标题
2021-09-24 13:30:02生成文章标题有两种方式,一种是自定义标题,还有一种是使用现有样式来生成,...org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> -
Apache-POI 设置excel单元格样式字体等
2018-10-30 18:23:05大概思路就是设置样式以及字体后添加进...baseFont.setFontName("宋体"); //大小 baseFont.setFontHeightInPoints((short)12); style.setFont(baseFont); //将样式设置应用具体单元格 cell.setCellStyle(style); -
POI 关于Word的使用
2021-09-09 16:02:21一、maven导入 <dependency>...org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency>