要用到poi-2.5.1-final-20040804.jar包
?1
import
?java.io.FileOutputStream;
?2
import
?java.sql.Connection;
?3
import
?java.sql.ResultSet;
?4
import
?java.sql.ResultSetMetaData;
?5
?6
import
?org.apache.poi.hssf.usermodel.HSSFCell;
?7
import
?org.apache.poi.hssf.usermodel.HSSFRow;
?8
import
?org.apache.poi.hssf.usermodel.HSSFSheet;
?9
import
?org.apache.poi.hssf.usermodel.HSSFWorkbook;
10
import
?org.dom4j.Document;
11
import
?org.dom4j.Element;
12
13
import
?wufalong.exportServer.ConnectBean;
14
import
?wufalong.exportServer.CreateConnection;
15
import
?wufalong.exportServer.GetResultSet;
16
import
?wufalong.exportServer.LoadRootElementByConfigFile;
17
import
?exportxls.JsUserBasicData;
18
19
public
?
class
?Person20070517
20
{
21
??
public
?
static
?
void
?main(String?args[])?
22
??
{
23
??????Person20070517?c
=
new
?Person20070517();
24
??????
int
?allcount
=
0
;
25
??????allcount
=
allcount
+
c.createPersonMobile(
"
c:/人才.xls
"
);
26
??}
27
??
28
??
public
?
int
?createPersonMobile(String?filename)
29
??
{
30
??????Connection?con?
=
?
null
;
31
??????ResultSet?rs?
=
?
null
;
32
??????Document?doc?
=
?LoadRootElementByConfigFile.getRootElement();
33
??????Element?e?
=
?doc.getRootElement().element(
"
import
"
);
34
??????
try
35
??????
{
36
????????????con?
=
?CreateConnection.getConnect(e);
37
????????????rs?
=
?GetResultSet.getResule(con,ConnectBean.getSql(e));
38
??????????FileOutputStream?fOut?
=
?
null
;
39
??????????HSSFWorkbook?workbook?
=
?
new
?HSSFWorkbook();
40
??????????HSSFSheet?sheet?
=
?workbook.createSheet();
41
??????????HSSFRow?row?
=
?
null
;
42
??????????HSSFCell?cell?
=
?
null
;
43
??????????
int
?i?
=
?
1
;
44
??????????
int
?writeadd?
=
?
0
;
45
??????????String??cellvalue?
=
?
""
;
46
??????????JsUserBasicData?jub?
=
?
new
?JsUserBasicData();
47
??????????row?
=
?sheet.createRow(
0
);
48
??????????cell?
=
?row.createCell((
short
)?
0
);
49
??????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
50
??????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
51
??????????cell.setCellValue(
"
姓名
"
);
52
??????????cell?
=
?row.createCell((
short
)?
1
);
53
??????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
54
??????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
55
??????????cell.setCellValue(
"
手機
"
);
56
??????????
57
??????????
while
?(rs.next())?
{
58
59
??????????????System.out.println(
"
正在生成第
"
?
+
?i?
+
?
"
條記錄
"
);
60
??????????????ResultSetMetaData?rsmd?
=
?rs.getMetaData();
61
??????????????row?
=
?sheet.createRow((
short
)?i);
62
??????????????
int
?colcount?
=
?rsmd.getColumnCount();
63
??????????????
for
?(
int
?icc?
=
?
0
;?icc?
<
?colcount;?icc
++
)?
{
64
??????????????????cellvalue?
=
?rs.getString(icc?
+
?
1
);?
65
??????????????????cell?
=
?row.createCell((
short
)?icc);
66
??????????????????System.out.println(
"
md:
"
?
+
?cellvalue);?
67
??????????????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
68
??????????????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
69
??????????????????cell.setCellValue(cellvalue);
70
??????????????}
71
??????????????i
++
;
72
??????????}
73
74
??????????fOut?
=
?
new
?FileOutputStream(filename);
75
??????????workbook.write(fOut);
76
??????????fOut.flush();
77
??????????fOut.close();
78
??????????System.out.println(filename?
+
?
"
文件生成
共
"
?
+
?i?
+
?
"
條
"
);
79
??????????rs.close();
80
??????????rs?
=
?
null
;
81
??????????con.close();
82
??????????con?
=
?
null
;
83
??????????
return
?i;
84
??????}
?
catch
?(Exception?ee)?
85
??????
{
86
??????????????System.out.println(
"
發生了
"
?
+
?ee.getMessage()?
+
?
"
例外
"
);
87
??????}
?
88
??????
finally
?
89
??????
{
90
??????????????GetResultSet.closeResultSet(rs);
91
??????????????CreateConnection.closeConnect(con);
92
??????}
93
?????
return
?
0
;
94
??}
95
}
96

?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

61

62

63



64

65

66

67

68

69

70

71

72

73

74

75

76

77

78


79

80

81

82

83

84

85



86

87

88

89



90

91

92

93

94

95

96
