CSV导出代码

public String exportqdCsv() {
OutputStream os = null;
WritableWorkbook wbook = null;
try {
HttpServletResponse response = ServletActionContext
.getResponse();
// 取得输出流
os = response.getOutputStream();
// 清空输出流
response.reset();
// 设定输出文件头
response.setHeader(
"Content-Disposition",
"attachment; filename=\""
+ new String("上游客户批量修改表".getBytes("GBK"),
("ISO8859-1")) + ".csv\"");
// 定义输出类型

response.setContentType("application/msexcel");
// 建立excel文件
wbook = Workbook.createWorkbook(os);
WritableSheet wsheet = wbook.createSheet("sheet1", 0);
// WritableSheet.setColumnView(int i,int width);
// 作用是指定第i+1列的宽度,比如:
// 将第一列的宽度设为30
// sheet.setColumnView(0,30);
// wsheet.setRowView(0,10);
// 设置单元格背景颜色
// 设置字体格式

// 表头
Label label_0 = new Label(0, 0, "测试");
wsheet.addCell(label_0);

Label label_1 = new Label(1, 0, "测试1");
wsheet.addCell(label_1);

Label label_2 = new Label(2, 0, "测试2");
wsheet.addCell(label_2);

Label label_3 = new Label(3, 0, "测试3");
wsheet.addCell(label_3);
wbook.write();
} catch (Exception e) {
logger.error(e);
} finally {
if (wbook != null) {
try {
wbook.close();
} catch (Exception e) {
logger.error(e);
}
wbook = null;
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
logger.error(e);
}
os = null;
}
}
return RESULT_MESSAGE;
}

你可能感兴趣的:(Excel)