package com.haiersoft.buztest.util;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelMethod {
public void toExcelTest(List stulist, HttpServletResponse response){
//创建工作流
OutputStream os = null;
//初始化工作表
WritableWorkbook workbook = null;
try {
//设置弹出对话框
response.setContentType("application/DOWLOAD");
//设置工作表的标题
response.setHeader("Content-Disposition", "attachment; filename=My Test TO Excel.xls");
os = response.getOutputStream();
//创建工作表
workbook = Workbook.createWorkbook(os);
//定义工作表 sheet 标题
WritableSheet ws = workbook.createSheet("Mapping", 0);
ws.getSettings().setShowGridLines(true);
ws.getSettings().setProtected(false);
//控制列的宽度,如果你要不给一样的宽度,就单独写,i代表的是列的下标,从0开始 ,从左到右
for(int i=0;i<7;i++){
ws.setColumnView(i, 20);
}
// 創建标题列名称 (此处可以利用一维数组输出,会更简单)
Label titleLabel = null;
titleLabel = new Label(0, 0, "StuNo", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(1, 0, "StuName", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(2, 0, "StuMoniter", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(3, 0, "StuAddr", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(4, 0, "Stu TELL", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(5, 0, "Stu sex", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(6, 0, "Stu classNo", getHeadFormat());
ws.addCell(titleLabel);
workbook.write();
workbook.close();
os.close();
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
public static WritableCellFormat getHeadFormat() throws Exception {
//设置字体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD);
//创建单元格FORMAT
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setLocked(true);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf.setBackground(Colour.GREY_25_PERCENT);
return wcf;
}
}
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelMethod {
public void toExcelTest(List stulist, HttpServletResponse response){
//创建工作流
OutputStream os = null;
//初始化工作表
WritableWorkbook workbook = null;
try {
//设置弹出对话框
response.setContentType("application/DOWLOAD");
//设置工作表的标题
response.setHeader("Content-Disposition", "attachment; filename=My Test TO Excel.xls");
os = response.getOutputStream();
//创建工作表
workbook = Workbook.createWorkbook(os);
//定义工作表 sheet 标题
WritableSheet ws = workbook.createSheet("Mapping", 0);
ws.getSettings().setShowGridLines(true);
ws.getSettings().setProtected(false);
//控制列的宽度,如果你要不给一样的宽度,就单独写,i代表的是列的下标,从0开始 ,从左到右
for(int i=0;i<7;i++){
ws.setColumnView(i, 20);
}
// 創建标题列名称 (此处可以利用一维数组输出,会更简单)
Label titleLabel = null;
titleLabel = new Label(0, 0, "StuNo", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(1, 0, "StuName", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(2, 0, "StuMoniter", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(3, 0, "StuAddr", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(4, 0, "Stu TELL", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(5, 0, "Stu sex", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(6, 0, "Stu classNo", getHeadFormat());
ws.addCell(titleLabel);
workbook.write();
workbook.close();
os.close();
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
public static WritableCellFormat getHeadFormat() throws Exception {
//设置字体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD);
//创建单元格FORMAT
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setLocked(true);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf.setBackground(Colour.GREY_25_PERCENT);
return wcf;
}
}