package test;

2

3import java.io.FileOutputStream;

4import java.io.IOException;

5

6import org.apache.poi.hssf.usermodel.HSSFCell;

7import org.apache.poi.hssf.usermodel.HSSFCellStyle;

8import org.apache.poi.hssf.usermodel.HSSFFont;

9import org.apache.poi.hssf.usermodel.HSSFPrintSetup;

10import org.apache.poi.hssf.usermodel.HSSFRichTextString;

11import org.apache.poi.hssf.usermodel.HSSFRow;

12import org.apache.poi.hssf.usermodel.HSSFSheet;

13import org.apache.poi.hssf.usermodel.HSSFWorkbook;

14

15public class ExcelTest {

16

17 public static void main(String[] args) throws IOException {

18

19 // create a new file

20 FileOutputStream out = new FileOutputStream("D:/workbook.xls");

21 // create a new workbook

22 HSSFWorkbook wb = new HSSFWorkbook();

23 // create a new sheet

24 HSSFSheet sheet = wb.createSheet();

25

26 //2.model

27 HSSFRow row = sheet.createRow(2);

28 row.setHeightInPoints(20);

29 HSSFCell cell = row.createCell(2);

30 HSSFFont cnFont = wb.createFont();

31 cnFont.setFontHeightInPoints((short) 10);

32 //font.setFontName("汉仪报宋简");

33 cnFont.setFontName("隶书");

34 HSSFCellStyle cnStyle = wb.createCellStyle();

35 cnStyle.setFont(cnFont);

36 cell.setCellStyle(cnStyle);

37 HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");

38 cell.setCellValue(richText);

39 HSSFCell enCell = row.createCell(3);

40 HSSFFont enFont = wb.createFont();

41 enFont.setFontHeightInPoints((short) 10);

42 enFont.setFontName("Arial Black");

43 HSSFCellStyle enStyle = wb.createCellStyle();

44 enStyle.setFont(enFont);

45 enCell.setCellStyle(enStyle);

46 enCell.setCellValue(new HSSFRichTextString("English font test"));

47 sheet.setColumnWidth(2, 4000);

48 sheet.setColumnWidth(3, 4000);

49

50 //3.output

51 sheet.setDisplayGridlines(false);

52 sheet.setPrintGridlines(false);

53 HSSFPrintSetup printSetup = sheet.getPrintSetup();

54 //A4纸

55 printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

56 wb.write(out);

57 out.close();

58 }

59}

HSSFSheet fromsheet = wb.getSheetAt(0); //模版页

for(int num=0;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标题栏如何在打印的时候保留到下一页_使用POI生成Excel文档并设置打印样式...