Java平台下著名的POI组件下的excel导出功能代码。
[代码] [Java]代码
001 |
package
com.tkqd.util.poi;
|
003 |
import
java.io.FileNotFoundException;
|
004 |
import
java.io.FileOutputStream;
|
005 |
import
java.io.IOException;
|
007 |
import
org.apache.log4j.Logger;
|
008 |
import
org.apache.poi.hssf.usermodel.HSSFCell;
|
009 |
import
org.apache.poi.hssf.usermodel.HSSFRow;
|
010 |
import
org.apache.poi.hssf.usermodel.HSSFSheet;
|
011 |
import
org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
012 |
import
org.apache.poi.hssf.util.HSSFCellUtil;
|
013 |
import
org.apache.poi.ss.usermodel.CellStyle;
|
014 |
import
org.apache.poi.ss.usermodel.Font;
|
015 |
import
org.apache.poi.ss.util.CellRangeAddress;
|
023 |
public
class ExcelUtil { |
024 |
private
static final
Logger log=Logger.getLogger(ExcelUtil.
class );
|
026 |
* 功能:将HSSFWorkbook写入Excel文件
|
027 |
* @param wb HSSFWorkbook
|
028 |
* @param absPath 写入文件的相对路径
|
031 |
public
static void
writeWorkbook(HSSFWorkbook wb,String fileName){
|
032 |
FileOutputStream fos= null ;
|
034 |
fos= new
FileOutputStream(fileName); |
036 |
}
catch (FileNotFoundException e) {
|
037 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
038 |
}
catch (IOException e) {
|
039 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
045 |
}
catch (IOException e) {
|
046 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
052 |
* @param wb HSSFWorkbook
|
053 |
* @param sheetName String
|
056 |
public
static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){
|
057 |
HSSFSheet sheet=wb.createSheet(sheetName);
|
058 |
sheet.setDefaultColumnWidth( 12 );
|
059 |
sheet.setGridsPrinted( false );
|
060 |
sheet.setDisplayGridlines( false );
|
065 |
* @param sheet HSSFSheet
|
070 |
public
static HSSFRow createRow(HSSFSheet sheet, int
rowNum, int
height){
|
071 |
HSSFRow row=sheet.createRow(rowNum);
|
072 |
row.setHeight(( short )height);
|
077 |
* @param wb HSSFWorkbook
|
078 |
* @param backgroundColor 背景色
|
079 |
* @param foregroundColor 前置色
|
083 |
public
static CellStyle createCellStyle(HSSFWorkbook wb, short
backgroundColor, short
foregroundColor, short
halign,Font font){ |
084 |
CellStyle cs=wb.createCellStyle();
|
085 |
cs.setAlignment(halign);
|
086 |
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
087 |
cs.setFillBackgroundColor(backgroundColor);
|
088 |
cs.setFillForegroundColor(foregroundColor);
|
089 |
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
094 |
* 功能:创建带边框的CellStyle样式
|
095 |
* @param wb HSSFWorkbook
|
096 |
* @param backgroundColor 背景色
|
097 |
* @param foregroundColor 前置色
|
101 |
public
static CellStyle createBorderCellStyle(HSSFWorkbook wb, short
backgroundColor, short
foregroundColor, short
halign,Font font){ |
102 |
CellStyle cs=wb.createCellStyle();
|
103 |
cs.setAlignment(halign);
|
104 |
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
105 |
cs.setFillBackgroundColor(backgroundColor);
|
106 |
cs.setFillForegroundColor(foregroundColor);
|
107 |
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
109 |
cs.setBorderLeft(CellStyle.BORDER_DASHED);
|
110 |
cs.setBorderRight(CellStyle.BORDER_DASHED);
|
111 |
cs.setBorderTop(CellStyle.BORDER_DASHED);
|
112 |
cs.setBorderBottom(CellStyle.BORDER_DASHED);
|
119 |
* @param style HSSFStyle
|
122 |
public
static HSSFCell createCell(HSSFRow row, int
cellNum,CellStyle style){ |
123 |
HSSFCell cell=row.createCell(cellNum);
|
124 |
cell.setCellStyle(style);
|
129 |
* @param sheet HSSFSheet
|
130 |
* @param firstRow int
|
132 |
* @param firstColumn int
|
133 |
* @param lastColumn int
|
136 |
public
static int
mergeCell(HSSFSheet sheet,
int
firstRow,
int lastRow, int
firstColumn, int
lastColumn){ |
137 |
return
sheet.addMergedRegion( new
CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn));
|
141 |
* @param wb HSSFWorkbook
|
142 |
* @param boldweight short
|
146 |
public
static Font createFont(HSSFWorkbook wb, short
boldweight, short
color, short
size){
|
147 |
Font font=wb.createFont();
|
148 |
font.setBoldweight(boldweight);
|
149 |
font.setColor(color);
|
150 |
font.setFontHeightInPoints(size);
|
155 |
* @param sheet HSSFSheet
|
156 |
* @param ca CellRangAddress
|
157 |
* @param style CellStyle
|
159 |
public
static void
setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) {
|
160 |
for
( int
i = ca.getFirstRow(); i <= ca.getLastRow(); i++) {
|
161 |
HSSFRow row = HSSFCellUtil.getRow(i, sheet);
|
162 |
for
( int
j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) {
|
163 |
HSSFCell cell = HSSFCellUtil.getCell(row, j);
|
164 |
cell.setCellStyle(style);
|
[文件] ExcelUtil.java ~ 5KB 下载(932)
001 |
package
com.tkqd.util.poi;
|
003 |
import
java.io.FileNotFoundException;
|
004 |
import
java.io.FileOutputStream;
|
005 |
import
java.io.IOException;
|
007 |
import
org.apache.log4j.Logger;
|
008 |
import
org.apache.poi.hssf.usermodel.HSSFCell;
|
009 |
import
org.apache.poi.hssf.usermodel.HSSFRow;
|
010 |
import
org.apache.poi.hssf.usermodel.HSSFSheet;
|
011 |
import
org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
012 |
import
org.apache.poi.hssf.util.HSSFCellUtil;
|
013 |
import
org.apache.poi.ss.usermodel.CellStyle;
|
014 |
import
org.apache.poi.ss.usermodel.Font;
|
015 |
import
org.apache.poi.ss.util.CellRangeAddress;
|
023 |
public
class ExcelUtil { |
024 |
private
static final
Logger log=Logger.getLogger(ExcelUtil.
class );
|
026 |
* 功能:将HSSFWorkbook写入Excel文件
|
027 |
* @param wb HSSFWorkbook
|
028 |
* @param absPath 写入文件的相对路径
|
031 |
public
static void
writeWorkbook(HSSFWorkbook wb,String fileName){
|
032 |
FileOutputStream fos= null ;
|
034 |
fos= new
FileOutputStream(fileName); |
036 |
}
catch (FileNotFoundException e) {
|
037 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
038 |
}
catch (IOException e) {
|
039 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
045 |
}
catch (IOException e) {
|
046 |
log.error( new
StringBuffer( "[" ).append(e.getMessage()).append( "]" ).append(e.getCause()));
|
052 |
* @param wb HSSFWorkbook
|
053 |
* @param sheetName String
|
056 |
public
static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){
|
057 |
HSSFSheet sheet=wb.createSheet(sheetName);
|
058 |
sheet.setDefaultColumnWidth( 12 );
|
059 |
sheet.setGridsPrinted( false );
|
060 |
sheet.setDisplayGridlines( false );
|
065 |
* @param sheet HSSFSheet
|