1个回答
展开全部
struts2 Action的类中代码
public void createExecl() throws WriteException, IOException{
WritableWorkbook book=null;
@SuppressWarnings("unused")
WritableSheet sheet=null;
try{
String path="C:\\test.xls";
File file=new File(path);
//打开文件
book = Workbook.createWorkbook(file);
// 生成名为“第一页”的工作表,参数0表示这是第一页
sheet = book.createSheet( " 第一页 " , 0 );
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
}catch(Exception e){
System.out.println(e);
}finally{
//写入数据并关闭文件
book.write();
book.close();
}
try {
String path="C:\\test.xls";
File file=new File(path);
// Excel获得文件
Workbook wb = Workbook.getWorkbook(file);
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book2 = Workbook.createWorkbook(file, wb);
// 添加一个工作表
//WritableSheet sheet = book.createSheet( " 第二页 " , 1 );
//WritableSheet sheet = book.getSheet(0);
WritableSheet sheet2=book2.getSheet(0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label label=null;
Label label2=null;
label=new Label(0,0,"计费Id");
sheet2.addCell(label);
label=new Label(1,0,"主动呼叫号码");
sheet2.addCell(label);
label=new Label(2,0,"被动呼叫号码");
sheet2.addCell(label);
label=new Label(3,0,"计费开始时间");
sheet2.addCell(label);
label=new Label(4,0,"计费结束时间");
sheet2.addCell(label);
label=new Label(5,0,"计费单价");
sheet2.addCell(label);
label=new Label(6,0,"计费总金额");
sheet2.addCell(label);
label=new Label(7,0,"扣费号码");
sheet2.addCell(label);
label=new Label(8,0,"活动名称");
sheet2.addCell(label);
label=new Label(9,0,"备注");
sheet2.addCell(label);
int j=1;
List<Object> list=baseManager.findByHql("from TalkInformationBean tb left join fetch tb.activeBean order by tb.talkId desc tb.talkId");
Iterator<Object> it=list.iterator();
while(it.hasNext()){
talkInfo=(TalkInformationBean)it.next();
label2 = new Label( 0 , j , talkInfo.getTalkId().toString() );
sheet2.addCell(label2);
label2 = new Label( 1 , j , talkInfo.getInitiMobile().toString() );
sheet2.addCell(label2);
label2 = new Label( 2 , j , talkInfo.getPassMobile().toString() );
sheet2.addCell(label2);
label2 = new Label( 3 , j , talkInfo.getStartTime().toString() );
sheet2.addCell(label2);
label2 = new Label( 4 , j , talkInfo.getEndTime().toString() );
sheet2.addCell(label2);
label2 = new Label( 5 , j , talkInfo.getPrice().toString() );
sheet2.addCell(label2);
label2 = new Label( 6 , j , talkInfo.getTotalMoney().toString() );
sheet2.addCell(label2);
label2 = new Label( 7 , j , talkInfo.getDeductMobile().toString() );
sheet2.addCell(label2);
if(talkInfo.getActiveBean()==null){
label2 = new Label( 8 , j , null);
}else{
label2 = new Label( 8 , j , talkInfo.getActiveBean().getActiveName().toString());
}
sheet2.addCell(label2);
label2 = new Label( 9 , j , talkInfo.getRemark().toString() );
sheet2.addCell(label2);
j++;
}
// 将定义好的单元格添加到工作表中
book2.write();
book2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public InputStream getDownloadFile() {
File file = new File("C:\\test.xls");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return fis;
}
/**
* 查询所有通话记录
*
* @param
*
* @return
* String success --> 欢迎页面 fail --> 错误页面
* @throws IOException
* @throws WriteException
* @throws
* HRException
*/
@SuppressWarnings("unchecked")
public String toexport() throws WriteException, IOException{
this.createExecl();
return "success";
}
struts.xml代码
<action name="toexport" class="talkAction" method="toexport">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="test.xls"</param>
<param name="inputName">downloadFile</param>
</result>
<result name="TALKINFO">/talkInfo/talkInfo.jsp</result>
</action>
JSP页面代码
<input class="input_back" type="button" name="Submit0" value="导出数据"onclick="location.href='toexport.action'"/></td>
public void createExecl() throws WriteException, IOException{
WritableWorkbook book=null;
@SuppressWarnings("unused")
WritableSheet sheet=null;
try{
String path="C:\\test.xls";
File file=new File(path);
//打开文件
book = Workbook.createWorkbook(file);
// 生成名为“第一页”的工作表,参数0表示这是第一页
sheet = book.createSheet( " 第一页 " , 0 );
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
}catch(Exception e){
System.out.println(e);
}finally{
//写入数据并关闭文件
book.write();
book.close();
}
try {
String path="C:\\test.xls";
File file=new File(path);
// Excel获得文件
Workbook wb = Workbook.getWorkbook(file);
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book2 = Workbook.createWorkbook(file, wb);
// 添加一个工作表
//WritableSheet sheet = book.createSheet( " 第二页 " , 1 );
//WritableSheet sheet = book.getSheet(0);
WritableSheet sheet2=book2.getSheet(0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label label=null;
Label label2=null;
label=new Label(0,0,"计费Id");
sheet2.addCell(label);
label=new Label(1,0,"主动呼叫号码");
sheet2.addCell(label);
label=new Label(2,0,"被动呼叫号码");
sheet2.addCell(label);
label=new Label(3,0,"计费开始时间");
sheet2.addCell(label);
label=new Label(4,0,"计费结束时间");
sheet2.addCell(label);
label=new Label(5,0,"计费单价");
sheet2.addCell(label);
label=new Label(6,0,"计费总金额");
sheet2.addCell(label);
label=new Label(7,0,"扣费号码");
sheet2.addCell(label);
label=new Label(8,0,"活动名称");
sheet2.addCell(label);
label=new Label(9,0,"备注");
sheet2.addCell(label);
int j=1;
List<Object> list=baseManager.findByHql("from TalkInformationBean tb left join fetch tb.activeBean order by tb.talkId desc tb.talkId");
Iterator<Object> it=list.iterator();
while(it.hasNext()){
talkInfo=(TalkInformationBean)it.next();
label2 = new Label( 0 , j , talkInfo.getTalkId().toString() );
sheet2.addCell(label2);
label2 = new Label( 1 , j , talkInfo.getInitiMobile().toString() );
sheet2.addCell(label2);
label2 = new Label( 2 , j , talkInfo.getPassMobile().toString() );
sheet2.addCell(label2);
label2 = new Label( 3 , j , talkInfo.getStartTime().toString() );
sheet2.addCell(label2);
label2 = new Label( 4 , j , talkInfo.getEndTime().toString() );
sheet2.addCell(label2);
label2 = new Label( 5 , j , talkInfo.getPrice().toString() );
sheet2.addCell(label2);
label2 = new Label( 6 , j , talkInfo.getTotalMoney().toString() );
sheet2.addCell(label2);
label2 = new Label( 7 , j , talkInfo.getDeductMobile().toString() );
sheet2.addCell(label2);
if(talkInfo.getActiveBean()==null){
label2 = new Label( 8 , j , null);
}else{
label2 = new Label( 8 , j , talkInfo.getActiveBean().getActiveName().toString());
}
sheet2.addCell(label2);
label2 = new Label( 9 , j , talkInfo.getRemark().toString() );
sheet2.addCell(label2);
j++;
}
// 将定义好的单元格添加到工作表中
book2.write();
book2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public InputStream getDownloadFile() {
File file = new File("C:\\test.xls");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return fis;
}
/**
* 查询所有通话记录
*
* @param
*
* @return
* String success --> 欢迎页面 fail --> 错误页面
* @throws IOException
* @throws WriteException
* @throws
* HRException
*/
@SuppressWarnings("unchecked")
public String toexport() throws WriteException, IOException{
this.createExecl();
return "success";
}
struts.xml代码
<action name="toexport" class="talkAction" method="toexport">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="test.xls"</param>
<param name="inputName">downloadFile</param>
</result>
<result name="TALKINFO">/talkInfo/talkInfo.jsp</result>
</action>
JSP页面代码
<input class="input_back" type="button" name="Submit0" value="导出数据"onclick="location.href='toexport.action'"/></td>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询