一般在java开发中需要下载xls或者是csv文件,需要在做报表或者是数据采集的时候需要,现将代码片段摘出:
public String exportMouldCsv() {
OutputStream os = null;
WritableWorkbook wbook = null;
try {
HttpServletResponse response = ServletActionContext.getResponse();
// 取得输出流
os = response.getOutputStream();
// 清空输出流
response.reset();
// 设定输出文件头
String fileName = "文件名设置.xls";
fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
// 定义输出类型
response.setContentType("application/msexcel");
// 建立excel文件
wbook = Workbook.createWorkbook(os);
WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
// WritableSheet.setColumnView(int i,int width)
// 作用是指定第i+1列的宽度,比如:
// 将第一列的宽度设为30
// sheet.setColumnView(0,30)
// wsheet.setRowView(0,10)
WritableFont font1 = new WritableFont(WritableFont.TIMES, 10,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat cellFormat_top = new WritableCellFormat();
cellFormat_top.setAlignment(jxl.format.Alignment.CENTRE);
// 设置单元格背景颜色
// cellFormat_top.setBackground(Colour.LIGHT_BLUE);
// 设置字体格式
cellFormat_top.setFont(font1);
cellFormat_top.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
WritableCellFormat cellFormat_bottom = new WritableCellFormat();
cellFormat_bottom.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
WritableCellFormat cellFormat_bottom_1 = new WritableCellFormat();
// 设置居中
cellFormat_bottom_1.setAlignment(jxl.format.Alignment.CENTRE);
// 设置表格边框
cellFormat_bottom_1.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
WritableSheet wsheet = wbook