CSDN博客

img softwave
博客专家

使用开源项目JExcelApi在Java环境中生成MS Excel文档

发表于2008/9/29 15:35:00  1398人阅读

欢迎点击此处订阅本Blog Blog 订阅

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

本文转载自:http://www.blogjava.net/supercrsky/archive/2008/05/21/201810.html

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());
        }

    }

完整源码可以在这里下载
(源码空间由文章原作者 々上善若水々 提供)

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 横链接单元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script> <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script>
0 0

相关博文

我的热门文章

img
取 消
img