JXL导出Excel(只支持xls版本)——(一)

注意:

  导出的后缀是xls可以直接打开,如果导出的后缀是xlsx打开报错,需要手动将名字改为xls才可以打开。也就是JXL不可以导出xlsx的excel。

Jar包

Java代码

 1 package JXL;
 2 import java.io.File;
 3 import java.io.IOException;
 4 
 5 
 6 import jxl.Workbook;
 7 import jxl.write.Label;
 8 import jxl.write.WritableSheet;
 9 import jxl.write.WritableWorkbook;
10 
11 /**
12  * JXL导出excel
13 * @author: qlq
14 * @date :  2017年7月14日下午9:08:40
15 * @description:
16  */
17 public class JXLExcel {
18 
19     public static void main(String[] args) throws Exception {
20 //        数组用于存放标题
21         String[] title = {"id","name","sex"};
22         File file = new File("E:/JXL_TEST.xls");
23         try {
24             file.createNewFile();
25 //            创建工作簿
26             WritableWorkbook workbook  = Workbook.createWorkbook(file);
27 //            创建sheet页,0代表索引
28             WritableSheet sheet = workbook.createSheet("sheet 1", 0);
29 //            在第一行列入列的名称
30             Label label=null;
31             for(int i=0;i<title.length;i++){
32 //                i代表第几列,0代表第一行。后面是插入的数据
33                 label = new Label(i,0,title[i]);
34                 sheet.addCell(label);
35             }
36 //            插入数据
37             for(int i=1;i<10;i++){
38 //                第一个参数是列号。第二个是行号
39                 label = new Label(0,i,"a"+i);
40                 sheet.addCell(label);
41                 label = new Label(1,i,"user"+i);
42                 sheet.addCell(label);
43                 label = new Label(2,i,"男");
44                 sheet.addCell(label);
45             }
46 //            写入数据
47             workbook.write();
48             workbook.close();
49             } catch (IOException e) {
50             // TODO Auto-generated catch block
51             e.printStackTrace();
52         }
53     }
54 }

结果:

原文地址:https://www.cnblogs.com/qlqwjy/p/7271612.html