Java web Excel导出数据

需求:将页面查询的数据导出到Excel中,页面查询的数据也是从后台查询的,所以向Excel 中插入数据时,不需要从页面获取数据,直接从后台把数据插入到excel中


对Excel操作的公共类


package util;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.loushang.next.data.DataSet;
import org.loushang.next.data.Record;
import jxl.Workbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {
//列明
private List<String> cell = null;
//列值
private List<String> cellList = null;
//shell名
private String sheet = "";
//excel路径
private String path = "";

public void setPath(String s){
path = s;
}
public void setCell(ArrayList ce){
cell = (List<String>) ce.clone();
}
public void setShell(String sh){
sheet = sh;
}
public void setCellList(ArrayList<String> list){
cellList = (List<String>) list.clone();
}
public  void  writeExcelBo(String fos, DataSet ds)
   {
    jxl.write.WritableWorkbook wwb;
    try
    {
     wwb= Workbook.createWorkbook(new File(fos));
     jxl.write.WritableSheet ws= wwb.createSheet(sheet, 10);
     for(int k=0;k<cell.size();k++){
     ws.addCell(new jxl.write.Label(k, 0, cell.get(k)));
     }
    
     int dscount=ds.getCount();
     Record record = new Record(); 
     for (int i= 0; i < dscount; i++)
     {
     record= ds.getRecord(i);
     for(int j=0;j<cellList.size();j++){
     ws.addCell(new jxl.write.Label(j, i + 1, "" + record.get(cellList.get(j)))); 
     }
      
     }
    // jxl.write.WritableFont wfc=
     //new jxl.write.WritableFont(WritableFont.ARIAL,255,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
     //jxl.write.WritableCellFormat wcfFC= new jxl.write.WritableCellFormat(wfc);
     //ws.addCell(new jxl.write.Label(0, 0, "2007年07月即将上市新书!"));
     wwb.write();
     // 关闭Excel工作薄对象
     wwb.close();
    } catch (IOException e){
    } catch (RowsExceededException e){
     
    } catch (WriteException e){
    }
   }
}


向Excel中插入数据


public void query(){
/*
* 先从数据库中读取数据,再把数据插入到excel中
* rcqjl
* */
ReadProperties rp = new ReadProperties();
// Properties p = rp.readProperties("/config.properties");
String path = rp.getProperties("/config.properties","Excel.path");

File file = new File(path+"历史水量.xls");
ArrayList<String> cell = new ArrayList<String>();
cell.add("监测站代码");
cell.add("监测站名称");
cell.add("时间");
cell.add("取排水流量(m³/h)");
cell.add("累计水量(m³)");
ArrayList<String> cellList = new ArrayList<String>();
cellList.add("mpCd");
cellList.add("mpNm");
cellList.add("tm");
cellList.add("mpQ");
cellList.add("accW");
DataSet ds = new DataSet();                
ParameterSet pset = new ParameterSet();
String ps =  (String) getParameter("pse");
String[] pse = ps.split(",");

if(!pse[0].equals("")||pse[0]!=null){
pset.setParameter("WR_MP_Q_R.MP_CD", pse[0]);
}
pset.setParameter("WR_MP_Q_R.TM@>", pse[1]);
pset.setParameter("WR_MP_Q_R.TM@<", pse[2]);
String addvcds=getQyshStcd();
pset.setParameter("WR_MP_Q_R.MP_CD@in", addvcds);
ds = wrMpQRDomain.query(pset);
 Record record = new Record();
 if(ds.getCount()>0){
  for(int i = 0;i<ds.getCount();i++){
   record = ds.getRecord(i);
   record.set("mpNm", pse[3]);
   Timestamp ts =  (Timestamp)record.get("tm");
   String tsStr = "";   
   DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
   try {   
   tsStr = sdf.format(ts);
   } catch (Exception e) {   
    e.printStackTrace();   
   }
   //record.set("showTime", tsStr);
   record.set("tm", tsStr);
   //dss.add(record);
  }
  
 }
 //向excel中插入数据
 WriteExcel writeExcel = new WriteExcel();
 writeExcel.setCell(cell);
 writeExcel.setCellList(cellList);
 writeExcel.setShell("历史水量");
 if (file.isFile() && file.exists()) {  
       file.delete();  
   }  
 //D:\solr\rcqjl
 writeExcel.writeExcelBo(path+"历史水量.xls", ds);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值