excel 导出 demo

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public void exportExcelFile() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
String fileName = Long.toString(System.currentTimeMillis())+".xls";
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型

ResourceMgt
.addDefaultResourceBundle("com.zte.cnt_manager_resource");

String title = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.title");

WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
String tmptitle = title; // 标题
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称

// 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setBackground(Colour.AQUA);
wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));

// 读取资源文件,获得excel的第一行文字内容。
String cntid = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.cntid");
String cntname = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.cntname");
String catname = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.catname");
String cntcreatetime = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.cntcreatetime");
String truename = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.truename");
String statusname = ResourceMgt
.findDefaultText("cnt.expexcel.cntmanager.status");
// 生成excel 第一行数据。
wsheet.addCell(new Label(0, 0, cntid));
wsheet.addCell(new Label(1, 0, cntname));
wsheet.addCell(new Label(2, 0, catname));
wsheet.addCell(new Label(3, 0, cntcreatetime));
wsheet.addCell(new Label(4, 0, truename));
wsheet.addCell(new Label(5, 0, statusname));

// 导出的excel主体 数据内容。

mcntContentExtends.setApplytimestart(mcntContentExtends
.getApplytimestart());
mcntContentExtends.setApplytimeend(mcntContentExtends
.getApplytimeend());
// 对导出的数据 排序。
PageUtilEntity puEntity = new PageUtilEntity();
puEntity.setOrderByColumn("applytime");
puEntity.setIsAsc(false);

MsysConfig msysConfig  = new MsysConfig();
msysConfig.setCfgkey("appstore.terminal.model");
msysConfig = msysConfigLS.getMsysConfig(msysConfig)==null?new MsysConfig():msysConfigLS.getMsysConfig(msysConfig);
String cfgvalue = msysConfig.getCfgvalue()==null?"":msysConfig.getCfgvalue();
if("1".equals(cfgvalue)){
mcntContentExtends.setCnttype(new Long(1));
}else if("2".equals(cfgvalue)){
mcntContentExtends.setCnttype(new Long(2));
}else{  // 暂时不支持 两者 都支持的情况。

}
// 判断是否要根据页面的查询条件来导出数据。
if ("y".equals(flag)) {
// mcntContentExtends = new McntContentExtends();
mcntContentExtends = new AppcntContentExtends();
if("1".equals(cfgvalue)){
mcntContentExtends.setCnttype(new Long(1));
}else if("2".equals(cfgvalue)){
mcntContentExtends.setCnttype(new Long(2));
}else{  // 暂时不支持 两者 都支持的情况。

}
}

// TableDataInfo tableDataInfo = mcntContentLS
// .pageInfoQueryWithForeignObj(mcntContentExtends, 0, 10000,
// puEntity);

TableDataInfo tableDataInfo = appcntContentLS
.appPageInfoQueryWithForeignObj(mcntContentExtends, 0,
10000, puEntity);

mcntExtendsList = (List<AppcntContentExtends>) tableDataInfo
.getData();

if (mcntExtendsList == null || mcntExtendsList.size() == 0) {
return;
}
// 开始生成主体内容
int i = 1;
for (AppcntContentExtends mcntContentExtend : mcntExtendsList) {
wsheet.addCell(new Label(0, i, mcntContentExtend.getCntid()));
wsheet.addCell(new Label(1, i, mcntContentExtend.getCntname()));
wsheet.addCell(new Label(2, i, mcntContentExtend
.getClass1name()
+ "--" + mcntContentExtend.getClass2name()));
wsheet
.addCell(new Label(3, i, mcntContentExtend
.getApplytime()));
wsheet
.addCell(new Label(4, i, mcntContentExtend
.getTruename()));
String statusString = "";
Integer status = mcntContentExtend.getStatus()==null?new Integer(10):mcntContentExtend.getStatus();
if (status == 0) {
statusString = "申请";
} else if (status == 1) {
statusString = "上线";

} else if (status == 2) {
statusString = "下线(暂停)";

} else if (status == 3) {

statusString = "归档";
}else if(status==10){
statusString ="无效状态";

}

wsheet.addCell(new Label(5, i, statusString));

i++;
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.close(); // 关闭流
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
} catch (RowsExceededException e2) {
e2.printStackTrace();
return;
} catch (WriteException e3) {
e3.printStackTrace();
return;
} catch (Exception e4) {
e4.printStackTrace();
return;
}
}

你可能感兴趣的:(OS,Excel)