1
package test;
2
3
import java.io.FileOutputStream;
4
import java.io.IOException;
5
6
import org.apache.poi.hssf.usermodel.HSSFCell;
7
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
8
import org.apache.poi.hssf.usermodel.HSSFFont;
9
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
10
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
11
import org.apache.poi.hssf.usermodel.HSSFRow;
12
import org.apache.poi.hssf.usermodel.HSSFSheet;
13
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
14
15
public 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
}
60

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
