生成查询读取Excel工具POIWord格式文档下载.docx

上传人:b****5 文档编号:19528141 上传时间:2023-01-07 格式:DOCX 页数:40 大小:163.16KB
下载 相关 举报
生成查询读取Excel工具POIWord格式文档下载.docx_第1页
第1页 / 共40页
生成查询读取Excel工具POIWord格式文档下载.docx_第2页
第2页 / 共40页
生成查询读取Excel工具POIWord格式文档下载.docx_第3页
第3页 / 共40页
生成查询读取Excel工具POIWord格式文档下载.docx_第4页
第4页 / 共40页
生成查询读取Excel工具POIWord格式文档下载.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

生成查询读取Excel工具POIWord格式文档下载.docx

《生成查询读取Excel工具POIWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《生成查询读取Excel工具POIWord格式文档下载.docx(40页珍藏版)》请在冰豆网上搜索。

生成查询读取Excel工具POIWord格式文档下载.docx

importorg.apache.poi.hssf.usermodel.HSSFFont;

importorg.apache.poi.hssf.usermodel.HSSFRow;

importorg.apache.poi.hssf.usermodel.HSSFSheet;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

importorg.apache.poi.hssf.util.HSSFColor;

先看poi的examples包中提供的最简单的例子,建立一个空xls文件。

importjava.io.FileOutputStream;

importjava.io.IOException;

publicclassExcelSample1{

publicstaticvoidmain(String[]args)throwsIOException{

//创建一个excel文件

HSSFWorkbookwb=newHSSFWorkbook();

FileOutputStreamfileOut=newFileOutputStream("

c:

\\workbook.xls"

);

//FileOutputStreamfileOut=newFileOutputStream("

/workbook.xls"

wb.write(fileOut);

fileOut.close();

}

}

通过这个例子,我们在c盘下建立的是一个空白的xls文件(不是空文件)。

在此基础上,我们可以进一步看其它的例子。

importorg.apache.poi.hssf.usermodel.*;

publicclassCreateCells

