/**
* 导出excel 案例
*/
public void exportExcel(){
long beginTime = System.currentTimeMillis();
HttpServletResponse response= ServletActionContext.getResponse();
OutputStream os=null;
if(activityForm==null){
activityForm=new ActivityForm();
}
try {
activityForm.setStartSize((getCurrentpage()-1)*getPagesize());
activityForm.setPageSize(getPagesize());
activityForm.setUserInfoId(getUserInfoSession().getId());
PageList
activityForm.setPageSize(page.getTotalcount());
List
Workbook createbook = createActivityBook(activitys);
// 创建下载文件名
String uploadFileName = System.currentTimeMillis() + ".xls";
os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="+ uploadFileName);// 设定输出文件头
response.setContentType("application/x-download");
createbook.write(os);
os.close();
} catch (Exception e) {
error("请求exportActivity方法异常",e);
}finally{
printTime(beginTime, getClass(), "exportActivity");
}
}
public static HSSFWorkbook createActivityBook(List
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
ActivityForm activityForm=null;
// sheet 对应一个工作页
HSSFSheet sheet = hwb.createSheet("导出活动列表信息");
int CountColumnNum = 5; //指定列头个数
HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "活动名称";
names[1] = "活动类型";
names[2] = "活动链接";
names[3] = "备注";
names[4] = "创建时间";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < list.size(); i++) {
// 创建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
activityForm = list.get(i);
for (int colu = 0; colu < CountColumnNum; colu++) {
// 在一行内循环
HSSFCell name = row.createCell(0);
HSSFCell type = row.createCell(1);
HSSFCell url = row.createCell(2);
HSSFCell remark = row.createCell(3);
HSSFCell createDate = row.createCell(4);
name.setCellValue(activityForm.getName());
type.setCellValue(activityForm.getType());
url.setCellValue(activityForm.getUrl());
remark.setCellValue(activityForm.getRemark());
createDate.setCellValue(activityForm.getCreateDate());
}
}
return hwb;
}
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章