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);
}
对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);
}
相关文章推荐
- leetcode-java-343. Integer Break
- 经典算法体会之冒泡排序
- Spring bean注入方式
- java WEB 配置文件的读取
- Java web复习要点
- Hikari java数据库连接池实战
- Java String.format() 和 Java的日期格式化常用方法
- Java的异常处理机制
- eclipse报错
- Java swing使用皮肤后出现Component creation must be done on Event Dispatch Thread错误解决方法
- Java基础之内部类(1)
- Java NIO Scatter / Gather
- Java中如何遍历Map对象的4种方法
- myeclipse如何添加源码反编译工具插件
- 在Eclipse中生成接口的JUnit测试类
- springMVC的拦截器
- Java UDP 多用户 组播 聊天程序
- java根据word模板导出word文件
- Spring 的Scope
- JAVA基础