要往xls文件里面写入数据的时候需要注意的是第一要新建一个xls文件
OutputStream os=new FileOutputStream("c:\\excel2.xls");
再建完这个文件的时候再建立工作文件
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(os));
如果这个文件已经存在,那么我们可以在这个文件里面加入一个sheet为了和以前的数据进行分开;
jxl.write.WritableSheet ws = wwb.createSheet("TestSheet 1" , 0);
在createSheet方法里前面的参数是sheet名,后面是要操作的sheet号
接下来就可以往这个文件里面写入数据了
写入数据的时候注意的格式
(1)添加的字体样式
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18,WritableFont.BOLD, true);
WritableFont()方法里参数说明:
这个方法算是一个容器,可以放进去好多属性
第一个: TIMES是字体大小,他写的是18
第二个: BOLD是判断是否为斜体,选择true时为斜体
第三个: ARIAL
第四个: UnderlineStyle.NO_UNDERLINE 下划线
第五个: jxl.format.Colour.RED 字体颜色是红色的
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelC = new jxl.write.Label(0, 0,"This is a Label cell" ,wcfF);
ws.addCell(labelC);
在Label()方法里面有三个参数
第一个是代表列数,
第二是代表行数,
第三个代表要写入的内容
第四个是可选项,是输入这个label里面的样式
然后通过写sheet的方法addCell()把内容写进sheet里面。
(2)添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
(3)添加Number对象
(3.1)显示number对象数据的格式
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
Number()方法参数说明:
前两上表示输入的位置
第三个表示输入的内容
(4)添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
(5)添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, newjava.util.Date());
ws.addCell(labelDT);
DateTime()方法的参数说明
前两个表示输入的位置
第三个表示输入的当前时间
(6)添加带有formatting的DateFormat对象
这个显示当前时间的所有信息,包括年月日小时分秒
jxl.write.DateFormat df = new jxl.write.DateFormat("ddMM yyyy hh:mm:ss" );
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
(7)添加带有字体颜色Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
import="jxl.format.*
jxl.write.WritableFont wfc = newjxl.write.WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
(8)设置单元格样式
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackGround(jxl.format.Colour.RED);//设置单元格的颜色为红色
wcfFC = new jxl.write.Label(6,0,"i lovechina" ,wcfFC);
设置列宽: cw_sheet.setColumnView(0,100);//第一个参数为第几列,第二个参数为列宽
设置行高: cw_sheet.setRowView(0, 1000);//第一个参数为第几行,第二个参数为行高
以下是部分例子:
1. import jxl.*;
2. importjxl.write.*;
3. importjava.io.*;
4. importjava.io.File.*;
5. importjava.util.*;
6.
7.
8.
9. public classexcel
10. {
11. public static void main(String[] args)
12. {
13.
14. String targetfile = "c:/out.xls" ;//输出的excel文件名
15. String worksheet = "List"; //输出的excel文件工作表名
16. String[] title = {"ID","NAME", "DESCRIB" }; //excel工作表的标题
17.
18.
19. WritableWorkbook workbook;
20. try
21. {
22. //创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下
23. //workbook = Workbook.createWorkbook(newFile("output.xls"));
24. System.out.println("begin");
25.
26. OutputStream os=new FileOutputStream(targetfile);
27. workbook=Workbook.createWorkbook(os);
28.
29. WritableSheet sheet = workbook.createSheet(worksheet,0 );//添加第一个工作表
30. //WritableSheet sheet1 =workbook.createSheet("MySheet1", 1); //可添加第二个工作
31. /*
32. jxl.write.Label label = newjxl.write.Label(0, 2, "A label record"); //put a label in cell A3,Label(column,row)
33. sheet.addCell(label);
34. */
35.
36. jxl.write.Label label;
37. for ( int i= 0 ; i<title.length; i++)
38. {
39. //Label(列号,行号 ,内容 )
40. label = new jxl.write.Label(i, 0 , title[i]);//put the title in row1
41. sheet.addCell(label);
42. }
43.
44.
45.
46.
47. //下列添加的对字体等的设置均调试通过,可作参考用
48.
49.
50. //添加数字
51. jxl.write.Number number = new jxl.write.Number( 3 , 4, 3.14159 );//put the number 3.14159 in cell D5
52. sheet.addCell(number);
53.
54. //添加带有字型Formatting的对象
55. jxl.write.WritableFont wf = newjxl.write.WritableFont(WritableFont.TIMES, 10 ,WritableFont.BOLD, true );
56. jxl.write.WritableCellFormat wcfF = newjxl.write.WritableCellFormat(wf);
57. jxl.write.Label labelCF = new jxl.write.Label( 4 , 4 ,"文本",wcfF);
58. sheet.addCell(labelCF);
59.
60. //添加带有字体颜色,带背景颜色 Formatting的对象
61. jxl.write.WritableFont wfc = newjxl.write.WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD, false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
62. jxl.write.WritableCellFormat wcfFC = newjxl.write.WritableCellFormat(wfc);
63. wcfFC.setBackground(jxl.format.Colour.BLUE);
64. jxl.write.Label labelCFC = new jxl.write.Label( 1 , 5,"带颜色",wcfFC);
65. sheet.addCell(labelCFC);
66.
67. //添加带有formatting的Number对象
68. jxl.write.NumberFormat nf = newjxl.write.NumberFormat("#.##");
69. jxl.write.WritableCellFormat wcfN = newjxl.write.WritableCellFormat(nf);
70. jxl.write.Number labelNF = new jxl.write.Number( 1 , 1, 3.1415926 ,wcfN);
71. sheet.addCell(labelNF);
72.
73. //3.添加Boolean对象
74. jxl.write.Boolean labelB = new jxl.write.Boolean( 0 ,2 , false );
75. sheet.addCell(labelB);
76.
77. //4.添加DateTime对象
78. jxl.write.DateTime labelDT = new jxl.write.DateTime( 0, 3 , new java.util.Date());
79. sheet.addCell(labelDT);
80.
81. //添加带有formatting的DateFormat对象
82. jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");
83. jxl.write.WritableCellFormat wcfDF = newjxl.write.WritableCellFormat(df);
84. jxl.write.DateTime labelDTF = new jxl.write.DateTime(1 , 3 , new java.util.Date(),wcfDF);
85. sheet.addCell(labelDTF);
86.
87. //和宾单元格
88. //sheet.mergeCells(int col1,int row1,int col2,introw2);//左上角到右下角
89. sheet.mergeCells(4 , 5 , 8 , 10 );//左上角到右下角
90. wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40 ,WritableFont.BOLD, false ,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
91. jxl.write.WritableCellFormat wchB = newjxl.write.WritableCellFormat(wfc);
92. wchB.setAlignment(jxl.format.Alignment.CENTRE);
93. labelCFC = new jxl.write.Label( 4 , 5 ,"单元合并",wchB);
94. sheet.addCell(labelCFC); //
95.
96.
97. //设置边框
98. jxl.write.WritableCellFormat wcsB = newjxl.write.WritableCellFormat();
99. wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THICK);
100. labelCFC = new jxl.write.Label( 0 , 6 ,"边框设置",wcsB);
101. sheet.addCell(labelCFC);
102. workbook.write();
103. workbook.close();
104. }catch (Exception e)
105. {
106. e.printStackTrace();
107. }
108. System.out.println("end");
109. Runtime r=Runtime.getRuntime();
110. Process p=null ;
111. //Stringcmd[]={"notepad","exec.java"};
112. String cmd[]={"C:\\ProgramFiles\\Microsoft Office\\Office\\EXCEL.EXE","out.xls" };
113. try {
114. p=r.exec(cmd);
115. }
116. catch (Exception e){
117. System.out.println("errorexecuting: "+cmd[ 0 ]);
118. }
119.
120.
121. }
122. }