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
}