Rising Sun

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  148 随笔 :: 0 文章 :: 22 评论 :: 0 Trackbacks
在导出数据生成excel时,定义excel单元格格式为文本。
最好拿你的导出代码贴出来看看,主要是生成excel时代码

--------------------------------------------------------------------------------

参考一下.
<% @ page contentType = " text/html; charset=gb2312 " %>
<% @ page  import = " java.io.* " %>
<% @ page  import = " org.apache.poi.hssf.util.HSSFColor " %>
<% @ page  import = " org.apache.poi.hssf.util.Region " %>
<% @ page  import = " org.apache.poi.hssf.usermodel.* " %>
<%
// 初始化
// 工作簿
HSSFWorkbook wb = new  HSSFWorkbook();
// 工作表
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(
0 , " Excel演示! " ,HSSFWorkbook.ENCODING_UTF_16);
// 准备完成
// 建样式
// 标题字
HSSFFont font_Header = wb.createFont();
font_Header.setFontName(
" headerFont " );
font_Header.setFontHeightInPoints((
short ) 12 );
HSSFCellStyle cellStyle_Header
= wb.createCellStyle();
cellStyle_Header.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle_Header.setFont(font_Header);
// 通用行
HSSFCellStyle cellStyle_Normal = wb.createCellStyle();
cellStyle_Normal.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// cellStyle_Normal.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderRight(HSSFCellStyle.BORDER_THIN);
// cellStyle_Normal.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 表格头
HSSFCellStyle cellStyle_Column = wb.createCellStyle();
cellStyle_Column.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle_Column.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle_Column.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle_Column.setFillPattern(HSSFCellStyle.BIG_SPOTS);
cellStyle_Column.setFillBackgroundColor((
short )HSSFColor.WHITE.index);
cellStyle_Column.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
// 数据行
HSSFCellStyle cellStyle_Cell = wb.createCellStyle();
cellStyle_Cell.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cellStyle_Cell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle_Cell.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle_Cell.setWrapText(
true );
// 样式结束
// 置标题
HSSFRow row = sheet.createRow(( short ) 0 );
HSSFCell cell
= row.createCell(( short ) 0 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" Excel演示! " );
sheet.addMergedRegion(
new  Region( 0 ,( short ) 0 , 0 ,( short ) 5 ));
cell.setCellStyle(cellStyle_Header);
// 完成标题
// 行信息
row = sheet.createRow(( short ) 1 );
cell
= row.createCell(( short ) 0 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldName " );
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 1 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldValue " );
sheet.addMergedRegion(
new  Region( 1 ,( short ) 1 , 1 ,( short ) 2 ));
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 3 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldName " );
cell.setCellStyle(cellStyle_Normal);
cell
= row.createCell(( short ) 4 );
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" FieldValue " );
sheet.addMergedRegion(
new  Region( 1 ,( short ) 4 , 1 ,( short ) 5 ));
cell.setCellStyle(cellStyle_Normal);
// 表数据
for ( int  mIndex = 2 ;mIndex < 10 ;mIndex ++ )
{
row
= sheet.createRow(( short )mIndex);
for ( int  nIndex = 0 ;nIndex < 6 ;nIndex ++ )
{
cell
= row.createCell(( short )nIndex);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue(
" 00.00 " );
cell.setCellStyle(cellStyle_Cell);
}

}

// 全局调
sheet.setHorizontallyCenter( true );
for ( int  kIndex = 0 ;kIndex < 10 ;kIndex ++ )
{
sheet.setColumnWidth((
short )kIndex,( short ) 4500 );
}

sheet.setMargin(HSSFSheet.BottomMargin,(
double ) 0.5 );
sheet.setMargin(HSSFSheet.LeftMargin,(
double ) 0.1 );
sheet.setMargin(HSSFSheet.RightMargin,(
double ) 0.1 );
sheet.setMargin(HSSFSheet.TopMargin,(
double ) 0.5 );
// 调整结束
// 输出Excel
OutputStream outData = null ;
outData
= response.getOutputStream();
response.setContentType(
" application/vnd.ms-excel " );
wb.write(outData);
outData.flush();
response.flushBuffer();
// 完成
%>
posted on 2006-09-19 15:59 brock 阅读(385) 评论(0)  编辑  收藏 所属分类: 处理Excel poi

只有注册用户登录后才能发表评论。


网站导航: