|
Posted on 2008-05-30 09:25 blues 阅读(307) 评论(0) 编辑 收藏 所属分类: Other
l转自 posted on 2008-05-21 08:59 々上善若水々 阅读(1101) 评论(1) 编辑 收藏
jexcelapi是一个开源项目,主要用来操作excel.
主页地址:
http://www.andykhan.com/jexcelapi/
现在做一个项目用到了它,根据不同的公司生成不同的文件夹,
在相应的文件夹下生成对应的xls. 这里只帖出生成xls部分核心代码:
public void generateXls()
{
try
{
/** *//** **********创建工作簿************ */
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:/test.xls"));
/** *//** **********创建工作表************ */
WritableSheet sheet = workbook.createSheet("工作表名称", 0);
/** *//** *********设置列宽**************** */
sheet.setColumnView(0, 15); // 第1列
sheet.setColumnView(1, 18); // 第2列
sheet.setColumnView(2, 13);
sheet.setColumnView(3, 13);
sheet.setColumnView(4, 15);
sheet.setColumnView(5, 15);
//设置行高
sheet.setRowView(0, 600, false);
sheet.setRowView(1, 400, false);
sheet.setRowView(7, 400, false);
//设置页边距
sheet.getSettings().setRightMargin(0.5);
//设置页脚
sheet.setFooter("", "", "测试页脚");
/** *//** ************设置单元格字体************** */
//字体
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD);
WritableFont tableFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableFont baodanFont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD);
/** *//** ************以下设置几种格式的单元格************ */
// 用于标题
WritableCellFormat wcf_title = new WritableCellFormat(BoldFont);
wcf_title.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_title.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_title.setWrap(true); // 是否换行
// 用于表格标题
WritableCellFormat wcf_tabletitle = new WritableCellFormat(
tableFont);
wcf_tabletitle.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_tabletitle.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_tabletitle.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(true); // 是否换行
// 用于正文右
WritableCellFormat wcf_right = new WritableCellFormat(NormalFont);
wcf_right.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_right.setAlignment(Alignment.RIGHT);
wcf_right.setWrap(false); // 是否换行
// 用于跨行
WritableCellFormat wcf_merge = new WritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(true); // 是否换行
WritableCellFormat wcf_table = new WritableCellFormat(NormalFont);
wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_table.setAlignment(Alignment.CENTRE);
wcf_table.setBackground(Colour.GRAY_25);
wcf_table.setWrap(true); // 是否换行
/** *//** ************单元格格式设置完成****************** */
//合并单元格,注意mergeCells(col0,row0,col1,row1) --列从0开始,col1为你要合并到第几列,行也一样
sheet.mergeCells(0, 0, 5, 0);
sheet.addCell(new Label(0, 0, "这里是大标题,自定义格式",
wcf_title));
sheet.mergeCells(0, 1, 1, 1);
sheet.mergeCells(2, 1, 5, 1);
sheet.addCell(new Label(0, 1, "", wcf_center));
sheet.addCell(new Label(2, 1, "姓名:" + "supercrsky",
wcf_center));
sheet.mergeCells(0, 2, 1, 2);
sheet.mergeCells(2, 2, 3, 2);
sheet.addCell(new Label(0, 2, "单位:", wcf_center));
sheet.addCell(new Label(2, 2, "ChinaLong", wcf_center));
sheet.addCell(new Label(4, 2, "薪水", wcf_center));
sheet.addCell(new Label(5, 2, "5000", wcf_center));
sheet.mergeCells(0, 3, 1, 3);
sheet.mergeCells(2, 3, 3, 3);
sheet.addCell(new Label(0, 3, "性别:", wcf_center));
sheet.addCell(new Label(2, 3, "男", wcf_center));
sheet.addCell(new Label(4, 3, "婚否:", wcf_center));
sheet.addCell(new Label(5, 3, "否", wcf_center));
sheet.mergeCells(0, 4, 1, 4);
sheet.mergeCells(2, 4, 3, 4);
sheet.addCell(new Label(0, 4, "是否在职:", wcf_center));
sheet.addCell(new Label(2, 4,"是",
wcf_center));
sheet.addCell(new Label(4, 4,"工作经验:", wcf_center));
sheet.addCell(new Label(5, 4, "4",wcf_center));
sheet.mergeCells(0, 5, 1, 5);
sheet.mergeCells(2, 5, 3, 5);
sheet.addCell(new Label(0, 5, "保险费:", wcf_center));
sheet.addCell(new Label(2, 5,"50",
wcf_center));
sheet.addCell(new Label(4, 5, "保险金额:", wcf_center));
sheet.addCell(new Label(5, 5, "50000",
wcf_center));
sheet.mergeCells(0, 6, 1, 6);
sheet.mergeCells(2, 6, 3, 6);
sheet.addCell(new Label(0, 6, "工作地点:", wcf_center));
sheet.addCell(new Label(2, 6, "北京", wcf_center));
sheet.addCell(new Label(4, 6, "开心度:", wcf_center));
sheet.addCell(new Label(5, 6, "一般", wcf_center));
// 另起一table
sheet.mergeCells(0, 7, 5, 7);
sheet.addCell(new Label(0, 7, "详细数据", wcf_tabletitle));
// table标题
sheet.addCell(new Label(0, 8, "序号", wcf_table));
sheet.addCell(new Label(1, 8, "姓名", wcf_table));
sheet.addCell(new Label(2, 8, "年龄", wcf_table));
sheet.addCell(new Label(3, 8, "性别", wcf_table));
sheet.addCell(new Label(4, 8, "婚否", wcf_table));
sheet.addCell(new Label(5, 8, "在职", wcf_table));
// table内容
//这里用你的dao
TestDAO dao = new TestDAO();
List list = dao.findBy(user.getUserId());
System.out.println("此保单拥有防疫码数量:" + list.size());
for (int i = 0; i < list.size(); i++)
{
//对应你的vo类
User data = (User) list.get(i);
sheet.addCell(new Label(0, 9 + i, String.valueOf(i + 1),
wcf_center));
sheet.addCell(new Label(1, 9 + i, data.getDlEPCode(),
wcf_center));
sheet
.addCell(new Label(2, 9 + i, data.getDlType(),
wcf_center));
sheet.addCell(new Label(3, 9 + i, String.valueOf(data
.getDlPigAge()), wcf_center));
sheet.addCell(new Label(4, 9 + i, "", wcf_center));
sheet.addCell(new Label(5, 9 + i, "", wcf_center));
}
/** *//** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
workbook.write();
/** *//** *********关闭文件************* */
workbook.close();
System.out.println("导出成功");
// 存放url地址
} catch (Exception e)
{
System.out.println("在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
}
}
|