首页 > 代码库 > Java导出Excel

Java导出Excel

--创建Excel文件

技术分享
 1 /**
 2  * 导出表格
 3  * @author gongyg
 4  * @date 2017-4-7
 5  */
 6 public class ExcelCreate{
 7     
 8     public static void createExcel() {
 9         String url = null;
10         try
11         {
12             //创建表格文件
13             String filePath = "/CIRCExcel/"+String.format("%s", new SimpleDateFormat("yyyy/MM/dd").format(new Date()))+"/";
14             File path = new File(rootPath+filePath);
15             if(!path.exists() && !path.isDirectory())
16             {
17                 path.mkdirs();
18             }
19             String fileName =String.format("%s%s.xls", new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date()), Global.getInstance().UUID());
20             File file = new File(path,fileName);
21             if(!file.exists())
22             {
23                 file.createNewFile();
24             }
25             //创建工作薄
26             WritableWorkbook workbook =  Workbook.createWorkbook(file);
27             WritableSheet sheet = workbook.createSheet("第1页", 0);
28             
29             /**
30              * 定义单元格样式
31              */
32             WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
33                     jxl.format.Colour.BLACK);   //字体--大小--粗体--斜体--下划线--颜色
34             WritableCellFormat wcf_head = new WritableCellFormat(wf_head);//定义单元格
35             wcf_head.setBackground(jxl.format.Colour.WHITE);   //设置单元格背景色
36             wcf_head.setAlignment(jxl.format.Alignment.CENTRE);    //设置对齐方式
37             wcf_head.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //设置垂直居中
38             wcf_head.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);    //设置边框
39             
40             WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
41                     jxl.format.Colour.BLACK);   //字体--大小--粗体--斜体--下划线--颜色
42             WritableCellFormat wcf_table = new WritableCellFormat(wf_table);//定义单元格
43             wcf_table.setBackground(jxl.format.Colour.WHITE);   //设置单元格背景色
44             wcf_table.setAlignment(jxl.format.Alignment.CENTRE);    //设置对齐方式
45             wcf_table.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);    //设置边框
46             //设置列宽
47                 for(int i=0; i<18; i++) 
48                 {
49                     sheet.setColumnView(i, 8);
50                 }
51             //在Label对象的构造子中指名单元格位置是第一列第一行(0,0) 
52             //以及单元格内容为test 
53             Label title=new Label(0,0,"统计",wcf_title);
54             Label titleOne=new Label(0,1,"统计1",wcf_title1);
55             Label titleTwo=new Label(2,1,"统计2",wcf_title2);
56 
57             Label column1=new Label(0,2,"姓名",wcf_head1);
58             Label column2=new Label(1,2,"所选课程",wcf_head1);
59             
60             Label column3=new Label(2,2,"姓名",wcf_head2);
61             Label column4=new Label(3,2,"所选课程",wcf_head2);
62 
63 
64 
65             //或者WritableCell cell =  new jxl.write.Number(column, row, value, wcf)
66             //将定义好的单元格添加到工作表中 
67             sheetOne.addCell(title); 
68             sheetOne.addCell(titleOne); 
69             sheetOne.addCell(titleTwo); 
70 
71             sheetOne.addCell(column1); 
72             sheetOne.addCell(column2); 
73             sheetOne.addCell(column3); 
74             sheetOne.addCell(column4); 
75 
76 
77             //合: 第1列第1行  到 第13列第1行
78             sheetOne.mergeCells(0, 0, 3, 0); 
79             sheetOne.mergeCells(0, 1, 1, 1); 
80             sheetOne.mergeCells(2, 1, 3, 1); 
81             //写入数据并关闭文件 
82             book.write(); 
83             book.close(); 
84             url = String.format("%s%s", filePath, fileName);
85         }
86         catch (Exception e) {
87            e.printStackTrace();
88         }
89         return url;
90     }
91 }
点击 + 展开

--js端调用,页面可下载

window.location.href = http://www.mamicode.com/url;

Java导出Excel