java poi excel

来源:互联网 发布:少女前线铁血数据 编辑:程序博客网 时间:2024/05/23 13:47
package com.tkqd.util.poi;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;


/**
 * 描述:Excel写操作帮助类
 * @authorALEX
 * @since2010-11-24
 * @version1.0v
 */
public class ExcelUtil {
private static final Logger log=Logger.getLogger(ExcelUtil.class);
/**
* 功能:将HSSFWorkbook写入Excel文件
* @param wbHSSFWorkbook
* @param absPath写入文件的相对路径
* @param wbName文件名
*/
public static void writeWorkbook(HSSFWorkbook wb,String fileName){
FileOutputStream fos=null;
try {
fos=new FileOutputStream(fileName);
wb.write(fos);
} catch (FileNotFoundException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
} catch (IOException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
} finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
}
}
}
/**
* 功能:创建HSSFSheet工作簿
* @param wbHSSFWorkbook
* @param sheetNameString
* @returnHSSFSheet
*/
public static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){
HSSFSheet sheet=wb.createSheet(sheetName);
sheet.setDefaultColumnWidth(12);
sheet.setGridsPrinted(false);
sheet.setDisplayGridlines(false);
return sheet;
}
/**
* 功能:创建HSSFRow
* @param sheetHSSFSheet
* @param rowNumint
* @param heightint
* @returnHSSFRow
*/
public static HSSFRow createRow(HSSFSheet sheet,int rowNum,int height){
HSSFRow row=sheet.createRow(rowNum);
row.setHeight((short)height);
return row;
}
/**
* 功能:创建CellStyle样式
* @param wbHSSFWorkbook
* @param backgroundColor背景色
* @param foregroundColor前置色
* @paramfont字体
* @returnCellStyle
*/
public static CellStyle createCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
return cs;
}
/**
* 功能:创建带边框的CellStyle样式
* @param wbHSSFWorkbook
* @param backgroundColor背景色
* @param foregroundColor前置色
* @paramfont字体
* @returnCellStyle
*/
public static CellStyle createBorderCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
cs.setBorderLeft(CellStyle.BORDER_DASHED);
cs.setBorderRight(CellStyle.BORDER_DASHED);
cs.setBorderTop(CellStyle.BORDER_DASHED);
cs.setBorderBottom(CellStyle.BORDER_DASHED);  
return cs;
}
/**
* 功能:创建CELL
* @param rowHSSFRow
* @param cellNumint
* @param styleHSSFStyle
* @returnHSSFCell
*/
public static HSSFCell createCell(HSSFRow row,int cellNum,CellStyle style){
HSSFCell cell=row.createCell(cellNum);
cell.setCellStyle(style);
return cell;
}
/**
* 功能:合并单元格
* @param sheetHSSFSheet
* @param firstRowint
* @param lastRowint
* @param firstColumnint
* @param lastColumnint
* @returnint合并区域号码
*/
public static int mergeCell(HSSFSheet sheet,int firstRow,int lastRow,int firstColumn,int lastColumn){
return sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn));
}
/**
* 功能:创建字体
* @param wbHSSFWorkbook
* @param boldweightshort
* @param colorshort
* @returnFont
*/
public static Font createFont(HSSFWorkbook wb,short boldweight,short color,short size){
Font font=wb.createFont();
font.setBoldweight(boldweight);
font.setColor(color);
font.setFontHeightInPoints(size);
return font;
}
/**
* 设置合并单元格的边框样式
* @paramsheetHSSFSheet
* @param caCellRangAddress
* @param styleCellStyle
*/
public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) {  
   for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) {  
       HSSFRow row = HSSFCellUtil.getRow(i, sheet);  
       for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) {  
           HSSFCell cell = HSSFCellUtil.getCell(row, j);  
           cell.setCellStyle(style);  
       }  
   }  
}  
}
[文件] ExcelUtil.java ~ 5KB    下载(136)
package com.tkqd.util.poi;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;


/**
 * 描述:Excel写操作帮助类
 * @authorALEX
 * @since2010-11-24
 * @version1.0v
 */
public class ExcelUtil {
private static final Logger log=Logger.getLogger(ExcelUtil.class);
/**
* 功能:将HSSFWorkbook写入Excel文件
* @param wbHSSFWorkbook
* @param absPath写入文件的相对路径
* @param wbName文件名
*/
public static void writeWorkbook(HSSFWorkbook wb,String fileName){
FileOutputStream fos=null;
try {
fos=new FileOutputStream(fileName);
wb.write(fos);
} catch (FileNotFoundException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
} catch (IOException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
} finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
}
}
}
/**
* 功能:创建HSSFSheet工作簿
* @param wbHSSFWorkbook
* @param sheetNameString
* @returnHSSFSheet
*/
public static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){
HSSFSheet sheet=wb.createSheet(sheetName);
sheet.setDefaultColumnWidth(12);
sheet.setGridsPrinted(false);
sheet.setDisplayGridlines(false);
return sheet;
}
/**
* 功能:创建HSSFRow
* @param sheetHSSFSheet
* @param rowNumint
* @param heightint
* @returnHSSFRow
*/
public static HSSFRow createRow(HSSFSheet sheet,int rowNum,int height){
HSSFRow row=sheet.createRow(rowNum);
row.setHeight((short)height);
return row;
}
/**
* 功能:创建CellStyle样式
* @param wbHSSFWorkbook
* @param backgroundColor背景色
* @param foregroundColor前置色
* @paramfont字体
* @returnCellStyle
*/
public static CellStyle createCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
return cs;
}
/**
* 功能:创建带边框的CellStyle样式
* @param wbHSSFWorkbook
* @param backgroundColor背景色
* @param foregroundColor前置色
* @paramfont字体
* @returnCellStyle
*/
public static CellStyle createBorderCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
cs.setBorderLeft(CellStyle.BORDER_DASHED);
cs.setBorderRight(CellStyle.BORDER_DASHED);
cs.setBorderTop(CellStyle.BORDER_DASHED);
cs.setBorderBottom(CellStyle.BORDER_DASHED);  
return cs;
}
/**
* 功能:创建CELL
* @param rowHSSFRow
* @param cellNumint
* @param styleHSSFStyle
* @returnHSSFCell
*/
public static HSSFCell createCell(HSSFRow row,int cellNum,CellStyle style){
HSSFCell cell=row.createCell(cellNum);
cell.setCellStyle(style);
return cell;
}
/**
* 功能:合并单元格
* @param sheetHSSFSheet
* @param firstRowint
* @param lastRowint
* @param firstColumnint
* @param lastColumnint
* @returnint合并区域号码
*/
public static int mergeCell(HSSFSheet sheet,int firstRow,int lastRow,int firstColumn,int lastColumn){
return sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn));
}
/**
* 功能:创建字体
* @param wbHSSFWorkbook
* @param boldweightshort
* @param colorshort
* @returnFont
*/
public static Font createFont(HSSFWorkbook wb,short boldweight,short color,short size){
Font font=wb.createFont();
font.setBoldweight(boldweight);
font.setColor(color);
font.setFontHeightInPoints(size);
return font;
}
/**
* 设置合并单元格的边框样式
* @paramsheetHSSFSheet
* @param caCellRangAddress
* @param styleCellStyle
*/
public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) {  
   for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) {  
       HSSFRow row = HSSFCellUtil.getRow(i, sheet);  
       for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) {  
           HSSFCell cell = HSSFCellUtil.getCell(row, j);  
           cell.setCellStyle(style);  
       }  
   }  
}  
}
原创粉丝点击