您的位置:首页 > 编程语言 > Java开发

Java web Excel导出数据

2016-06-27 10:57 483 查看
需求:将页面查询的数据导出到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
    {<
4000
br />     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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: