1 package poi; 2 3 import java.io.File; 4 import jxl.Workbook; 5 import jxl.write.Label; 6 import jxl.write.WritableSheet; 7 import jxl.write.WritableWorkbook; 8 /* 9 * poi 10 * jxl 创建excel文件,并且添加数据 11 * */ 12 public class JxlExpExcel { 13 14 public static void main(String[] args) { 15 //表头 16 String[] title = {"name", "age", "sex"}; 17 //创建excel文件 18 File file = new File("e:/jxl.xls"); 19 try { 20 file.createNewFile(); 21 //创建工作薄 22 WritableWorkbook workbook = Workbook.createWorkbook(file); 23 //创建sheet 24 WritableSheet sheet = workbook.createSheet("sheet1", 0); 25 Label label= null; 26 //填写表头 27 for (int i = 0; i < title.length; i++) { 28 //label参数 列,行,内容 29 label = new Label(i, 0, title[i]); 30 //将内容填写到单元格中 31 sheet.addCell(label); 32 } 33 //添加数据 34 for (int i = 1; i < 10; i++) { 35 label = new Label(0, i, "user"+i ); 36 sheet.addCell(label); 37 label = new Label(1, i, 12+i+"" ); 38 sheet.addCell(label); 39 label = new Label(2, i, "M" ); 40 sheet.addCell(label); 41 label = new Label(3, i, "d" ); 42 sheet.addCell(label); 43 44 } 45 //写入数据 46 workbook.write(); 47 //关闭工作薄 48 workbook.close(); 49 50 51 } catch (Exception e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 56 } 57 58 }