使用POI生成Excel文档并设置打印样式

http://blog.youkuaiyun.com/giianhui/article/details/7935090

package 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 {
        
         // create a new file  
         FileOutputStream out = new FileOutputStream("D:/workbook.xls");  
         // create a new workbook  
         HSSFWorkbook wb = new HSSFWorkbook();  
         // create a new sheet  
         HSSFSheet sheet = wb.createSheet();  
           
         //2.model  
         HSSFRow row = sheet.createRow(2);  
         row.setHeightInPoints(20);  
         HSSFCell cell = row.createCell(2);  
         HSSFFont cnFont = wb.createFont();  
         cnFont.setFontHeightInPoints((short) 10);  
         //font.setFontName("汉仪报宋简");  
         cnFont.setFontName("隶书");  
         HSSFCellStyle cnStyle = wb.createCellStyle();  
         cnStyle.setFont(cnFont);  
         cell.setCellStyle(cnStyle);  
         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);
         
         //3.output  
         sheet.setDisplayGridlines(false);  
         sheet.setPrintGridlines(false);  
         HSSFPrintSetup printSetup = sheet.getPrintSetup();  
         //A4纸
         printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);  
         wb.write(out);  
         out.close(); 
    }
}

 

 

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
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值