1、使用POI
A)下载地址http://poi.apache.org/download.html
B)使用方式
1、准备一个简单的excel文档(静态的数据都先填写上去),放入到项目src路径下中
2、在action中读取excel文档,并创建一个excel文档对象
String filename = PropertyUtil.getPropertyValue(“配置文件路径”)+”文件名”;
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename));
3、获取每一个分页的对象文档中默认有三个sheet,默认第一个下角标为0。 HSSFSheet sheet0 = wb.getSheetAt(0);//获取第一个sheet。1,2表示后面两个
4、动态加入数据
1、创建一个行
Row row = sheet0.createRow(“第几行”);
2、在这一行创建 一个单元格
Cell cell = row.createCell(第几个);
3、往里头填数据
cell.setCellValue(“我的数据”);
5、特效
1、单元格合并
Sheet0.addMergedRegion(new CellRangeAddress(四个参数分别表示行,列坐标));//合并这里面的范围到一个单元格中
2、单元格样式
Cell.setCellStyle(一个样式对象CellStyle类型);
CellStyle style = wb.createCellStyle();
Style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
Style.setBorderTop(CellStyle.BORDER_THIN);等等
//还有很多可以查看POI文档
C)好坏
1、好处:控制力强,是我们熟悉的java代码,不过需要熟悉第三方API
2、缺点:代码量比较大
2、使用excel模板方式
1、制作你想要的excel文档,列宽度、字体等设置好。(作为模板使用)
2、将excel另存为:->其他格式->XML 电子表格2003 (*.xml) 文件名记:1.xls
3、使用文本编辑器打开,1.xls。你可以发现类似于xml的标签
<?xml version="1.0"?>
%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%
String date=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
response.setContentType("application/vnd.ms-excel");
String filename="项目信息表_"+date;
String userAgent=request.getHeader("User-Agent");
if(userAgent.indexOf("MSIE")>-1){
filename=java.net.URLEncoder.encode(filename,"UTF-8");
}else{
filename=new String(filename.getBytes("UTF-8"),"ISO8859-1");
}
response.setHeader("Content-disposition","attachment; filename="+filename+".xls");
%><?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>1996-10-14T23:33:28Z</Created>
<LastSaved>2006-10-19T23:03:49Z</LastSaved>
<Version>11.5606</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>9300</WindowHeight>
<WindowWidth>15135</WindowWidth>
<WindowTopX>120</WindowTopX>
<WindowTopY>120</WindowTopY>
<AcceptLabelsInFormulas/>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font x:Family="Swiss"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
~等等
我们需要在动态的row哪里添加我们的数据
<Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="<s:property value="#request.list.size+1"/>" x:FullColumns="1"
x:FullRows="1">
<Column ss:AutoFitWidth="0" ss:Width="80"/>
<Column ss:AutoFitWidth="0" ss:Width="80"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="60"/>
<Column ss:AutoFitWidth="0" ss:Width="100"/>
<Row>
<Cell ss:StyleID="s25"><Data ss:Type="String">主板名称</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">项目名称</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">产品线</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">产品类型</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">项目经理</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">项目类别</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">项目状态</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">重要程度</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">启动时间</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">计划完成</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">实际完成</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">创建者</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">创建时间</Data></Cell>
</Row>
<s:iterator id="list" status="index" value="#request.list">
<Row>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="mainboard"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="productLine.name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="productCategory.name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="manager.realname"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="projectCategory.name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="projectStatus.name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="projectLevel.name"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:date name="startTime" id="date1" format="yyyy-MM-dd"/><s:if test="#attr.date1=='1900-01-01'">待定</s:if><s:else><s:property value="%{#attr.date1}"/></s:else></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:date name="finishTime" id="date1" format="yyyy-MM-dd"/><s:if test="#attr.date1=='1900-01-01'">待定</s:if><s:else><s:property value="%{#attr.date1}"/></s:else></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:date name="actualFinishTime" id="date1" format="yyyy-MM-dd"/><s:if test="#attr.date1=='1900-01-01'">待定</s:if><s:else><s:property value="%{#attr.date1}"/></s:else></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:property value="creator.realname"/></Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String"><s:date name="addTime" id="date1" format="yyyy-MM-dd HH:mm:ss"/><s:property value="%{#attr.date1}"/></Data></Cell>
</Row>
</s:iterator>
C)、好坏
1、坏处:xml标签学习代价很高,不易上手,这样的jsp页面不容易看懂。比如动态合并单元格就很麻烦,你得需要XML标签知识才能更改。
2、好处:不用java代码,只要在填充数据的地方使用itertor来遍历你的数据即可,所以对于简单的数据量,样式很少的可以使用这个
3、使用html基本table方式
<%@ page contentType="application/vnd.ms-excel; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!-- 以上这行设定本网页为excel格式的网页 -->
<%
String fileName = new String("如果要显示中文文件名.xls".getBytes("UTF-8"), "iso8859-1");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition","inline; filename=" + fileName);
//response.setHeader("Content-disposition","inline; filename=test1.xls");
//以上这行设定传送到前端浏览器时的档名为test1.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta charset="utf-8">
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
<style type="text/css">
.tb_head {
background-color: #339966;
}
</style>
<title>Excel档案呈现方式</title>
</head>
<body>
<table border="1" width="100%">
<thead>
<tr>
<th class="tb_head" width="60" rowspan="2">名称</th>
<th class="tb_head" width="60" rowspan="2">型号</th>
<th class="tb_head" width="60" rowspan="2">经理</th>
<th class="tb_head" width="60" rowspan="2"">状态</th>
<th class="tb_head" width="60" rowspan="2">平台</th>
<th class="tb_head" width="70" rowspan="2">描述</th>
<th class="tb_head" width="112.5" rowspan="2">时间点</th>
<th class="tb_head" width="417.75" bgcolor="#ffff00">项目进展</th>
</tr>
<tr>
<th>2月第5周</th>
</tr>
</thead>
<tbody>
<s:iterator value="#request.list" id="list" status="status">
<tr>
<td><s:property value="project.name==''|project.name==null?project.tmpName:project.name"/></td>
<td>${project.foreignType }</td>
<td><s:property value="project.managerId!=0?project.manager.realname:''"/></td>
<td><s:property value="project.projectStatusId!=0?project.projectStatus.name:''"/></td>
<td><s:property value="project.hardwarePlatformId!=0?project.hardwarePlatform.name:''"/></td>
<td>产品描述</td>
<td>【生成定型】1212121</td>
<td>${content}</td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
</html>
注意一点:就是我的content是使用富文本的,所以他有很多<div>标签,这个导致生成的excel文档中content内容不再同一个单元格里面。所以,我们需要在保存富文本的时候,把<div><br>可以导致换行的string给替换掉
如:content = content.replace("</div>", "<br style='mso-data-placement:same-cell;' />");
当表格中出现rowspan字样要小心了,它会出现空行,这时候需要在span那个tr后面补全td,并且把下一行的tr给去掉。tr从第三行再加上。
【重要】:两个边框问题
1、设置表格内的边框: border="1"
2、表格之外的边框:见代码红色部分;代码红色显示不出来。。
一个是:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
另一个是:
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
参考资料:
http://www.cnblogs.com/cannel/archive/2011/06/30/2094189.html