📄 excelaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.accphr.web.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.accphr.biz.IThirdKindBiz;
import com.accphr.entity.ThirdKind;
public class ExcelAction extends DispatchAction {
private IThirdKindBiz thirdKindBiz;
public void setThirdKindBiz(IThirdKindBiz thirdKindBiz) {
this.thirdKindBiz = thirdKindBiz;
}
private int i=0;
public ActionForward doShow(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = System.getProperty("java.io.tmpdir") + "\\xls"
+ (new java.util.Date().getTime()) + (i++);
// 创建一个空的Excel文档
WritableWorkbook wwb = Workbook.createWorkbook(new File(fileName));
// 创建Excel工作表
WritableSheet ws = wwb.createSheet("三级机构信息", 0);
Label label0 = new Label(0, 0, "一级机构名称");
Label label1 = new Label(1, 0, "二级机构名称");
Label label2 = new Label(2, 0, "三级机构名称");
Label label3 = new Label(3, 0, "负责人");
ws.addCell(label0);
ws.addCell(label1);
ws.addCell(label2);
ws.addCell(label3);
// 添加记录信息
List list =this.thirdKindBiz.findAll();
for (int i = 0; i < list.size(); i++) {
ThirdKind thirdKind=(ThirdKind)list.get(i);
Label temp0 = new Label(0, i + 1, String.valueOf(thirdKind.getFirstKindName()));
ws.addCell(temp0);
Label temp1 = new Label(1, i + 1, String.valueOf(thirdKind.getSecondKindName()));
ws.addCell(temp1);
Label temp2 = new Label(2, i + 1, String.valueOf(thirdKind.getThirdKindName()));
ws.addCell(temp2);
Label temp3 = new Label(3, i + 1, String.valueOf(thirdKind.getThirdKindSaleName()));
ws.addCell(temp3);
}
wwb.write();
wwb.close();
response.setContentType("application/x-msdownload;charset=GBK");
response.setCharacterEncoding("UTF-8");
String docName = java.net.URLEncoder.encode("三级机构信息.xls", "UTF-8");
response.setHeader("Content-Disposition", "inline; filename="
+ new String(docName.getBytes("UTF-8"), "GBK"));
BufferedInputStream br = new BufferedInputStream(new FileInputStream(
fileName));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
out.close();
br.close();
return null;
}
public ActionForward toShow(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
return mapping.findForward("toshow");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -