/*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; } }
新闻热点
疑难解答