java将List中的数据导入到excel表中

/*package com.dqd.shop.user.action;

import java.util.List;

import com.dqd.shop.user.vo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GetDataAction extends ActionSupport{
	public String getData(){
		List<User> list =(List<User>) ActionContext.getContext().getSession().get("data");
		for(User u:list){
			System.out.println(u+"---");
		}
		return NONE;
	}
}
*/
package com.dqd.shop.user.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.apache.commons.lang3.math.NumberUtils;

import com.dqd.shop.user.vo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Excel导入导出
 * 
 * @author dqd
 * @version 1.0 Feb 7, 2014 4:14:51 PM
 */
public class GetDataAction extends ActionSupport{



    /**
     * 导出(导出到磁盘)
     */
  
    public void exportExcel() {
        WritableWorkbook book = null;
        try {
            // 打开文件
            book = Workbook.createWorkbook(new File("D:/test/测试.xls"));
            // 生成名为"学生"的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet("学生", 0);
            // 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
            Label label = new Label(0, 0, "张三");
            // 将定义好的单元格添加到工作表中
            sheet.addCell(label);
            // 保存数字的单元格必须使用Number的完整包路径
            jxl.write.Number number = new jxl.write.Number(1, 0, 30);
            sheet.addCell(number);
            // 写入数据并关闭文件
            book.write();
        } catch (Exception e) {
            System.out.println(e);
        }finally{
            if(book!=null){
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
    }
    
    /**
     * 对象数据写入到Excel
     */
    
    public String  getData() {
        WritableWorkbook book = null;
        try {
            // 打开文件
            book = Workbook.createWorkbook(new File("D:/text/stu.xls"));
            // 生成名为"学生"的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet("证券数据", 0);
            
            List<User> list =(List<User>) ActionContext.getContext().getSession().get("data");
            if(list!=null && !list.isEmpty()){
                for(int i=0; i<list.size(); i++){
                   // sheet.addCell(new Label(0, i, String.valueOf(list.get(i).getUid())));
                    sheet.addCell(new Number(0, i, list.get(i).getUnumber()));
                    sheet.addCell(new Label(1, i, String.valueOf(list.get(i).getUtime())));
                    sheet.addCell(new Label(2, i, String.valueOf(list.get(i).getUcount())));
                }
            }
            
            // 写入数据并关闭文件
            book.write();
        } catch (Exception e) {
            System.out.println(e);
        }finally{
            if(book!=null){
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
		return NONE;
    
    }
   
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值