package poi_test;

import java.io.FileOutputStream;
import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelTest { public static void main(String[] args) throws IOException {
//新建文件输出流对象
FileOutputStream out = new FileOutputStream("F:/poitest.xls");
//新建workbook
HSSFWorkbook wb = new HSSFWorkbook();
//新建sheet
HSSFSheet sheet = wb.createSheet();
//新建行
HSSFRow row = sheet.createRow(2);
//设置行高
row.setHeightInPoints(20);
//新建单元格
HSSFCell cell = row.createCell(2); //创建整个文本的字体对象,workbook创建
HSSFFont cnFont = wb.createFont();
//设置字体行高,字体名字
cnFont.setFontHeightInPoints((short)10);
cnFont.setFontName("隶书"); //将文本字面格式用到单元格上,新建单元格风格
HSSFCellStyle cnStyle = wb.createCellStyle();
cnStyle.setFont(cnFont);
cell.setCellStyle(cnStyle); //单元格内文本对象新建,HSSFRichTextString的应用
HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");
cell.setCellValue(richText); //再建一个单元格,重复上面的设置
HSSFCell enCell = row.createCell(3);
HSSFFont enFont = wb.createFont();
enFont.setFontHeightInPoints((short) 10);
enFont.setFontName("Arial Black");
HSSFCellStyle enStyle = wb.createCellStyle();
enStyle.setFont(enFont);
enCell.setCellStyle(enStyle);
enCell.setCellValue(new HSSFRichTextString("English font test"));
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000); //输出
//设置边框
sheet.setDisplayGridlines(false);
//设置打印的边框
sheet.setPrintGridlines(false); //设置打印对象
HSSFPrintSetup printSetup = sheet.getPrintSetup();
//设置页边距
printSetup.setHeaderMargin((double) 0.44); // 页眉
printSetup.setFooterMargin((double) 0.2);//页脚 //设置页宽
printSetup.setFitWidth((short)1);
printSetup.setFitHeight((short)1000); //设置打印方向,横向就是true
printSetup.setLandscape(true);
//设置A4纸
printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //打印,关闭流
wb.write(out);
out.close(); System.out.println("ok");
} }
//以下为转载
HSSFSheet fromsheet = wb.getSheetAt(0); //模版页
for(int num=0;num<addSheetNum;num++)//新增
{
String numStr = String.valueOf(num+2); HSSFSheet newsheet = wb.createSheet("第"+numStr+"页");
//设置打印参数
newsheet.setMargin(HSSFSheet.TopMargin,fromsheet.getMargin(HSSFSheet.TopMargin));// 页边距(上)
newsheet.setMargin(HSSFSheet.BottomMargin,fromsheet.getMargin(HSSFSheet.BottomMargin));// 页边距(下)
newsheet.setMargin(HSSFSheet.LeftMargin,fromsheet.getMargin(HSSFSheet.LeftMargin) );// 页边距(左)
newsheet.setMargin(HSSFSheet.RightMargin,fromsheet.getMargin(HSSFSheet.RightMargin));// 页边距(右 HSSFPrintSetup ps = newsheet.getPrintSetup();
ps.setLandscape(false); // 打印方向,true:横向,false:纵向(默认)
ps.setVResolution((short)600);
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型 SheetFunc.copyRows(wb, 0, num+1,0 , 46, 0);//复制
wb.getSheetAt(num+1).setColumnWidth((short)0, (short)2400);//256,31.38
}

【Java】常用POI生成Excel文档设置打印样式的更多相关文章

  1. Java之Poi导出Excel文档

    一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...

  2. java操作poi生成excel.xlsx(设置下拉框)下载本地和前端下载

    需求:导入excel表格,如果excel有错误,将错误的地方标红,在把数据以excel的形式写出,供用户下载解决方案:1.以实体类的方式接收excel并解析(创建两个集合一个接收正常的数据一个接收错误 ...

  3. C# NPOI生成Excel文档(简单样式)

    效果图: 代码:  /// <summary> /// 导出Excel /// </summary> /// <param name="DeptId" ...

  4. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  5. POI生成WORD文档

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  6. Java 利用 poi 生成 Excel文件的通用例子

    在用java 写数据库应用的时候, 通常会生成各种报表,而这些报表可能会被导出为各种格式的文件,比如Excel文档,pdf 文档等等. 今天先做了一个生成Excel 文档的例子,主要解决以下问题: 生 ...

  7. 后台生成EXCEL文档,自定义列

    后台生成EXCEL文档,自定义列 //response输出流处理 //设置编码.类型.文件名 getResponse().reset(); getResponse().setCharacterEnco ...

  8. php用PHPExcel库生成Excel文档的例子

    <?php require_once '../libs/PHPWord/PHPWord.php'; require_once '../libs/PHPWord/PHPWord/IOFactory ...

  9. NPOI 2.1.1 系列(2) 使用NPOI读取List或者datatable数据生成 Excel文档 ;Npoi生成 xlsx 2007以上文档

    结合上一篇文章  NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx ...

随机推荐

  1. 成都Uber优步司机奖励政策(4月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. day6 网络 HTML模板

    1.HTML模板 HTML模板 baidu一下 http://www.cssmoban.com/ http://www.cnblogs.com/web-d/archive/2010/04/16/171 ...

  3. 在sql server 中查找一定时间段内访问数据库情况

    total_worker_time AS [总消耗CPU 时间(ms)], execution_count [运行次数], qs.total_worker_time AS [平均消耗CPU 时间(ms ...

  4. php编程知识点2018

    一 .PHP基础部分 1.PHP语言的一大优势是跨平台,什么是跨平台? PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配 ...

  5. 【转】ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.165' (113)

    原文转自:http://blog.csdn.net/chengyuqiang/article/details/54285857 1.程序报错: com.mysql.jdbc.exceptions.jd ...

  6. java阻塞队列之LinkedBlockingQueue

    LinkedBlockingQueue是BlockingQueue中的其中一个,其实现方式为单向链表,下面看其具体实现.(均为JDK8) 一.构造函数 在LinkedBlockingQueue中有三个 ...

  7. C# webapi 路由规则和接收数据

    1:新建的web api项目 默认的访问api方式:  (get,post,delect,put)  api+控制器  以Post为例子 post提交单个参数: 接收方法  post提交多个参数  接 ...

  8. scrapy 爬取知乎问题、答案 ,并异步写入数据库(mysql)

      python版本  python2.7 爬取知乎流程: 一 .分析 在访问知乎首页的时候(https://www.zhihu.com),在没有登录的情况下,会进行重定向到(https://www. ...

  9. json简单操作

    通过内置的json模块对json数据进行编码 1.对数据进行编码(dumps) import json #使用dumps将python数据结构转换为json data = { , "name ...

  10. 深入了解MySQL存储索引

    (一)关于存储引擎 创建合适的索引是SQL性能调优中最重要的技术之一.在学习创建索引之前,要先了解MySql的架构细节,包括在硬盘上面如何组织的,索引和内存用法和操作方式,以及存储引擎的差异如何影响到 ...