`
king_tt
  • 浏览: 2113732 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

 
阅读更多
<!-- Feedsky FEED发布代码开始 --> 欢迎点击此处订阅本Blog <!-- FEED自动发现标记开始 --> <link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 -->Blog 订阅

<!--Google 468*60横幅广告开始--><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><!--Google 468*60横幅广告结束-->

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

jexcelapi是一个开源项目,主要用来操作excel.
主页地址:
http://www.andykhan.com/jexcelapi/

现在做一个项目用到了它,根据不同的公司生成不同的文件夹,
在相应的文件夹下生成对应的xls. 这里只帖出生成xls部分核心代码:
publicvoidgenerateXls()
{
try
{
/***//************创建工作簿*************/
WritableWorkbookworkbook
=Workbook.createWorkbook(newFile("d:/test.xls"));
/***//************创建工作表*************/
WritableSheetsheet
=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("","","测试页脚");
/***//**************设置单元格字体***************/
//字体
WritableFontNormalFont=newWritableFont(WritableFont.ARIAL,10);
WritableFontBoldFont
=newWritableFont(WritableFont.ARIAL,14,
WritableFont.BOLD);
WritableFonttableFont
=newWritableFont(WritableFont.ARIAL,12,
WritableFont.NO_BOLD);
WritableFontbaodanFont
=newWritableFont(WritableFont.ARIAL,10,
WritableFont.BOLD);

/***//**************以下设置几种格式的单元格*************/
//用于标题
WritableCellFormatwcf_title=newWritableCellFormat(BoldFont);
wcf_title.setBorder(Border.NONE,BorderLineStyle.THIN);
//线条
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
wcf_title.setAlignment(Alignment.CENTRE);//水平对齐
wcf_title.setWrap(true);//是否换行

//用于表格标题
WritableCellFormatwcf_tabletitle=newWritableCellFormat(
tableFont);
wcf_tabletitle.setBorder(Border.NONE,BorderLineStyle.THIN);
//线条
wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
wcf_tabletitle.setAlignment(Alignment.CENTRE);//水平对齐
wcf_tabletitle.setWrap(true);//是否换行

//用于正文左
WritableCellFormatwcf_left=newWritableCellFormat(NormalFont);
wcf_left.setBorder(Border.ALL,BorderLineStyle.THIN);
//线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(
true);//是否换行

//用于正文左
WritableCellFormatwcf_center=newWritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL,BorderLineStyle.THIN);
//线条
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(
true);//是否换行

//用于正文右
WritableCellFormatwcf_right=newWritableCellFormat(NormalFont);
wcf_right.setBorder(Border.ALL,BorderLineStyle.THIN);
//线条
wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
wcf_right.setAlignment(Alignment.RIGHT);
wcf_right.setWrap(
false);//是否换行

//用于跨行
WritableCellFormatwcf_merge=newWritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL,BorderLineStyle.THIN);
//线条
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP);//垂直对齐
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(
true);//是否换行

WritableCellFormatwcf_table
=newWritableCellFormat(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(
newLabel(0,0,"这里是大标题,自定义格式",
wcf_title));

sheet.mergeCells(
0,1,1,1);
sheet.mergeCells(
2,1,5,1);

sheet.addCell(
newLabel(0,1,"",wcf_center));
sheet.addCell(
newLabel(2,1,"姓名:"+"supercrsky",
wcf_center));

sheet.mergeCells(
0,2,1,2);
sheet.mergeCells(
2,2,3,2);

sheet.addCell(
newLabel(0,2,"单位:",wcf_center));
sheet.addCell(
newLabel(2,2,"ChinaLong",wcf_center));
sheet.addCell(
newLabel(4,2,"薪水",wcf_center));
sheet.addCell(
newLabel(5,2,"5000",wcf_center));

sheet.mergeCells(
0,3,1,3);
sheet.mergeCells(
2,3,3,3);

sheet.addCell(
newLabel(0,3,"性别:",wcf_center));
sheet.addCell(
newLabel(2,3,"",wcf_center));
sheet.addCell(
newLabel(4,3,"婚否:",wcf_center));
sheet.addCell(
newLabel(5,3,"",wcf_center));

sheet.mergeCells(
0,4,1,4);
sheet.mergeCells(
2,4,3,4);

sheet.addCell(
newLabel(0,4,"是否在职:",wcf_center));
sheet.addCell(
newLabel(2,4,"",
wcf_center));
sheet.addCell(
newLabel(4,4,"工作经验:",wcf_center));
sheet.addCell(
newLabel(5,4,"4",wcf_center));

sheet.mergeCells(
0,5,1,5);
sheet.mergeCells(
2,5,3,5);

sheet.addCell(
newLabel(0,5,"保险费:",wcf_center));
sheet.addCell(
newLabel(2,5,"50",
wcf_center));
sheet.addCell(
newLabel(4,5,"保险金额:",wcf_center));
sheet.addCell(
newLabel(5,5,"50000",
wcf_center));

sheet.mergeCells(
0,6,1,6);
sheet.mergeCells(
2,6,3,6);

sheet.addCell(
newLabel(0,6,"工作地点:",wcf_center));
sheet.addCell(
newLabel(2,6,"北京",wcf_center));
sheet.addCell(
newLabel(4,6,"开心度:",wcf_center));
sheet.addCell(
newLabel(5,6,"一般",wcf_center));

//另起一table
sheet.mergeCells(0,7,5,7);
sheet.addCell(
newLabel(0,7,"详细数据",wcf_tabletitle));
//table标题
sheet.addCell(newLabel(0,8,"序号",wcf_table));
sheet.addCell(
newLabel(1,8,"姓名",wcf_table));
sheet.addCell(
newLabel(2,8,"年龄",wcf_table));
sheet.addCell(
newLabel(3,8,"性别",wcf_table));
sheet.addCell(
newLabel(4,8,"婚否",wcf_table));
sheet.addCell(
newLabel(5,8,"在职",wcf_table));
//table内容
//这里用你的dao
TestDAOdao=newTestDAO();
Listlist
=dao.findBy(user.getUserId());
System.out.println(
"此保单拥有防疫码数量:"+list.size());
for(inti=0;i<list.size();i++)
{
//对应你的vo类
Userdata=(User)list.get(i);

sheet.addCell(
newLabel(0,9+i,String.valueOf(i+1),
wcf_center));
sheet.addCell(
newLabel(1,9+i,data.getDlEPCode(),
wcf_center));
sheet
.addCell(
newLabel(2,9+i,data.getDlType(),
wcf_center));
sheet.addCell(
newLabel(3,9+i,String.valueOf(data
.getDlPigAge()),wcf_center));
sheet.addCell(
newLabel(4,9+i,"",wcf_center));
sheet.addCell(
newLabel(5,9+i,"",wcf_center));
}

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

}

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

<!--新Google 468*60横幅广告开始--><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><!--新Google 468*60横幅广告结束-->

<!--新Google 468x15 横链接单元开始--><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><!--新Google 468x15 横链接单元结束-->

<!-- Google Reader shared发布代码开始 --><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&amp;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><!-- Google Reader shared发布代码结束 -->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics