1.poi和jxl都可以进行对excel的导出,但两者的差别也是很明显的,读者可以细查
以下介绍jxl到处excel的方式
1.1无表头的方式 需要 OutputStream,List集合注意行和列都是从0开始
public static void createExcel(OutputStream os,
List<Object> object) throws WriteException, IOException {
String FirstSheet = "人员信息";
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
// 创建新的一页 命名
WritableSheet sheet = workbook.createSheet(FirstSheet, 0);
// 构造表头
WritableFont color = new WritableFont(WritableFont.createFont("宋体"), 12);// 选择字体
// color.setColour(Colour.BLACK);// 设置字体颜色为黑色
WritableCellFormat titleFormate1 = new WritableCellFormat(color);
titleFormate1.setAlignment(jxl.format.Alignment.LEFT);// 单元格中的内容水平方向居左
titleFormate1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中
titleFormate1.setWrap(true);
//设置表头
Label XLH = new Label(0, 0, "编号", titleFormate1);
//设置工作表指定列的宽度。这导致Excel调整整个列。如果已经指定的列视图相关联的信息,然后取而代之的是新的数据
//第一个参数是列,第二个参数是宽度
sheet.setColumnView(0, 9);
//添加单元格到工作表中
sheet.addCell(XLH);
Label BH = new Label(1, 0, "姓名", titleFormate1);
sheet.setColumnView(1, 12);
sheet.addCell(BH);
Label KS = new Label(2, 0, "出生年月", titleFormate1);
sheet.setColumnView(2, 16);
sheet.addCell(KS);
Label FSSJ = new Label(3, 0, "初始学校", titleFormate1);
sheet.setColumnView(3, 25);
sheet.addCell(FSSJ);
sheet.setRowView(1, 600, false);// 设置第二行的高度
for (int i = 0; i < object.size(); i++) {
Object object1= object.get(i);
int rowIndex = i + 1;
sheet.addCell(new Label(0, rowIndex, i + 1 + "", titleFormate1));
sheet.addCell(new Label(1, rowIndex, object1.Name,titleFormate1));
sheet.addCell(new Label(2, rowIndex, object1.phoneNumber, titleFormate1));
sheet.addCell(new Label(3, rowIndex, object1.phoneNumber, titleFormate1));
}
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}