当前页面: 开发资料首页 → J2EE 专题 → 使用POI生成真正的电子表格文件
摘要: 使用POI生成真正的电子表格文件
有很多文章都说到了 cfm 生成电子表格的问题 :)
很多都是生成一个 txt 文件 用 cfhead 修改文件头信息骗浏览器
<cfset tabchar=chr(9) />
<cfset newline=chr(13)&chr(10) />
<cfcontent type="application/msexcel; charset=gbk" >
<cfoutput>#string#</cfoutput>
变化也就这几种 当然好处也比较明显了 txt 文件可以被绝大多数的 office 工具正确的转化 并显示内容
不过 如果我们需要生成一个真正的电子表格文件的时候呢,或者我需要生成一个包含几个工作表的 excel 文件的时候,这种方法就显得力不从心了 我们需要用到 apache POI 项目
http://jakarta.apache.org/poi/hssf/quick-guide.html
在这里可以参考相关的资料 首先你需要下载 poi.jar 文件并放到 cfusion/lib 目录下让 coldfusion 可以访问到 poi 的 class
然后
New Workbook
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
New Sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Creating Cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
接口和调用都很简单 只是既然选择了 coldfusion 我们应该可以做得更简单一些 因为项目中需要用到这个功能 简单的封装了几个最必要的功能
<cfcomponent>
<cffunction name="createWorkbook" access="public" output="false" returntype="struct">
<cfscript>
instance = structnew();
instance.HSSFWorkbook = createObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").init();
</cfscript>
<cfreturn this />
</cffunction>
<cffunction name="save" access="public" output="false" returntype="void">
<cfargument name="file" required="no" default="Sheet1" type="string" />
<cfscript>
// Init File Stream
instance.FileOutputStream = createObject("java","java.io.FileOutputStream").init(arguments.file);
instance.HSSFWorkbook.write(instance.FileOutputStream);
instance.FileOutputStream.close();
</cfscript>
</cffunction>
<cffunction name="createSheet" access="public" output="false" returntype="void">
<cfargument name="SheetName" required="no" default="Sheet1" type="string" />
<cfscript>
// Create sheet
instance.HSSFSheet = instance.HSSFWorkbook.createSheet();
// Alert sheet name
instance.HSSFWorkbook.setSheetName(0, arguments.SheetName, instance.HSSFWorkbook.ENCODING_UTF_16);
</cfscript>
</cffunction>
<cffunction name="setSheetName" access="public" output="false" returntype="void">
<cfargument name="SheetName" required="no" default="Sheet1" type="string" />
<cfscript>
// Alert sheet name
instance.HSSFWorkbook.setSheetName(0, arguments.SheetName, instance.HSSFWorkbook.ENCODING_UTF_16);
</cfscript>
</cffunction>
<cffunction name="createRow" access="public" output="false" returntype="void">
<cfargument name="RowsNumber" required="no" default="0" type="numeric" />
<cfscript>
// Create a row and put some cells in it. Rows are 0 based.
instance.HSSFRow = instance.HSSFSheet.createRow(arguments.RowsNumber);
</cfscript>
</cffunction>
<cffunction name="createCell" access="public" output="false" returntype="void">
<cfargument name="Column" required="no" default="0" type="numeric" />
<cfargument name="CellValue" required="no" default="" type="string" />
<cfscript>
// Create a cell and put a value in it.
instance.HSSFCell = instance.HSSFRow.createCell(arguments.Column);
instance.HSSFCell.setencoding(instance.HSSFCell.encoding_UTF_16);
instance.HSSFCell.setCellValue(javacast("String",arguments.CellValue));
</cfscript>
</cffunction>
</cfcomponent>
实际上就是把上面 java 的几个功能封装到 cfc 中了 调用的方式像下面
temp_file = application._physical_cache_jdbc_dir & application._separator & createUUID() & ".tempxls";
rs_agents = createobject("component","#application._components_prefix#database.mail.utility").get_agent_fed();
obj = createobject("component","#application._components_prefix#xls.entity").createWorkbook();
obj.createSheet("信息员反馈信息");
obj.createRow(0);
obj.createCell(0,"信息员编号");
obj.createCell(1,"姓名");
obj.createCell(2,"教学班");
obj.createCell(3,"反馈内容");
obj.createCell(4,"教务处回复");
obj.createCell(5,"教学周");
obj.createCell(6,"日期");
while(rs_agents.next()){
obj.createRow(rs_agents.currentrow);
obj.createCell(0,rs_agents.sender_id);
obj.createCell(1,rs_agents.stu_name);
obj.createCell(2,rs_agents.taskclass_chs);
obj.createCell(3,xls_safe(rs_agents.mail_content));
obj.createCell(4,iif(rs_agents.reply_content neq "", de("#xls_safe(rs_agents.reply_content)#"), de("无")));
obj.createCell(5,rs_agents.current_week);
obj.createCell(6,LSDateFormat(rs_agents.mail_senttime,"YYYY-MM-DD"));
}
obj.save(temp_file);
可以直接生成一个 xls 文件