{

publicstaticvoidmain(String[]args)throwsIOException

HSSFWorkbookwb=newHSSFWorkbook();

//建立新HSSFWorkbook对象

HSSFSheetsheet=wb.createSheet("

newsheet"

//建立新的sheet对象

HSSFRowrow=sheet.createRow((short)0);

//在sheet里创建一行,参数为行号(第一行,此处可想象成数组)

HSSFCellcell=row.createCell((short)0);

//在row里建立新cell(单元格),参数为列号(第一列)

cell.setCellvalue

(1);

//设置cell的整数类型的值

row.createCell((short)1).setCellvalue(1.2);

//设置cell浮点类型的值

row.createCell((short)2).setCellvalue("

test"

//设置cell字符类型的值

row.createCell((short)3).setCellvalue(true);

//设置cell布尔类型的值

HSSFCellStylecellStyle=wb.createCellStyle();

//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("

m/d/yyh:

mm"

));

//设置cell样式为定制的日期格式

HSSFCelldCell=row.createCell((short)4);

dCell.setCellvalue(newDate());

//设置cell为日期类型的值

dCell.setCellStyle(cellStyle);

//设置该cell日期的显示格式

HSSFCellcsCell=row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设置cell编码解决中文高位字节截断

csCell.setCellvalue("

中文测试_ChineseWordsTest"

//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);

//建立错误cell

FileOutputStreamfileOut=newFileOutputStream("

workbook.xls"

wb.write(fileOut);

fileOut.close();

通过这个例子,我们可以清楚的看到xls文件从大到小包括了HSSFWorkbookHSSFSheetHSSFRowHSSFCell这样几个对象。

我们可以在cell中设置各种类型的值。

尤其要注意的是如果你想正确的显示非欧美的字符时,尤其象中日韩这样的语言,必须设置编码为16位的即是HSSFCell.ENCODING_UTF_16,才能保证字符的高8位不被截断而引起编码失真形成乱码。

其他测试可以通过参考examples包中的测试例子掌握poi的详细用法,包括字体的设置,cell大小和低纹的设置等。

需要注意的是POI是一个仍然在完善中的公开代码的项目,所以有些功能正在不断的扩充。

感觉上面的操作比较的繁琐,然后就自己写了一个方法。

这个方法不需要事先创建row和cell,直接进行cteateCell就可以了,在程序中会自动进行判断,如果不存在的话会创建。

privatestaticvoidcteateCell(HSSFWorkbookwb,HSSFRowrow,shortcol,shortalign,Stringval){

HSSFCellcell=row.createCell(col);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(val);

HSSFCellStylecellstyle=wb.createCellStyle();

cellstyle.setAlignment(align);

cell.setCellStyle(cellstyle);

对里面的几个参数的说明:

shortcol应该是你的cell单元格的位置也就是列号;

shortalign应该是你的对齐方式;

Stringval应该是你单元格里面要添加的值;

具体的调用如下:

HSSFRowrow=sheet.createRow((short)1);

cteateCell(wb,row,(short)0,HSSFCellStyle.ALIGN_CENTER_SELECTION,"

SampleID"

在上边的例子里我们看到了要设置一个单元格里面信息的格式(例如,要将信息居中)设置的操作如下:

HSSFCellStylecellstyle=wb.createCellStyle();

cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);

cell.setCellStyle(cellstyle);

还有我们我们经常会用到的合并单元格,在这里我们也有这样的操作,代码如下:

sheet.addMergedRegion(newRegion(1,(short)1,2,(short)4));

这里面我们还要介绍一个经常会遇到的问题,就是怎么来冻结一个窗口。

poi也为我们集成了这样的事情了。

代码如下:

sheet.createFreezePane(1,2);

●在这里我们需要注意的是

一、该方法是在一个具体的sheet里面来进行操作。

二、方法createFreezepane;

有2个参数。

前一个参数代表列;

后一个参数代表行。

上边的代码对应的excel文件如下:

我么在画面上看到了明显的两条黑线,这就是冻结的窗口。

然后我们来看一个完整的STRUTS的小例子,在这个例子里面我们要做的事情是要模拟移动公司的网上营业厅里面的一个功能,我们要把一个客户当月的通话记录和各种信息查询出来,并且生成一张excel报表。

首先,我们来看一下网上效果的截图。

然后就是我们具体的代码实现了。

struts-config.xml

<

?

xmlversion="

1.0"

encoding="

UTF-8"

>

!

DOCTYPEstruts-configPUBLIC"

-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN"

"

http:

//struts.apache.org/dtds/struts-config_1_2.dtd"

struts-config>

<

action-mappings>

action

path="

/search"

type="

action.SearchAction"

<

forwardname="

success"

path="

/detial.jsp"

/>

/action>

/down"

action.DownAction"

display"

/down.jsp"

/>

/action-mappings>

message-resourcesparameter="

ApplicationResources"

/struts-config>

index.jsp

%@pagecontentType="

text/html;

charset=gb2312"

language="

java"

%>

html>

head>

title>

欢迎进入POI-Excel文件报表系统<

/title>

/head>

body>

tablealign="

center"

tr>

<

tdalign="

/td>

/tr>

ahref="

%=request.getContextPath()%>

/search.do"

进入查询页面<

/a>

/table>

/body>

/html>

连接数据库的SQLBean,这个bean和我们之前在分页里面用到的bean是一样的。

packagebean;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.Statement;

publicclassSQLBean

Stringurl="

jdbc:

microsoft:

sqlserver:

//localhost:

1433;

DatabaseName=poi_mvc"

;

Connectioncon=null;

Statementsta=null;

publicSQLBean()

{

try

Class.forName("

com.microsoft.jdbc.sqlserver.SQLServerDriver"

con=DriverManager.getConnection(url,"

sa"

"

"

sta=con.createStatement();

catch(Exceptione)

e.printStackTrace();

publicResultSetselect(Stringselects)throwsException

returnsta.executeQuery(selects);

为了体现MVC模式,我们在这里面引入了一个存储数据的Bean。

EntityBean.java

publicclassEntityBean{

privateStringsjhm;

privateStringhjlx;

privateStringdfhm;

privateStringqssj;

privateStringthsc;

privateStringthdd;

privateStringctlx;

privateStringjbhf;

privateStringchf;

privateStringzhf;

privateStringzjls;

publicEntityBean(Stringsjhm,Stringhjlx,Stringdfhm,Stringqssj,

Stringthsc,Stringthdd,Stringctlx,Stringjbhf,

Stringchf,Stringzhf){

this.sjhm=sjhm;

this.hjlx=hjlx;

this.dfhm=dfhm;

this.qssj=qssj;

this.thsc=thsc;

this.thdd=thdd;

this.ctlx=ctlx;

this.jbhf=jbhf;

this.chf=chf;

this.zhf=zhf;

publicStringgetChf(){

returnchf;

publicvoidsetChf(Stringchf){

this.chf=chf;

publicStringgetCtlx(){

returnctlx;

publicvoidsetCtlx(Stringctlx){

this.ctlx=ctlx;

publicStringgetDfhm(){

returndfhm;

publicvoidsetDfhm(Stringdfhm){

this.dfhm=dfhm;

publicStringgetHjlx(){

returnhjlx;

publicvoidsetHjlx(Stringhjlx){

this.hjlx=hjlx;

publicStringgetJbhf(){

returnjbhf;

publicvoidsetJbhf(Stringjbhf){

this.jbhf=jbhf;

publicStringgetQssj(){

returnqssj;

publicvoidsetQssj(Stringqssj){

this.qssj=qssj;

publicStringgetSjhm(){

returnsjhm;

publicvoidsetSjhm(Stringsjhm){

this.sjhm=sjhm;

publicStringgetThdd(){

returnthdd;

publicvoidsetThdd(Stringthdd){

this.thdd=thdd;

publicStringgetThsc(){

returnthsc;

publicvoidsetThsc(Stringthsc){

this.thsc=thsc;

publicStringgetZhf(){

returnzhf;

publicvoidsetZhf(Stringzhf){

this.zhf=zhf;

publicStringgetZjls(){

returnzjls;

publicvoidsetZjls(Stringzjls){

this.zjls=zjls;

}

然后让我们来看看SearchAction,他的主要功能把数据库里的数据提取出来,然后封装到EntityBean里面,在转发到一个新的页面。

packageaction;

importjava.util.ArrayList;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importmons.logging.Log;

importmons.logging.LogFactory;

importorg.apache.struts.action.Action;

importorg.apache.struts.action.ActionForm;

importorg.apache.struts.action.ActionForward;

importorg.apache.struts.action.ActionMapping;

importbean.EntityBean;

importbean.SQLBean;

publicclassSearchActionextendsAction{

publicLoglog=LogFactory.getLog("

Poi_mvc"

publicActionForwardexecute(

ActionMappingmapping,

ActionFormform,

HttpServletRequestrequest,

HttpServletResponseresponse){

ArrayListlist=newArrayList();

SQLBeansq=newSQLBean();

Stringsql="

select*fromdetial"

try{

ResultSetres=sq.select(sql);

while(res.next()){

Stringsjhm=res.getString("

sjhm"

Stringhjlx=res.getString("

hjlx"

Stringdfhm=res.getString("

dfhm"

Stringqssj=res.getString("

qssj"

Stringthsc=res.getString("

thsc"

Stringthdd=res.getString("

thdd"

Stringctlx=res.getString("

ctlx"

Stringjbhf=res.getString("

jbhf"

Stringchf=res.getString("

chf"

Stringzhf=res.getString("

zhf"

log.info("

开始封装数据:

手机号码="

+sjhm+"

呼叫类型"

+hjlx+"

对方号码"

+dfhm+"

起始时间"

+qssj+"

通话时间"

+thsc+"

通话地点"

+thdd+"

长途类型"

+ctlx+"

基本话费"

+jbhf+"

常话费"

+chf+"

总话费"

+zhf+"

总纪录"

+i);

EntityBeaneb=newEntityBean(sjhm,hjlx,dfhm,qssj,thsc,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1