﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Rising Sun -随笔分类-处理Excel poi</title><link>http://www.blogjava.net/brock/category/15415.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 19:53:25 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 19:53:25 GMT</pubDate><ttl>60</ttl><item><title>excel poi 处理2</title><link>http://www.blogjava.net/brock/archive/2006/09/19/70548.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Tue, 19 Sep 2006 07:59:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/09/19/70548.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/70548.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/09/19/70548.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/70548.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/70548.html</trackback:ping><description><![CDATA[
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">在导出数据生成excel时，定义excel单元格格式为文本。<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />最好拿你的导出代码贴出来看看，主要是生成excel时代码<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #000000">--------------------------------------------------------------------------------</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />参考一下.<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">@ page contentType</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">text/html; charset=gb2312</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">@ page </span>
				<span style="COLOR: #0000ff">import</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">java.io.*</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">@ page </span>
				<span style="COLOR: #0000ff">import</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">org.apache.poi.hssf.util.HSSFColor</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">@ page </span>
				<span style="COLOR: #0000ff">import</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">org.apache.poi.hssf.util.Region</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">@ page </span>
				<span style="COLOR: #0000ff">import</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">org.apache.poi.hssf.usermodel.*</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">&lt;%</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">初始化<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">工作簿</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFWorkbook wb</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> HSSFWorkbook();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">工作表</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFSheet sheet</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createSheet();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />wb.setSheetName(</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">Excel演示!</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">,HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">准备完成<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">建样式<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">标题字</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFFont font_Header</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createFont();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />font_Header.setFontName(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">headerFont</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />font_Header.setFontHeightInPoints((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">12</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />HSSFCellStyle cellStyle_Header</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createCellStyle();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Header.setAlignment(HSSFCellStyle.ALIGN_CENTER);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Header.setFont(font_Header);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">通用行</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFCellStyle cellStyle_Normal</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createCellStyle();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Normal.setAlignment(HSSFCellStyle.ALIGN_LEFT);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">cellStyle_Normal.setBorderBottom(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">cellStyle_Normal.setBorderLeft(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">cellStyle_Normal.setBorderRight(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">cellStyle_Normal.setBorderTop(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">表格头</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFCellStyle cellStyle_Column</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createCellStyle();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setAlignment(HSSFCellStyle.ALIGN_CENTER);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setBorderBottom(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setBorderLeft(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setBorderRight(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setBorderTop(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setFillPattern(HSSFCellStyle.BIG_SPOTS);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setFillBackgroundColor((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)HSSFColor.WHITE.index);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Column.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">数据行</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFCellStyle cellStyle_Cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">wb.createCellStyle();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setAlignment(HSSFCellStyle.ALIGN_LEFT);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setBorderBottom(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setBorderLeft(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setBorderRight(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setBorderTop(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle_Cell.setWrapText(</span>
				<span style="COLOR: #0000ff">true</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">样式结束<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">置标题</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFRow row</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">sheet.createRow((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />HSSFCell cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">row.createCell((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellValue(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">Excel演示!</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.addMergedRegion(</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Region(</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">5</span>
				<span style="COLOR: #000000">));<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellStyle(cellStyle_Header);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">完成标题<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">行信息</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">row</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">sheet.createRow((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">row.createCell((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellValue(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">FieldName</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellStyle(cellStyle_Normal);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">row.createCell((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellValue(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">FieldValue</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.addMergedRegion(</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Region(</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">));<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellStyle(cellStyle_Normal);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">row.createCell((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">3</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellValue(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">FieldName</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellStyle(cellStyle_Normal);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">row.createCell((</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">4</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellValue(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">FieldValue</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.addMergedRegion(</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Region(</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">4</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,(</span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">5</span>
				<span style="COLOR: #000000">));<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cell.setCellStyle(cellStyle_Normal);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">表数据</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> mIndex</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">;mIndex</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">10</span>
				<span style="COLOR: #000000">;mIndex</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_3331_3556_Open_Image" onclick="this.style.display='none'; Codehighlighter1_3331_3556_Open_Text.style.display='none'; Codehighlighter1_3331_3556_Closed_Image.style.display='inline'; Codehighlighter1_3331_3556_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_3331_3556_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_3331_3556_Closed_Text.style.display='none'; Codehighlighter1_3331_3556_Open_Image.style.display='inline'; Codehighlighter1_3331_3556_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_3331_3556_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_3331_3556_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />row</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000">sheet.createRow((</span>
						<span style="COLOR: #0000ff">short</span>
						<span style="COLOR: #000000">)mIndex);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /></span>
						<span style="COLOR: #0000ff">for</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> nIndex</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000">;nIndex</span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #000000">6</span>
						<span style="COLOR: #000000">;nIndex</span>
						<span style="COLOR: #000000">++</span>
						<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_3405_3554_Open_Image" onclick="this.style.display='none'; Codehighlighter1_3405_3554_Open_Text.style.display='none'; Codehighlighter1_3405_3554_Closed_Image.style.display='inline'; Codehighlighter1_3405_3554_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_3405_3554_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_3405_3554_Closed_Text.style.display='none'; Codehighlighter1_3405_3554_Open_Image.style.display='inline'; Codehighlighter1_3405_3554_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_3405_3554_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_3405_3554_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />cell</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">row.createCell((</span>
								<span style="COLOR: #0000ff">short</span>
								<span style="COLOR: #000000">)nIndex);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />cell.setCellValue(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">00.00</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />cell.setCellStyle(cellStyle_Cell);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />}</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">全局调</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">sheet.setHorizontallyCenter(</span>
				<span style="COLOR: #0000ff">true</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> kIndex</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;kIndex</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">10</span>
				<span style="COLOR: #000000">;kIndex</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_3636_3687_Open_Image" onclick="this.style.display='none'; Codehighlighter1_3636_3687_Open_Text.style.display='none'; Codehighlighter1_3636_3687_Closed_Image.style.display='inline'; Codehighlighter1_3636_3687_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_3636_3687_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_3636_3687_Closed_Text.style.display='none'; Codehighlighter1_3636_3687_Open_Image.style.display='inline'; Codehighlighter1_3636_3687_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_3636_3687_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_3636_3687_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />sheet.setColumnWidth((</span>
						<span style="COLOR: #0000ff">short</span>
						<span style="COLOR: #000000">)kIndex,(</span>
						<span style="COLOR: #0000ff">short</span>
						<span style="COLOR: #000000">)</span>
						<span style="COLOR: #000000">4500</span>
						<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setMargin(HSSFSheet.BottomMargin,(</span>
				<span style="COLOR: #0000ff">double</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0.5</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setMargin(HSSFSheet.LeftMargin,(</span>
				<span style="COLOR: #0000ff">double</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0.1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setMargin(HSSFSheet.RightMargin,(</span>
				<span style="COLOR: #0000ff">double</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0.1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setMargin(HSSFSheet.TopMargin,(</span>
				<span style="COLOR: #0000ff">double</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">0.5</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">调整结束<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">输出Excel</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">OutputStream outData</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />outData</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">response.getOutputStream();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />response.setContentType(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">application/vnd.ms-excel</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />wb.write(outData);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />outData.flush();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />response.flushBuffer();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">完成</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">%&gt;</span>
		</div>
<img src ="http://www.blogjava.net/brock/aggbug/70548.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-09-19 15:59 <a href="http://www.blogjava.net/brock/archive/2006/09/19/70548.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Busy Developers' Guide to HSSF Features  (说明书)</title><link>http://www.blogjava.net/brock/archive/2006/09/14/69688.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Thu, 14 Sep 2006 08:47:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/09/14/69688.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/69688.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/09/14/69688.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/69688.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/69688.html</trackback:ping><description><![CDATA[
		<div class="h4">
				<h4>Features</h4>
		</div>
		<a name="NewWorkbook">
		</a>
		<div class="h2">
				<h2>New Workbook</h2>
		</div>
		<pre class="code">    //创建新的Excel 工作簿<br />    HSSFWorkbook wb = new HSSFWorkbook();<br />     //new 一个FileOutputStream 
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");<br />     //写入流
    wb.write(fileOut); 
    fileOut.close();
                    </pre>
		<a name="NewSheet">
		</a>
		<div class="h2">
				<h2>New Sheet</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();<br />　  // 如要新建一名为"效益指标"的工作表，其语句为：<br />  　// HSSFSheet sheet = workbook.createSheet("效益指标");<br />
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="CreateCells">
		</a>
		<div class="h2">
				<h2>Creating Cells</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.<br /><br />     //创建一行并放一些元素值 ,0是起始行
    HSSFRow row = sheet.createRow((short)0);
    // Create a cell and put a value in it.<br />     创建一个单元格 
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(1);

    // Or do it on one line.<br />     创建单元格也可以一行代码
    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<br />     
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="CreateDateCells">
		</a>
		<div class="h2">
				<h2>Creating Date Cells</h2>
		</div>
		<pre class="code">    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 date value in it.  The first cell is not styled
    // as a date.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time).  It is important to
    // create a new cell style from the workbook otherwise you can end up
    // modifying the built in style and effecting not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    cell = row.createCell((short)1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="CellTypes">
		</a>
		<div class="h2">
				<h2>Working with different types of cells不同类型的单元格一起工作</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)2);
    row.createCell((short) 0).setCellValue(1.1);
    row.createCell((short) 1).setCellValue(new Date());
    row.createCell((short) 2).setCellValue("a string");
    row.createCell((short) 3).setCellValue(true);
    row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="Alignment">
		</a>
		<div class="h2">
				<h2>Demonstrates various alignment options示范不同的对齐选项</h2>
		</div>
		<pre class="code">    public static void main(String[] args)
            throws IOException
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row = sheet.createRow((short) 2);
        createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
        createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
        createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
        createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
        createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
        createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
        createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();

    }

    /**
     * Creates a cell and aligns it a certain way.
     *
     * @param wb        the workbook
     * @param row       the row to create the cell in
     * @param column    the column number to create the cell in
     * @param align     the alignment for the cell.
     */
    private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
    {
        HSSFCell cell = row.createCell(column);
        cell.setCellValue("Align It");
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(align);
        cell.setCellStyle(cellStyle);
    }
                    </pre>
		<a name="Borders">
		</a>
		<div class="h2">
				<h2>Working with borders设置边框</h2>
		</div>
		<pre class="code">    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) 1);

    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue(4);

    // Style the cell with borders all around.
    HSSFCellStyle style = wb.createCellStyle();
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setLeftBorderColor(HSSFColor.GREEN.index);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setRightBorderColor(HSSFColor.BLUE.index);
    style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
    style.setTopBorderColor(HSSFColor.BLACK.index);
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="FillsAndFrills">
		</a>
		<div class="h2">
				<h2>Fills and colors填充 和 颜色</h2>
		</div>
		<pre class="code">    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) 1);

    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.AQUA.index);
    style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("X");
    cell.setCellStyle(style);

    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue("X");
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="MergedCells">
		</a>
		<div class="h2">
				<h2>Merging cells 合并单元格</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    HSSFRow row = sheet.createRow((short) 1);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");

    sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="WorkingWithFonts">
		</a>
		<div class="h2">
				<h2>Working with fonts 字体</h2>
		</div>
		<pre class="code">    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) 1);

    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);

    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);

    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="CustomColors">
		</a>
		<div class="h2">
				<h2>Custom colors 自定义颜色</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("Default Palette");

    //apply some colors from the standard palette,
    // as in the previous examples.
    //we'll use red text on a lime background

    HSSFCellStyle style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.LIME.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.RED.index);
    style.setFont(font);

    cell.setCellStyle(style);

    //save with the default palette
    FileOutputStream out = new FileOutputStream("default_palette.xls");
    wb.write(out);
    out.close();

    //now, let's replace RED and LIME in the palette
    // with a more attractive combination
    // (lovingly borrowed from freebsd.org)

    cell.setCellValue("Modified Palette");

    //creating a custom palette for the workbook
    HSSFPalette palette = wb.getCustomPalette();

    //replacing the standard red with freebsd.org red
    palette.setColorAtIndex(HSSFColor.RED.index,
            (byte) 153,  //RGB red (0-255)
            (byte) 0,    //RGB green
            (byte) 0     //RGB blue
    );
    //replacing lime with freebsd.org gold
    palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);

    //save with the modified palette
    // note that wherever we have previously used RED or LIME, the
    // new colors magically appear
    out = new FileOutputStream("modified_palette.xls");
    wb.write(out);
    out.close();
                    </pre>
		<a name="ReadWriteWorkbook">
		</a>
		<div class="h2">
				<h2>Reading and Rewriting Workbooks  读和 重写工作簿</h2>
		</div>
		<pre class="code">    POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("workbook.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(2);
    HSSFCell cell = row.getCell((short)3);
    if (cell == null)
        cell = row.createCell((short)3);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue("a test");

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="NewLinesInCells">
		</a>
		<div class="h2">
				<h2>Using newlines in cells  换行</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();

    cs = wb.createCellStyle();

    cs.setFont( f2 );
    //Word Wrap MUST be turned on
    cs.setWrapText( true );

    r = s.createRow( (short) 2 );
    r.setHeight( (short) 0x349 );
    c = r.createCell( (short) 2 );
    c.setCellType( HSSFCell.CELL_TYPE_STRING );
    c.setCellValue( "Use \n with word wrap on to create a new line" );
    c.setCellStyle( cs );
    s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );

    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();</pre>
		<a name="DataFormats">
		</a>
		<div class="h2">
				<h2>Data Formats  数据格式</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFCellStyle style;
    HSSFDataFormat format = wb.createDataFormat();
    HSSFRow row;
    HSSFCell cell;
    short rowNum = 0;
    short colNum = 0;

    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);

    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="FitTo">
		</a>
		<div class="h2">
				<h2>Fit Sheet to One Page   一页中显视合适的工作表</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFPrintSetup ps = sheet.getPrintSetup();
    
    sheet.setAutobreaks(true);
    
    ps.setFitHeight((short)1);
    ps.setFitWidth((short)1);


    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="PrintArea2">
		</a>
		<div class="h2">
				<h2>Set Print Area 设置打印区</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    wb.setPrintArea(0, "$A$1:$C$2");
    //sets the print area for the first sheet
    //Alternatively:
    //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)  

    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    
    
                    </pre>
		<a name="FooterPageNumbers">
		</a>
		<div class="h2">
				<h2>Set Page Numbers on Footer 设置页数 在页用中</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFFooter footer = sheet.getFooter()
    
    footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
    


    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="ConvenienceFunctions">
		</a>
		<div class="h2">
				<h2>Using the Convenience Functions  行用方便的方法</h2>
		</div>
		<p>The convenience functions live in contrib and provide utility features such as setting borders around merged regions and changing style attributes without explicitly creating new styles. </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet( "new sheet" );

    // Create a merged region
    HSSFRow row = sheet1.createRow( (short) 1 );
    HSSFRow row2 = sheet1.createRow( (short) 2 );
    HSSFCell cell = row.createCell( (short) 1 );
    cell.setCellValue( "This is a test of merging" );
    Region region = new Region( 1, (short) 1, 4, (short) 4 );
    sheet1.addMergedRegion( region );

    // Set the border and border colors.
    final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
    HSSFRegionUtil.setBorderBottom( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderTop( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderLeft( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderRight( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);

    // Shows some usages of HSSFCellUtil
    HSSFCellStyle style = wb.createCellStyle();
    style.setIndention((short)4);
    HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
    HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
    HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);

    // Write out the workbook
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
                    </pre>
		<a name="ShiftRows">
		</a>
		<div class="h2">
				<h2>Shift rows up or down on a sheet</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");

    // Create various cells and rows for spreadsheet.

    // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
    sheet.shiftRows(5, 10, -5);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="SelectSheet">
		</a>
		<div class="h2">
				<h2>Set a sheet as selected 设置为以选</h2>
		</div>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    sheet.setSelected(true);

    // Create various cells and rows for spreadsheet.

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="Zoom">
		</a>
		<div class="h2">
				<h2>Set the zoom magnification设置放大率</h2>
		</div>
		<p>The zoom is expressed as a fraction. For example to express a zoom of 75% use 3 for the numerator and 4 for the denominator. </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    sheet1.setZoom(3,4);   // 75 percent magnification
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="Splits">
		</a>
		<div class="h2">
				<h2>Splits and freeze panes</h2>
		</div>
		<p>There are two types of panes you can create; freeze panes and split panes. </p>
		<p>A freeze pane is split by columns and rows. You create a freeze pane using the following mechanism: </p>
		<p>sheet1.createFreezePane( 3, 2, 3, 2 ); </p>
		<p>The first two parameters are the columns and rows you wish to split by. The second two parameters indicate the cells that are visible in the bottom right quadrant. </p>
		<p>Split pains appear differently. The split area is divided into four separate work area's. The split occurs at the pixel level and the user is able to adjust the split by dragging it to a new position. </p>
		<p>Split panes are created with the following call: </p>
		<p>sheet2.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT ); </p>
		<p>The first parameter is the x position of the split. This is in 1/20th of a point. A point in this case seems to equate to a pixel. The second parameter is the y position of the split. Again in 1/20th of a point. </p>
		<p>The last parameter indicates which pane currently has the focus. This will be one of HSSFSheet.PANE_LOWER_LEFT, PANE_LOWER_RIGHT, PANE_UPPER_RIGHT or PANE_UPPER_LEFT. </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    HSSFSheet sheet3 = wb.createSheet("third sheet");
    HSSFSheet sheet4 = wb.createSheet("fourth sheet");

    // Freeze just one row
    sheet1.createFreezePane( 0, 1, 0, 1 );
    // Freeze just one column
    sheet2.createFreezePane( 1, 0, 1, 0 );
    // Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
    sheet3.createFreezePane( 2, 2 );
    // Create a split with the lower left side being the active quadrant
    sheet4.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="Repeating">
		</a>
		<div class="h2">
				<h2>Repeating rows and columns</h2>
		</div>
		<p>It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the HSSFWorkbook class. </p>
		<p>This function Contains 5 parameters. The first parameter is the index to the sheet (0 = first sheet). The second and third parameters specify the range for the columns to repreat. To stop the columns from repeating pass in -1 as the start and end column. The fourth and fifth parameters specify the range for the rows to repeat. To stop the columns from repeating pass in -1 as the start and end rows. </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");

    // Set the columns to repeat from column 0 to 2 on the first sheet
    wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
    // Set the the repeating rows and columns on the second sheet.
    wb.setRepeatingRowsAndColumns(1,4,5,1,2);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="HeaderFooter">
		</a>
		<div class="h2">
				<h2>Headers and Footers 页头 和 页脚</h2>
		</div>
		<p>Example is for headers but applies directly to footers. </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    HSSFHeader header = sheet.getHeader();
    header.setCenter("Center Header");
    header.setLeft("Left Header");
    header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + 
                    HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<a name="DrawingShapes">
		</a>
		<div class="h2">
				<h2>Drawing Shapes</h2>
		</div>
		<p>POI supports drawing shapes using the Microsoft Office drawing tools. Shapes on a sheet are organized in a hiearchy of groups and and shapes. The top-most shape is the patriarch. This is not visisble on the sheet at all. To start drawing you need to call <span class="codefrag">createPatriarch</span> on the <span class="codefrag">HSSFSheet</span> class. This has the effect erasing any other shape information stored in that sheet. By default POI will leave shape records alone in the sheet unless you make a call to this method. </p>
		<p>To create a shape you have to go through the following steps: </p>
		<ol>
				<li>Create the patriarch. 
</li>
				<li>Create an anchor to position the shape on the sheet. 
</li>
				<li>Ask the patriarch to create the shape. 
</li>
				<li>Set the shape type (line, oval, rectangle etc...) 
</li>
				<li>Set any other style details converning the shape. (eg: line thickness, etc...) </li>
		</ol>
		<pre class="code">    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
                    </pre>
		<p>Text boxes are created using a different call: </p>
		<pre class="code">    HSSFTextbox textbox1 = patriarch.createTextbox(
            new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
    textbox1.setString(new HSSFRichTextString("This is a test") );
                    </pre>
		<p>It's possible to use different fonts to style parts of the text in the textbox. Here's how: </p>
		<pre class="code">    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_DOUBLE);
    HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
    string.applyFont(2,5,font);
    textbox.setString(string );
                    </pre>
		<p>Just as can be done manually using Excel, it is possible to group shapes together. This is done by calling <span class="codefrag">createGroup()</span> and then creating the shapes using those groups. </p>
		<p>It's also possible to create groups within groups. </p>
		<div class="frame warning">
				<div class="label">Warning</div>
				<div class="content">Any group you create should contain at least two other shapes or subgroups.</div>
		</div>
		<p>Here's how to create a shape group: </p>
		<pre class="code">    // Create a shape group.
    HSSFShapeGroup group = patriarch.createGroup(
            new HSSFClientAnchor(0,0,900,200,(short)2,2,(short)2,2));

    // Create a couple of lines in the group.
    HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3,3,500,500));
    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
    ( (HSSFChildAnchor) shape1.getAnchor() ).setAnchor((short)3,3,500,500);
    HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)1,200,400,600));
    shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
                    </pre>
		<p>If you're being observant you'll noticed that the shapes that are added to the group use a new type of anchor: the <span class="codefrag">HSSFChildAnchor</span>. What happens is that the created group has it's own coordinate space for shapes that are placed into it. POI defaults this to (0,0,1023,255) but you are able to change it as desired. Here's how: </p>
		<pre class="code">    myGroup.setCoordinates(10,10,20,20); // top-left, bottom-right
                    </pre>
		<p>If you create a group within a group it's also going to have it's own coordinate space. </p>
		<a name="StylingShapes">
		</a>
		<div class="h2">
				<h2>Styling Shapes  </h2>
		</div>
		<p>By default shapes can look a little plain. It's possible to apply different styles to the shapes however. The sorts of things that can currently be done are: </p>
		<ul>
				<li>Change the fill color. 
</li>
				<li>Make a shape with no fill color. 
</li>
				<li>Change the thickness of the lines. 
</li>
				<li>Change the style of the lines. Eg: dashed, dotted. 
</li>
				<li>Change the line color. </li>
		</ul>
		<p>Here's an examples of how this is done: </p>
		<pre class="code">    HSSFSimpleShape s = patriarch.createSimpleShape(a);
    s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
    s.setLineStyleColor(10,10,10);
    s.setFillColor(90,10,200);
    s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
    s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
                    </pre>
		<a name="Graphics2d">
		</a>
		<div class="h2">
				<h2>Shapes and Graphics2d</h2>
		</div>
		<p>While the native POI shape drawing commands are the recommended way to draw shapes in a shape it's sometimes desirable to use a standard API for compatibility with external libraries. With this in mind we created some wrappers for <span class="codefrag">Graphics</span> and <span class="codefrag">Graphics2d</span>. </p>
		<div class="frame warning">
				<div class="label">Warning</div>
				<div class="content">It's important to not however before continuing that <span class="codefrag">Graphics2d</span> is a poor match to the capabilities of the Microsoft Office drawing commands. The older <span class="codefrag">Graphics</span> class offers a closer match but is still a square peg in a round hole. </div>
		</div>
		<p>All Graphics commands are issued into an <span class="codefrag">HSSFShapeGroup</span>. Here's how it's done: </p>
		<pre class="code">    a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
    group = patriarch.createGroup( a );
    group.setCoordinates( 0, 0, 80 * 4 , 12 * 23  );
    float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
    g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
    g2d = new EscherGraphics2d( g );
    drawChemicalStructure( g2d );
                    </pre>
		<p>The first thing we do is create the group and set it's coordinates to match what we plan to draw. Next we calculate a reasonable fontSizeMultipler then create the EscherGraphics object. Since what we really want is a <span class="codefrag">Graphics2d</span> object we create an EscherGraphics2d object and pass in the graphics object we created. Finally we call a routine that draws into the EscherGraphics2d object. </p>
		<p>The vertical points per pixel deserves some more explanation. One of the difficulties in converting Graphics calls into escher drawing calls is that Excel does not have the concept of absolute pixel positions. It measures it's cell widths in 'characters' and the cell heights in points. Unfortunately it's not defined exactly what type of character it's measuring. Presumably this is due to the fact that the Excel will be using different fonts on different platforms or even within the same platform. </p>
		<p>Because of this constraint we've had to implement the concept of a verticalPointsPerPixel. This the amount the font should be scaled by when you issue commands such as drawString(). To calculate this value use the follow formula: </p>
		<pre class="code">    multipler = groupHeightInPoints / heightOfGroup
                    </pre>
		<p>The height of the group is calculated fairly simply by calculating the difference between the y coordinates of the bounding box of the shape. The height of the group can be calculated by using a convenience called <span class="codefrag">HSSFClientAnchor.getAnchorHeightInPoints()</span>. </p>
		<p>Many of the functions supported by the graphics classes are not complete. Here's some of the functions that are known to work. </p>
		<ul>
				<li>fillRect() 
</li>
				<li>fillOval() 
</li>
				<li>drawString() 
</li>
				<li>drawOval() 
</li>
				<li>drawLine() 
</li>
				<li>clearRect() </li>
		</ul>
		<p>Functions that are not supported will return and log a message using the POI logging infrastructure (disabled by default). </p>
		<a name="Outlining">
		</a>
		<div class="h2">
				<h2>Outlining</h2>
		</div>
		<p>Outlines are great for grouping sections of information together and can be added easily to columns and rows using the POI API. Here's how: </p>
		<pre class="code">    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupRow( 5, 14 );
    sheet1.groupRow( 7, 14 );
    sheet1.groupRow( 16, 19 );

    sheet1.groupColumn( (short)4, (short)7 );
    sheet1.groupColumn( (short)9, (short)12 );
    sheet1.groupColumn( (short)10, (short)11 );

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
                    </pre>
		<p>To collapse (or expand) an outline use the following calls: </p>
		<pre class="code">    sheet1.setRowGroupCollapsed( 7, true );
    sheet1.setColumnGroupCollapsed( (short)4, true );
                    </pre>
		<p>The row/column you choose should contain an already created group. It can be anywhere within the group. </p>
		<a name="Images">
		</a>
		<div class="h3">
				<h3>Images</h3>
		</div>
		<p>Images are part of the drawing support. To add an image just call <span class="codefrag">createPicture()</span> on the drawing patriarch. At the time of writing the following types are supported: </p>
		<ul>
				<li>PNG 
</li>
				<li>JPG 
</li>
				<li>DIB </li>
		</ul>
		<p>It is not currently possible to read existing images and it should be noted that any existing drawings may be erased once you add a image to a sheet. </p>
		<pre class="code">    // Create the drawing patriarch.  This is the top level container for
    // all shapes. This will clear out any existing shapes for that sheet.
    HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();

    HSSFClientAnchor anchor;
    anchor = new HSSFClientAnchor(0,0,0,255,(short)2,2,(short)4,7);
    anchor.setAnchorType( 2 );
    patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
            </pre>
		<a name="NamedRanges">
		</a>
		<div class="h3">
				<h3>Named Ranges and Named Cells</h3>
		</div>
		<p>Named Range is a way to refer to a group of cells by a name. Named Cell is a degenerate case of Named Range in that the 'group of cells' contains exactly one cell. You can create as well as refer to cells in a workbook by their named range. When working with Named Ranges, the classes: org.apache.poi.hssf.util.CellReference and &amp; org.apache.poi.hssf.util.AreaReference are used. </p>
		<p>Creating Named Range / Named Cell </p>
		<pre class="code">    // setup code
    String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sname);
    sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);
     
    // 1. create named range for a single cell using areareference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1:A1"; // area reference
    namedCell.setReference(reference);
    
    // 2. create named range for a single cell using cellreference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1"; // cell reference
    namedCell.setReference(reference);
    
    // 3. create named range for an area using AreaReference
    HSSFName namedCell = wb.createName();
    namedCell.setNameName(cname);
    String reference = sname+"!A1:C5"; // area reference
    namedCell.setReference(reference);
    
            </pre>
		<p>Reading from Named Range / Named Cell </p>
		<pre class="code">    // setup code
    String cname = "TestName";
    HSSFWorkbook wb = getMyWorkbook(); // retrieve workbook

    // retrieve the named range
    int namedCellIdx = wb.getNameIndex(cellName);
    HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
    
    // retrieve the cell at the named range and test its contents
    AreaReference aref = new AreaReference(aNamedCell.getReference());
    CellReference[] crefs = aref.getCells();
    for (int i=0; i&lt;crefs.length; i++) {
        HSSFSheet s = wb.getSheet(crefs[i].getSheetName());
        HSSFRow r = sheet.getRow(crefs[i].getRow());
        HSSFCell c = r.getCell(crefs[i].getCol());
        // extract the cell contents based on cell type etc.
    }
            </pre>
<img src ="http://www.blogjava.net/brock/aggbug/69688.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-09-14 16:47 <a href="http://www.blogjava.net/brock/archive/2006/09/14/69688.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>excel 处理 Poi</title><link>http://www.blogjava.net/brock/archive/2006/09/14/69674.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Thu, 14 Sep 2006 08:24:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/09/14/69674.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/69674.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/09/14/69674.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/69674.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/69674.html</trackback:ping><description><![CDATA[
		<p>excel 样式<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">create a cell object  创建一个元素(行的列)对象</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFCell cell </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> row.createCell(column);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">create a cell style object 创建一个元素的样式对象</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">HSSFCellStyle cellStyle </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> wb.createCellStyle();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setAlignment(align);  <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setVerticalAlignment(valign);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set cell border   设置元素的边框</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set foreground color 设置元素的前景色</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cellStyle.setFillForegroundColor(bgColor);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set font 设置字体</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cellStyle.setFont(font);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set size<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set the style of this cell  把样式加到元素中</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cell.setCellStyle(cellStyle); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set cell&amp;#39;s charset 设置字符 (中文问题)</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cell.setEncoding(HSSFCell.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">set cell value 改元素赋值</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">cell.setCellValue(value);</span>
		</div>从data  中得到数据 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">ArrayList datacell_list </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> DBToExcel.getSheetDataCol(tableid,k_row,</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> DBAgent());<br /><img id="Codehighlighter1_133_425_Open_Image" onclick="this.style.display='none'; Codehighlighter1_133_425_Open_Text.style.display='none'; Codehighlighter1_133_425_Closed_Image.style.display='inline'; Codehighlighter1_133_425_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_133_425_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_133_425_Closed_Text.style.display='none'; Codehighlighter1_133_425_Open_Image.style.display='inline'; Codehighlighter1_133_425_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> k_col</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;k_col</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">datacell_list.size();k_col</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span id="Codehighlighter1_133_425_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_133_425_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />　　HSSFCell cell </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> row.createCell((</span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">)k_col);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />　　cell.setEncoding(HSSFCell.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />　　SheetDataBean sdb </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(SheetDataBean)datacell_list.get(k_col);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />colnum </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> sdb.getColnum();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />String data </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> DBToExcel.getSheetCellValue(tableid,k_row,colnum,</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> DBAgent());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">cell.setCellValue(data);　</span></span></div>打印<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">1</span><span style="COLOR: #000000">.<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />HSSFCell c;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><img src="http://www.blogjava.net/images/dot.gif" />..<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />c.setEncoding(HSSFCell.ENCODING_UTF_16);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />c.setCellValue(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试测试测试测试测试测试测试测试</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><img src="http://www.blogjava.net/images/dot.gif" /><img src="http://www.blogjava.net/images/dot.gif" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">.<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />打印设置<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.poi.hssf.usermodel.HSSFPrintSetup;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />创建打印设置对象<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />HSSFPrintSetup hps </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> hs.getPrintSetup();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />设置A4纸<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />hps.setPaperSize((</span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />将页面设置为横向打印模式<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />hps.setLandscape(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />设置打印页面为水平居中<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setHorizontallyCenter(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />设置打印页面为垂直居中<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sheet.setVerticallyCenter(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span></div><img src ="http://www.blogjava.net/brock/aggbug/69674.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-09-14 16:24 <a href="http://www.blogjava.net/brock/archive/2006/09/14/69674.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用POI将数据表导入Excel </title><link>http://www.blogjava.net/brock/archive/2006/07/26/60186.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Wed, 26 Jul 2006 07:30:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/07/26/60186.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/60186.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/07/26/60186.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/60186.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/60186.html</trackback:ping><description><![CDATA[
		<ol>
				<li>
						<i>
								<font color="#339900">/*</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * QuickExcel.java</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * 作者：杨庆成</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * Created on 2004年11月22日, 下午4:05</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * 在实际应用中经常要将数据库中的表导入Excel</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * 本人在Apache的POI基础上写了一个简单的类</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * 有不当指出请指正，谢谢！</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * </font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> */</font>
						</i>
				</li>
				<li>
				</li>
				<li>
						<b>
								<font color="#0000ff">package</font>
						</b> yqc.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>; 
</li>
				<li>
				</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.sql.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.util.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.io.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.io.<b><a href="http://www.javaresearch.org/source/jdk142/java/io/ByteArrayInputStream.java.html" target="_blank"><font class="classLink"><u>ByteArrayInputStream</u></font></a></b>; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.io.<b><a href="http://www.javaresearch.org/source/jdk142/java/io/FileInputStream.java.html" target="_blank"><font class="classLink"><u>FileInputStream</u></font></a></b>; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.io.<b><a href="http://www.javaresearch.org/source/jdk142/java/io/FileOutputStream.java.html" target="_blank"><font class="classLink"><u>FileOutputStream</u></font></a></b>; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> java.io.<b><a href="http://www.javaresearch.org/source/jdk142/java/io/IOException.java.html" target="_blank"><font class="classLink"><u>IOException</u></font></a></b>; 
</li>
				<li>
				</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.hssf.usermodel.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.poifs.filesystem.POIFSFileSystem; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.hssf.record.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.hssf.model.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.hssf.usermodel.*; 
</li>
				<li>
						<b>
								<font color="#0000ff">import</font>
						</b> org.apache.<font style="BACKGROUND-COLOR: #ffff00"><b>poi</b></font>.hssf.util.*;<b><font color="#0000ff">import</font></b> yqc.sql.*; 
</li>
				<li>
				</li>
				<li>
						<i>
								<font color="#339900">/**</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> *</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> * @author  Administrator</font>
						</i>
				</li>
				<li>
						<i>
								<font color="#339900"> */</font>
						</i>
				</li>
				<li>
						<b>
								<font color="#0000ff">public</font>
						</b> <b><font color="#0000ff">class</font></b> QuickExcel { 
</li>
				<li>     
</li>
				<li>    <i><font color="#339900">/** Creates a new instance of QuickExcel */</font></i></li>
				<li>    <b><font color="#0000ff">private</font></b> QuickExcel(<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> file){ 
</li>
				<li>        _file=file; 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">private</font></b> <b><font color="#0000ff">void</font></b> open()<b><font color="#0000ff">throws</font></b> <b><a href="http://www.javaresearch.org/source/jdk142/java/io/IOException.java.html" target="_blank"><font class="classLink"><u>IOException</u></font></a></b>{ 
</li>
				<li>        <b><a href="http://www.javaresearch.org/source/jdk142/java/io/InputStream.java.html" target="_blank"><font class="classLink"><u>InputStream</u></font></a></b> stream = <b><font color="#0000ff">null</font></b>; 
</li>
				<li>        Record[] records = <b><font color="#0000ff">null</font></b>; 
</li>
				<li>        POIFSFileSystem fs = 
</li>
				<li>            <b><font color="#0000ff">new</font></b> POIFSFileSystem(<b><font color="#0000ff">new</font></b> <b><a href="http://www.javaresearch.org/source/jdk142/java/io/FileInputStream.java.html" target="_blank"><font class="classLink"><u>FileInputStream</u></font></a></b>(_file)); 
</li>
				<li>        _wb = <b><font color="#0000ff">new</font></b> HSSFWorkbook(fs); 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">private</font></b> <b><font color="#0000ff">void</font></b> create(){ 
</li>
				<li>        _wb=<b><font color="#0000ff">new</font></b> HSSFWorkbook(); 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">public</font></b> <b><font color="#0000ff">static</font></b> QuickExcel newInstance (<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> file){ 
</li>
				<li>        QuickExcel qe=<b><font color="#0000ff">new</font></b> QuickExcel(file); 
</li>
				<li>        qe.create(); 
</li>
				<li>        <b><font color="#0000ff">return</font></b> qe; 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">public</font></b> <b><font color="#0000ff">static</font></b> QuickExcel openInstance(<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> file) <b><font color="#0000ff">throws</font></b> <b><a href="http://www.javaresearch.org/source/jdk142/java/io/IOException.java.html" target="_blank"><font class="classLink"><u>IOException</u></font></a></b> { 
</li>
				<li>        QuickExcel qe=<b><font color="#0000ff">new</font></b> QuickExcel(file); 
</li>
				<li>        qe.open(); 
</li>
				<li>        <b><font color="#0000ff">return</font></b> qe; 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">public</font></b> <b><font color="#0000ff">void</font></b> close(){ 
</li>
				<li>        <b><font color="#0000ff">try</font></b>{ 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/io/FileOutputStream.java.html" target="_blank"><font class="classLink"><u>FileOutputStream</u></font></a></b> fileOut = <b><font color="#0000ff">new</font></b> <b><a href="http://www.javaresearch.org/source/jdk142/java/io/FileOutputStream.java.html" target="_blank"><font class="classLink"><u>FileOutputStream</u></font></a></b>(_file); 
</li>
				<li>            _wb.write(fileOut);<i><font color="#339900">//把Workbook对象输出到文件workbook.xls中</font></i></li>
				<li>            fileOut.close(); 
</li>
				<li>        } 
</li>
				<li>        <b><font color="#0000ff">catch</font></b> (<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/Exception.java.html" target="_blank"><font class="classLink"><u>Exception</u></font></a></b> ex){ 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target="_blank"><font class="classLink"><u>System</u></font></a></b>.out.println(ex.getMessage()); 
</li>
				<li>        } 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">private</font></b> <b><font color="#0000ff">void</font></b> removeSheet(<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> sheetName){ 
</li>
				<li>        <b><font color="#0000ff">int</font></b> i=_wb.getSheetIndex(<font color="#ff33ff">"sheetName"</font>); 
</li>
				<li>        <b><font color="#0000ff">if</font></b> (i&gt;=0) _wb.removeSheetAt(i); 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">public</font></b> <b><font color="#0000ff">int</font></b> fillSheet (<b><a href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSet.java.html" target="_blank"><font class="classLink"><u>ResultSet</u></font></a></b> rs,<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> sheetName)<b><font color="#0000ff">throws</font></b> <b><a href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target="_blank"><font class="classLink"><u>SQLException</u></font></a></b> { 
</li>
				<li>        HSSFSheet st= _wb.createSheet(sheetName); 
</li>
				<li>        <b><a href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSetMetaData.java.html" target="_blank"><font class="classLink"><u>ResultSetMetaData</u></font></a></b> rsmd= rs.getMetaData(); 
</li>
				<li>        <b><font color="#0000ff">int</font></b> index=0; 
</li>
				<li>        <b><font color="#0000ff">int</font></b> result=0; 
</li>
				<li>        HSSFRow row=st.createRow(index++); 
</li>
				<li>        <b><font color="#0000ff">for</font></b>(<b><font color="#0000ff">int</font></b> i=1;i&lt;=rsmd.getColumnCount();++i){ 
</li>
				<li>            HSSFCell cell=row.createCell((<b><font color="#0000ff">short</font></b>)(i-1)); 
</li>
				<li>            cell.setCellValue(rsmd.getColumnName(i)); 
</li>
				<li>        } 
</li>
				<li>        <b><font color="#0000ff">while</font></b>(rs.next()) { 
</li>
				<li>            result++; 
</li>
				<li>            row=st.createRow(index++); 
</li>
				<li>            <b><font color="#0000ff">for</font></b>(<b><font color="#0000ff">int</font></b> i=1;i&lt;=rsmd.getColumnCount();++i){ 
</li>
				<li>                HSSFCell cell=row.createCell((<b><font color="#0000ff">short</font></b>)(i-1)); 
</li>
				<li>                cell.setEncoding(cell.ENCODING_UTF_16); 
</li>
				<li>                cell.setCellValue(rs.getString(i)); 
</li>
				<li>            } 
</li>
				<li>        } 
</li>
				<li>        <b><font color="#0000ff">return</font></b> result; 
</li>
				<li>} 
</li>
				<li>     
</li>
				<li>    <b><font color="#0000ff">public</font></b> <b><font color="#0000ff">static</font></b> <b><font color="#0000ff">void</font></b> main(<b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b>[] args){ 
</li>
				<li>        <b><font color="#0000ff">try</font></b>{ 
</li>
				<li>            QuickConnection qc=<b><font color="#0000ff">new</font></b> MssqlConnection(<font color="#ff33ff">"jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=ls"</font>); 
</li>
				<li>            QuickExcel qe=QuickExcel.newInstance(<font color="#ff33ff">"a.xls"</font>); 
</li>
				<li>            qc.connect(); 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> sql=<font color="#ff33ff">"select * from ls.dbo.radio1_emcee"</font>; 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSet.java.html" target="_blank"><font class="classLink"><u>ResultSet</u></font></a></b> rs=qc.getStatement().executeQuery(sql); 
</li>
				<li>            qe.fillSheet(rs,<font color="#ff33ff">"MT"</font>); 
</li>
				<li>            qe.close(); 
</li>
				<li>            qe=QuickExcel.openInstance(<font color="#ff33ff">"a.xls"</font>); 
</li>
				<li>            qe.fillSheet(rs,<font color="#ff33ff">"MO"</font>); 
</li>
				<li>            qe.close(); 
</li>
				<li>            qc.close(); 
</li>
				<li>        } 
</li>
				<li>        <b><font color="#0000ff">catch</font></b> (<b><a href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target="_blank"><font class="classLink"><u>SQLException</u></font></a></b> ex){ 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target="_blank"><font class="classLink"><u>System</u></font></a></b>.out.println(ex.getMessage()); 
</li>
				<li>        } 
</li>
				<li>        <b><font color="#0000ff">catch</font></b> (<b><a href="http://www.javaresearch.org/source/jdk142/java/io/IOException.java.html" target="_blank"><font class="classLink"><u>IOException</u></font></a></b> ex){ 
</li>
				<li>            <b><a href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target="_blank"><font class="classLink"><u>System</u></font></a></b>.out.println(ex.getMessage()); 
</li>
				<li>        } 
</li>
				<li>    } 
</li>
				<li>     
</li>
				<li>    HSSFWorkbook _wb; 
</li>
				<li>    <b><a href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target="_blank"><font class="classLink"><u>String</u></font></a></b> _file=<font color="#ff33ff">"new.xls"</font>; 
</li>
				<li>} </li>
		</ol>
<img src ="http://www.blogjava.net/brock/aggbug/60186.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-07-26 15:30 <a href="http://www.blogjava.net/brock/archive/2006/07/26/60186.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>纯java的Excel解决方案</title><link>http://www.blogjava.net/brock/archive/2006/07/26/60185.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Wed, 26 Jul 2006 07:29:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/07/26/60185.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/60185.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/07/26/60185.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/60185.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/60185.html</trackback:ping><description><![CDATA[
		<div class="center">
				<h4>摘要:</h4>Apache的Jakata项目的POI子项目，目标是处理ole2对象。目前比较成熟的是HSSF接口，处理MS Excel（97-2002）对象 </div>
		<div class="right">
				<div class="help">
						<h4>工具箱</h4>
						<a href="http://www.matrix.org.cn/favorite.shtml?type=article&amp;title=Jakata%2520Poi%2520HSSF%25EF%25BC%259A%25E7%25BA%25AFjava%25E7%259A%2584Excel%25E8%25A7%25A3%25E5%2586%25B3%25E6%2596%25B9%25E6%25A1%2588&amp;url=resource%2Farticle%2F1%2F1528.html">本站收藏</a>
						<br />
						<a onclick="javascript:location.href='http://del.icio.us/post?&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)" href="http://www.matrix.org.cn/resource/article/1/1528.html#">美味书签</a>
						<br />
						<a href="http://www.matrix.org.cn/resource/article/1/1528.html#avote">投票评分</a>
						<br />
						<a href="http://www.matrix.org.cn/resource/article/1/1528.html#areview">发表评论</a>
						<br />
						<a title="点击后直接粘贴即可" onclick="copyLink();" href="http://www.matrix.org.cn/resource/article/1/1528.html#">复制链接</a>
						<br />
				</div>
		</div>
		<!-- end of summary line -->
		<div class="overflow" id="text">微软在桌面系统上的成功，令我们不得不大量使用它的办公产品，如：Word，Excel。时至今日，它的源代码仍然不公开已封锁了我们的进一步应用和开发。然而在要求更高的服务器领域，微软本身的产品移植性不好，  <br />性能不佳。在我们实际的开发中，表现层的解决方案虽然有多样，但是Ie浏览器已成为最多人使用的浏览器，因为大家都用Windows。在企业办公系统中，常常有客户这样子要求：你要把我们的报表直接用Excel打开。或者是：我们已经习惯用Excel打印。这样子如果用.net开发是没有问题的，但是有j2ee这个比.net更有前途的开放式的开发环境，难道我为了解决打印的要求去另写客户端的控件？或者在服务器端使用本地代码？第一种方案的问题是关键数据的处理有时候不能在客户端做，第2种方案的问题是牺牲了代码的可移植性和稳定性。如果让客户端只负责处理生成好的报表，那将是一种诱人的选择。<br /><br />Apache的Jakata项目的POI子项目，目标是处理ole2对象。目前比较成熟的是HSSF接口，处理MS Excel（97-2002）对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西，而是真正的Excel对象，你可以控制一些属性如sheet,cell等等。这是一个年轻的项目，所以象HDF这样直接支持Word对象的好东西仍然在设计中。其它支持word格式的纯java方案还有itext，不过也是仍在奋斗中。但是HSSF已经成熟到能够和足够我们使用了。另外，无锡永中Office的实现方案也是纯java的解决方案，不过那也是完全商业的产品，并不是公开代码项目。其实，从开发历史的角度讲，在80年代中期starOffice的原作者在德国成立了StarOffice suite公司，然后到1999年夏天starOffice被sun收购，再到2000年6月starOffice5.2的发布；并且从starOffice6.0开始，starOffice建立在OpenOffice的api的基础上，这个公开代码的office项目已经进行了很长的时间。虽然那是由C++写的，但是POI的代码部分也是由openOffice改过来的。所以，应该对POI充满足够的信心。国内已经有部分公司在他们的办公自动化等Web项目中使用poi了，如日恒的ioffice，海泰的HTOffice等。<br /><br />java当初把核心处理设成Unicode，带来的好处是另代码适应了多语言环境。然而由于老外的英语只有26个字母，有些情况下，一些程序员用8位的byte处理，一不小心就去掉了CJK的高位。或者是由于习惯在程序中采用硬编码，还有多种原因，使得许多java应用在CJK的处理上很烦恼。还好在POI HSSF中考虑到这个问题，可以设置encoding为双字节。<br /><br />POI可以到www.apache.org下载到。编译好的jar主要有这样4个：poi包,poi Browser包,poi hdf包,poi hssf例程包。实际运行时，需要有poi包就可以了。如果用Jakarta ant编译和运行，下载apache Jakarta POI的release中的src包，它里面已经为你生成好了build文件了。只要运行ant就可以了(ant 的安装和使用在此不说了)。如果是用Jbuilder 运行，请在新建的项目中加入poi包。以Jbuilder6为例，选择Tools菜单项的config libraries...选项，新建一个lib。在弹出的菜单中选择poi包，如这个jakarta-poi-1.5.1-final-20020820.jar，把poi添加到jbuilder中。然后，右键点击你的项目，在project的properties菜单中path的required Libraries中，点add，添加刚才加入到jbuilder中的poi到你现在的项目中。如果你仅仅是为了熟悉POI hssf的使用，可以直接看POI的samples包中的源代码，并且运行它。hssf的各种对象都有例程的介绍。hssf提供的例程在org.apache.poi.hssf.usermodel.examples包中，共有14个，生成的目标xls都是workbook.xls。如果你想看更多的例程，可以参考hssf的Junit test cases，在poi的包的源代码中有。hssf都有测试代码。<br /><br />这里只对部分例程的实现做介绍。<br /><br />HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中,主要部分包括Excell对象，样式和格式，还有辅助操作。有以下几种对象：<br /><br />HSSFWorkbook excell的文档对象<br /><br />HSSFSheet excell的表单<br /><br />HSSFRow excell的行 <br /><br />HSSFCell excell的格子单元<br /><br />HSSFFont excell字体<br /><br />HSSFName 名称<br /><br />HSSFDataFormat 日期格式<br /><br />在poi1.7中才有以下2项：<br /><br />HSSFHeader sheet头<br /><br />HSSFFooter sheet尾<br /><br />和这个样式<br /><br />HSSFCellStyle cell样式<br /><br />辅助操作包括<br /><br />HSSFDateUtil 日期<br /><br />HSSFPrintSetup 打印 <br /><br />HSSFErrorConstants 错误信息表<br /><br />仔细看org.apache.poi.hssf包的结构，不难发现HSSF的内部实现遵循的是MVC模型。<br /><br />这里我用Rose把org.apache.poi.hssf.usermodel包中的对象反向导入并根据相互关系作了整理，详见下面两图：<br /><br />  <b><a class="wiki_link" title="wiki link" href="http://wiki.matrix.org.cn/Wiki.jsp?page=IMG">IMG</a></b> ../upload/article/a2005512101549.jpg[/IMG]<br /><br />图1 基本对象 <br /><br />从中不难可以发现每一个基本对象都关联了一个Record对象。Record对象是一个参考Office格式的相关记录。<br /><br />   <b><a class="wiki_link" title="wiki link" href="http://wiki.matrix.org.cn/Wiki.jsp?page=IMG">IMG</a></b> ../upload/article/2005512101631.jpg[/IMG]<br /><br /><br />图2 HSSFWorkbook<br /><br />HSSFWorkbook即是一个Excell对象。这幅类图体现的是HSSFWorkbook和基本对象的相互关系。可见，许多对象中也建立了Workbook的引用。还需要注意的是在HSSFWorkbook和HSSFSheet中建立了log机制POILogger，而且POILogger也是使用apache Log4J实现的。<br /><br />先看poi的examples包中提供的最简单的例子，建立一个空xls文件。<br /><br />import org.apache.poi.hssf.usermodel.HSSFWorkbook;<br /><br />import java.io.FileOutputStream;<br /><br />import java.io.IOException;<br /><br />public class NewWorkbook<br /><br />{<br /><br />public static void main(String[] args)<br /><br />throws IOException<br /><br />{<br /><br />HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象<br /><br />FileOutputStream fileOut = new FileOutputStream("workbook.xls");<br /><br />wb.write(fileOut);//把Workbook对象输出到文件workbook.xls中<br /><br />fileOut.close();<br /><br />}<br /><br />}<br /><br />通过这个例子，我们建立的是一个空白的xls文件（不是空文件）。在此基础上，我们可以进一步看其它的例子。<br /><br /><br />import org.apache.poi.hssf.usermodel.*;<br /><br />import java.io.FileOutputStream;<br /><br />import java.io.IOException;<br /><br />public class CreateCells<br /><br />{<br /><br />public static void main(String[] args)<br /><br />throws IOException<br /><br />{<br /><br />HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象<br /><br />HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象<br /><br /><br />// Create a row and put some cells in it. Rows are 0 based.<br /><br />HSSFRow row = sheet.createRow((short)0);//建立新行<br /><br />// Create a cell and put a value in it.<br /><br />HSSFCell cell = row.createCell((short)0);//建立新cell<br /><br />cell.setCellValue(1);//设置cell的整数类型的值<br /><br /><br />// Or do it on one line.<br /><br />row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值<br /><br />row.createCell((short)2).setCellValue("test");//设置cell字符类型的值<br /><br />row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值 <br /><br />HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式<br /><br />cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式<br /><br />HSSFCell dCell =row.createCell((short)4);<br /><br />dCell.setCellValue(new Date());//设置cell为日期类型的值<br /><br />dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式<br /><br />HSSFCell csCell =row.createCell((short)5);<br /><br />csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断<br /><br />csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串<br /><br />row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell<br /><br /><br />// Write the output to a file<br /><br />FileOutputStream fileOut = new FileOutputStream("workbook.xls");<br /><br />wb.write(fileOut);<br /><br />fileOut.close();<br /><br />}<br /><br />}<br /><br />我稍微修改了原来的examples包中的CreateCells类写了上面的功能测试类。通过这个例子，我们可以清楚的看到xls文件从大到小包括了HSSFWorkbook HSSFSheet HSSFRow HSSFCell这样几个对象。我们可以在cell中设置各种类型的值。尤其要注意的是如果你想正确的显示非欧美的字符时，尤其象中日韩这样的语言，必须设置编码为16位的即是HSSFCell.ENCODING_UTF_16，才能保证字符的高8位不被截断而引起编码失真形成乱码。<br /><br />其他测试可以通过参考examples包中的测试例子掌握poi的详细用法，包括字体的设置，cell大小和低纹的设置等。需要注意的是POI是一个仍然在完善中的公开代码的项目，所以有些功能正在不断的扩充。如HSSFSheet的getFooter() getHeader()和setFooter(HSSFFooter hsf) setHeader(HSSFHeader hsh)是在POI1.7中才有的，而POI1.5中就没有。运行测试熟悉代码或者使用它做项目时请注意POI的版本。<br /><br />另外需要注意的是HSSF也有它的对xls基于事件的解析。可以参考例程中的EventExample.java。它通过实现HSSFListener完成从普通流认知Xls中包含的内容，在apache Cocoon中的org.apache.cocoon.serialization.HSSFSerializer中用到了这个解析。因为Cocoon2是基于事件的，所以POI为了提供快速的解析也提供了相应的事件。当然我们自己也可以实现这个事件接口。<br /><br />因为POI还不是一个足够成熟的项目，所以有必要做进一步的开发和测试。但是它已经为我们用纯java操作ole2对象提供了可能，而且克服了ole对象调用的缺陷，提供了服务器端的Excel解决方案。<br /></div>
		<p> </p>
<img src ="http://www.blogjava.net/brock/aggbug/60185.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-07-26 15:29 <a href="http://www.blogjava.net/brock/archive/2006/07/26/60185.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Excel的java处理方式</title><link>http://www.blogjava.net/brock/archive/2006/07/13/57961.html</link><dc:creator>brock</dc:creator><author>brock</author><pubDate>Thu, 13 Jul 2006 05:48:00 GMT</pubDate><guid>http://www.blogjava.net/brock/archive/2006/07/13/57961.html</guid><wfw:comment>http://www.blogjava.net/brock/comments/57961.html</wfw:comment><comments>http://www.blogjava.net/brock/archive/2006/07/13/57961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/brock/comments/commentRss/57961.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/brock/services/trackbacks/57961.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">Poi即poor object interface之意，是poi项目组对微软封闭的office文件格式（接口）的称谓！ <br />其中提供了对word和excel的java接口，用法非常简单，并且是完全免费的，对中文的支持也相当不错，下面是处理excel一个简单的实例： </font>
		</p>
		<p>
				<br />
				<font size="2">package test; </font>
		</p>
		<p>
				<font size="2">import java.io.*; <br />import org.apache.poi.hssf.usermodel.*; <br />import org.apache.poi.poifs.filesystem.POIFSFileSystem; </font>
		</p>
		<p>
				<font size="2">public class Test { <br />public static void main(String[] args) { <br />try { <br />/**************创建一个xls文档*************/ <br />HSSFWorkbook wb = new HSSFWorkbook(); <br />FileOutputStream fileOut = new FileOutputStream("workbook.xls"); <br />HSSFSheet sheet = wb.createSheet("new sheet"); <br />HSSFRow row = sheet.createRow((short)0); <br />HSSFCell cell = row.createCell((short)0); <br />row.createCell((short)1); <br />row.createCell((short)2); <br />row.createCell((short)3); <br />row.createCell((short)4); <br />row.createCell((short)5); <br />cell.setCellValue("test_write!"); <br />wb.write(fileOut); <br />fileOut.close(); <br />/*************读取并修改xls文档***************/ <br />POIFSFileSystem pfs=new POIFSFileSystem(new FileInputStream("workbook.xls")); <br />HSSFWorkbook hwb=new HSSFWorkbook(pfs); <br />HSSFSheet hs=hwb.getSheetAt(0); <br />HSSFRow hr=hs.getRow(0); <br />HSSFCellStyle style=hwb.createCellStyle(); <br />style.setAlignment(style.ALIGN_CENTER); <br />HSSFFont hf=hwb.createFont(); <br />hf.setFontName("楷体_GB2312"); <br />style.setFont(hf); <br />HSSFCell cl=hr.getCell((short)2); <br />cl.setEncoding(cl.ENCODING_UTF_16); <br />cl.setCellStyle(style); <br />cl.setCellValue("test_modify! 测试\u2014\u2014编辑！"); <br />FileOutputStream fos=new FileOutputStream("workbook.xls"); <br />hwb.write(fos); <br />fos.flush(); <br />fos.close(); <br />} <br />catch (Exception ex) { <br />ex.printStackTrace(); <br />} <br />} <br />} <br /></font>
		</p>
<img src ="http://www.blogjava.net/brock/aggbug/57961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/brock/" target="_blank">brock</a> 2006-07-13 13:48 <a href="http://www.blogjava.net/brock/archive/2006/07/13/57961.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>