java用jxl实现导出execl表格

//先将需要导出的数据放到list中

//然后将list中的数据放到execl表中

 1 @RequestMapping(params="exportExecl")
 2     public String exportExecl( HttpServletRequest request) throws IOException{
 3         List<Lanmu> list=new ArrayList<Lanmu>();
 4         list=lanmuBiz.findLmxx();
 5         int sure = ExcelUtils.createExcel(list,request);//生成execl
 6         if(sure==200){
 7             File file=new File("F://myeclipse+tomcat+jdk//apache-tomcat-7.0.26//webapps//Manage_ssm//exportLanmuExcel");
 8             Desktop.getDesktop().open(file);//打开文件所在位置
 9             return "redirect:lmxx.do?findLmxx";
10             
11         }else{
12             return "view/luntan/errorExcel";
13         }
14     }
15     
 1 public class ExcelUtils {
 2     
 3     private static final int SUCCESS = 200;
 4     private static final int FAIL = 500;
 5     
 6     public static int createExcel(List<Lanmu> list, HttpServletRequest req) {
 7         try {
 8             String path = "/exportLanmuExcel";
 9             path = req.getSession().getServletContext().getRealPath(path);
10     
11             File file  = new File(path);
12             if(!file.exists()){
13                 file.mkdirs();
14             }
15             OutputStream os = new FileOutputStream(path + "/" + "lanmuList" + ".xls");
16             //创建工作薄
17             WritableWorkbook workbook = Workbook.createWorkbook(os);
18             //创建新的一页
19             WritableSheet sheet = workbook.createSheet("First Sheet", 0);
20             //构造表头
21             sheet.mergeCells(0, 0, 2, 0);//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
22             WritableFont bold = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
23             WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一个单元格样式控制对象
24             titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
25             titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中
26             Label title = new Label(0,0,"lanmuList",titleFormate);
27             sheet.setRowView(0, 600, false);//设置第一行的高度
28             sheet.addCell(title);           
29             //设置每一列的宽度
30             for(int i=0;i<=2;i++){
31                  sheet.setColumnView(i, 15);
32             }
33      
34             //创建要显示的具体内容
35             WritableFont color = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//选择字体
36             WritableCellFormat colorFormat = new WritableCellFormat(color);
37             Label a1 = new Label(0,1,"序号",colorFormat);
38             sheet.addCell(a1);
39             Label a2 = new Label(1,1,"栏目名称",colorFormat);
40             sheet.addCell(a2);
41             Label a3 = new Label(2,1,"栏目状态",colorFormat);
42             sheet.addCell(a3);
43            
44             //获取数据,将数据赋值到execl表格中
45             for (int i = 2; i < list.size()+2; i++) {
46                 Lanmu ap = list.get(i-2);
47                 int xh = i-2; 
48                 String name = ap.getName(); 
49                 String state=ap.getState();
50                 if("1".equals(state)){
51                     state = "普通用户栏目";
52                 }else{
53                     state = "管理员栏目";
54                 }
55     
56                 // 创建第一个sheet
57                 Label b1 = new Label(0,i,xh+"");
58                 sheet.addCell(b1);
59                 Label b2 = new Label(1,i,name);
60                 sheet.addCell(b2);
61                 Label b3 = new Label(2,i,state);
62                 sheet.addCell(b3);
63               
64             
65             }
66             //把创建的内容写入到输出流中,并关闭输出流
67             workbook.write();
68             workbook.close();
69             os.close();
70             return SUCCESS;
71         } catch (Exception e) {
72             e.printStackTrace();
73             return FAIL;
74         }
75         
76     }
77 
78 }
原文地址:https://www.cnblogs.com/xiaotian-222/p/6952614.html