我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

jexcelapi使用小记

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(
015); // 第1列
            sheet.setColumnView(118); // 第2列
            sheet.setColumnView(213);
            sheet.setColumnView(
313);
            sheet.setColumnView(
415);
            sheet.setColumnView(
515);
            
//设置行高
            sheet.setRowView(0600false);
            sheet.setRowView(
1400false);
            sheet.setRowView(
7400false);
            
//设置页边距
            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(0050);

            sheet.addCell(
new Label(00"这里是大标题,自定义格式",
                    wcf_title));

            sheet.mergeCells(
0111);
            sheet.mergeCells(
2151);

            sheet.addCell(
new Label(01"", wcf_center));
            sheet.addCell(
new Label(21"姓名:" + "supercrsky",
                    wcf_center));

            sheet.mergeCells(
0212);
            sheet.mergeCells(
2232);

            sheet.addCell(
new Label(02"单位:", wcf_center));
            sheet.addCell(
new Label(22"ChinaLong", wcf_center));
            sheet.addCell(
new Label(42"薪水", wcf_center));
            sheet.addCell(
new Label(52"5000", wcf_center));

            sheet.mergeCells(
0313);
            sheet.mergeCells(
2333);

            sheet.addCell(
new Label(03"性别:", wcf_center));
            sheet.addCell(
new Label(23"", wcf_center));
            sheet.addCell(
new Label(43"婚否:", wcf_center));
            sheet.addCell(
new Label(53"", wcf_center));

            sheet.mergeCells(
0414);
            sheet.mergeCells(
2434);

            sheet.addCell(
new Label(04"是否在职:", wcf_center));
            sheet.addCell(
new Label(24,"",
                    wcf_center));
            sheet.addCell(
new Label(44,"工作经验:", wcf_center));
            sheet.addCell(
new Label(54"4",wcf_center));

            sheet.mergeCells(
0515);
            sheet.mergeCells(
2535);

            sheet.addCell(
new Label(05"保险费:", wcf_center));
            sheet.addCell(
new Label(25,"50",
                    wcf_center));
            sheet.addCell(
new Label(45"保险金额:", wcf_center));
            sheet.addCell(
new Label(55"50000",
                    wcf_center));

            sheet.mergeCells(
0616);
            sheet.mergeCells(
2636);

            sheet.addCell(
new Label(06"工作地点:", wcf_center));
            sheet.addCell(
new Label(26"北京", wcf_center));
            sheet.addCell(
new Label(46"开心度:", wcf_center));
            sheet.addCell(
new Label(56"一般", wcf_center));

            
// 另起一table
            sheet.mergeCells(0757);
            sheet.addCell(
new Label(07"详细数据", wcf_tabletitle));
            
// table标题
            sheet.addCell(new Label(08"序号", wcf_table));
            sheet.addCell(
new Label(18"姓名", wcf_table));
            sheet.addCell(
new Label(28"年龄", wcf_table));
            sheet.addCell(
new Label(38"性别", wcf_table));
            sheet.addCell(
new Label(48"婚否", wcf_table));
            sheet.addCell(
new Label(58"在职", 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(09 + i, String.valueOf(i + 1),
                        wcf_center));
                sheet.addCell(
new Label(19 + i, data.getDlEPCode(),
                        wcf_center));
                sheet
                        .addCell(
new Label(29 + i, data.getDlType(),
                                wcf_center));
                sheet.addCell(
new Label(39 + i, String.valueOf(data
                        .getDlPigAge()), wcf_center));
                sheet.addCell(
new Label(49 + i, "", wcf_center));
                sheet.addCell(
new Label(59 + i, "", wcf_center));
            }

            
/** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
            workbook.write();
            
/** *********关闭文件************* */
            workbook.close();
            System.out.println(
"导出成功");
            
// 存放url地址
        }
 catch (Exception e)
        
{
            System.out.println(
"在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
        }

    }

完整源码可以在这里下载

posted on 2008-05-21 08:59 々上善若水々 阅读(7963) 评论(2)  编辑  收藏

评论

# re: jexcelapi使用小记  回复  更多评论   

用过,跨平台,无需装office就可以操作,算是方便吧。
2008-05-21 09:13 | Matthew Chen

# re: jexcelapi使用小记  回复  更多评论   

谢谢,正是需要的 ^_^
2008-06-28 21:48 | smf

只有注册用户登录后才能发表评论。


网站导航: