POIExcelUtil


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletResponse;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


import flexjson.JSONSerializer;










/**
 * todo  Excel工具类
 *@author 
 *@date 2010-10-26 上午11:04:18
 */
public class POIExcelUtil {

private  final static String  FILE_TYPE_XLS="xls"; 

private HSSFWorkbook workbook;
private HSSFSheet currentSheet ;

public static void main(String [] args) throws Exception

{

POIExcelUtil poiUtil = new POIExcelUtil();

// 模板
poiUtil.loadExcel("F:\\workspace\\jsti\\dhcc-home\\documentlib\\jdjg\\template\\excel\\工程投资报表-填报模板.xls");
int beginRowNum = poiUtil.getFirstNullRowIndex();
//System.out.println(poiUtil.currentSheet.getRow(2).getCell((short) 5).getStringCellValue());


// 报表差异计算差异

// 从报表配置中心获取Excel的业务主键配置信息。
String[] excelkeyArr = "0".split(",");


//数据库数据集
poiUtil.loadExcel("F:\\workspace\\jsti\\dhcc-home\\documentlib\\jdjg\\template\\excel\\工程投资报表-数据.xls");
List dbList = poiUtil.getExcelDataList(beginRowNum);

//Excel数据集
poiUtil.loadExcel("F:\\workspace\\jsti\\dhcc-home\\documentlib\\jdjg\\template\\excel\\工程投资报表-数据.xls");
List excelList = poiUtil.getExcelDataList(beginRowNum);

Object[] obj = poiUtil.getDifferen4TowDataSet(dbList, excelList, excelkeyArr);

JSONSerializer serializer = new JSONSerializer();
String str = serializer.deepSerialize(obj[0]);
}





/*
 * 
 *  测试导出Excel的main 方法。
    public static void main(String [] args) throws Exception

{
POIExcelUtil poiUtil = new POIExcelUtil();

poiUtil.createExcel("workbook","xls");
poiUtil.getCurrentSheet().addMergedRegion(new Region((short)0,(short)0,(short)0,(short)7));
poiUtil.setCellValue(0, 0, "只要信息\n点对点点对点");
// 初始化表头样式(设置背景颜色,边框样式,字体样式)
HSSFCellStyle cellStyle = poiUtil.getDefaultBorderStyle();
poiUtil.setFillForegroundColor(146, 205, 220,cellStyle);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 粗体
HSSFFont font  = poiUtil.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);

// 输出表头信息
poiUtil.setCellValue(4, 0, "序号", cellStyle);
poiUtil.setCellValue(4, 1, "标准名称", cellStyle);
poiUtil.setCellValue(4, 2, "标准编号", cellStyle);
poiUtil.setCellValue(4, 3, "标准归口", cellStyle);
poiUtil.setCellValue(4, 4, "适用范围", cellStyle);
poiUtil.setCellValue(4, 5, "实施时间", cellStyle);
poiUtil.setCellValue(4, 6, "申请单位", cellStyle);
poiUtil.setCellValue(4, 7, "标准状态", cellStyle);

// 设置列宽度自适应
//poiUtil.autoSizeColumn(new int[]{0,1,2,3,4,5,6,7});

// 设置行高
poiUtil.getCurrentSheet().getRow(4).setHeightInPoints((float)1.5*poiUtil.getCurrentSheet().getDefaultRowHeightInPoints());
poiUtil.exportExcel("workbook.xls");

//poiUtil.loadExcel("workbook.xls");
}
*/


/**-----------------------------------------------------Wookbook操作相关-----------------------------------------------------------*

/**
* todo 新建Excel文档,并初始化Sheet1,并指定sheet名字。
*@author 
*@date 2010-10-24 下午02:45:16
*@param fileName
*@return
*@throws Exception
*/
public void createExcel(String sheetName, String fileType) throws Exception
{
HSSFWorkbook wb = this.getWorkBook(fileType);
this.workbook = wb;

// 以文件名为sheet的名字
currentSheet = workbook.createSheet(sheetName);

}

/**
* todo 新建Excel,并初始化Sheet1,不指定sheet名字。
*@author 
*@date 2010-10-24 下午02:45:16
*@param fileName
*@return
*@throws Exception
*/
public void createExcel(String fileType) throws Exception
{
HSSFWorkbook wb = this.getWorkBook(fileType);
this.workbook = wb;

// 以文件名为sheet的名字
currentSheet = wb.createSheet();
}

/**
* todo 加载Excel,并加载sheet1。
*@author 
*@date 2010-10-24 下午03:18:57
*@param fileName
*@throws IOException
*/
public void loadExcel(String fileName) throws IOException
{
//this.workbook = this.getWorkBook(fileName);
InputStream in = new FileInputStream(fileName);
try{
POIFSFileSystem  fs = new POIFSFileSystem(in);
this.workbook= new HSSFWorkbook(fs);
this.currentSheet = this.workbook.getSheetAt(0);
}catch(IOException e)
{
e.printStackTrace();
throw e;


}finally
{
if(in!=null)in.close();
}
}



/**
* todo 加载Excel,并加载sheet1。
*@author 
*@date 2010-10-24 下午03:18:57
*@param fileName
*@throws IOException
*/
public void loadExcel(File file) throws IOException
{
//this.workbook = this.getWorkBook(fileName);
InputStream in = new FileInputStream(file);
try{
POIFSFileSystem  fs = new POIFSFileSystem(in);
this.workbook= new HSSFWorkbook(fs);
this.currentSheet = this.workbook.getSheetAt(0);
}catch(IOException e)
{
e.printStackTrace();
throw e;


}finally
{
if(in!=null)in.close();
}
}



/**
* todo 导出Excel,输出流为文件流。
*@author 
*@date 2010-10-24 下午03:47:31
*@throws IOException
*/
public void exportExcel(String filePath) throws IOException
{
FileOutputStream fileOut = new FileOutputStream(filePath);
this.workbook.write(fileOut);
fileOut.close();
}

/**
* todo 导出Excel,输出流为response。
*@author 
*@date 2010-10-24 下午03:47:31
*@throws IOException
*/
public void exportExcel(HttpServletResponse response) throws IOException
{
this.workbook.write(response.getOutputStream());
}

    /**
     * todo 加载Workbook。
     *@author 
     *@date 2010-10-24 下午02:20:37
     *@param fileName
     *@return
     *@throws IOException
     */
private HSSFWorkbook getWorkBook(String fileType) throws IOException
{
if(FILE_TYPE_XLS.equals(fileType))
{
return new HSSFWorkbook();

}else
{
return null;
}


/**-----------------------------------------------------Sheet操作相关-----------------------------------------------------------*/

/**
* todo 往工作簿中添加新的sheet。
*@author 
*@date 2010-10-24 下午03:55:12
*@param sheetName
*/
public void addSheet(String sheetName)
{
this.currentSheet = this.workbook.createSheet(sheetName);
}

/**
* todo 根据Excel文件名和Sheet名字获取sheet工作布
*@author 
*@date 2010-10-24 下午02:24:30
*@param fileName
*@param sheetName
*@return
*@throws Exception
*/
public  void loadSheet(String sheetName) throws Exception
{
this.currentSheet = this.workbook.getSheet(sheetName);
}



/**-----------------------------------------------------内容相关-----------------------------------------------------------*/



/**
* todo 获取Row
*@author 
*@date 2010-10-26 上午10:51:18
*@param row
*@param col
*@return
*/
public HSSFRow getRow(int row )
{
HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
return hrow;
}


/**
* todo 获取Cell
*@author 
*@date 2010-10-26 上午10:51:18
*@param row
*@param col
*@return
*/
public HSSFCell getCell(int row ,int col)
{
HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
HSSFCell cell = hrow.getCell((short)col);
if(cell==null) cell = hrow.createCell((short)col);

return cell;
}


   /**
    * 根据指定的行列值,确定单元格,并获取单元格内容。
    * @param row
    * @param col
    * @return
    */
public  String getCellValue(int row, int col) {
String ret = "";
HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
HSSFCell cell = hrow.getCell((short)col);
if(cell==null) cell = hrow.createCell((short)col);
if (cell != null) {
if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
ret = cell.getStringCellValue();
ret = ret.replaceAll("\\(", "(").replaceAll("\\)", ")");

} else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
ret = String.valueOf(cell.getBooleanCellValue());
} else if (HSSFCell.CELL_TYPE_FORMULA == cell.getCellType()) {
ret = String.valueOf(cell.getCellFormula());
} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {


//System.out.println(cell.getDateCellValue());

   if (HSSFDateUtil.isCellDateFormatted(cell)) {    
   
       double d = cell.getNumericCellValue();    
        Date date = HSSFDateUtil.getJavaDate(d);  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        ret = sdf.format(date);
    }else
    {
DecimalFormat df = new DecimalFormat("0.000"); 
ret = df.format(MathUtil.div(cell.getNumericCellValue(), 1)); 
// 12.0->12 , 12.11->12.11
if(ret.matches("\\d+\\.0+"))
{
ret = ret.split("\\.")[0];
}
    }

} else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
ret = "";
}
}

ret = ret.replaceAll("^\\s*|\\s*$", "");

return ret;
}

/**
   * 根据指定的行列值,确定单元格,并获取单元格内容(工资条发放)。
   * @param row
   * @param col
   * @return
   */
public  String getCellValue_Gzt(int row, int col) {
String ret = "";
HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
HSSFCell cell = hrow.getCell((short)col);
if(cell==null) cell = hrow.createCell((short)col);
if (cell != null) {
if((row%2==1)&&(col==0)){

if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
ret = cell.getStringCellValue();
ret = ret.replaceAll("\\(", "(").replaceAll("\\)", ")");

}
}
else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
ret = cell.getStringCellValue();
ret = ret.replaceAll("\\(", "(").replaceAll("\\)", ")");
if(ret.matches("\\d+(\\.\\d+)?$")){
DecimalFormat df = new DecimalFormat("0.00"); 
ret = df.format(MathUtil.div(Double.parseDouble(cell.getStringCellValue()), 1));
}

} else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
ret = String.valueOf(cell.getBooleanCellValue());
} else if (HSSFCell.CELL_TYPE_FORMULA == cell.getCellType()) {
ret = String.valueOf(cell.getCellFormula());
} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {


//System.out.println(cell.getDateCellValue());

   if (HSSFDateUtil.isCellDateFormatted(cell)) {    
   
       double d = cell.getNumericCellValue();    
        Date date = HSSFDateUtil.getJavaDate(d);  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        ret = sdf.format(date);
    }else
    {
DecimalFormat df = new DecimalFormat("0.00"); 
ret = df.format(MathUtil.div(cell.getNumericCellValue(), 1)); 
// // 12.0->12 , 12.11->12.11
// if(ret.matches("\\d+\\.0+"))
// {
// ret = ret.split("\\.")[0];
// }
    }

} else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
ret = "";
}
}

ret = ret.replaceAll("^\\s*|\\s*$", "");

return ret;
}


/**
* 根据指定的行列值,确定单元格,并获取单元格内容,其中单元格的内容是时间值。
* @param row
* @param col
* @param format
* @return
*/
public String getDateValue(HSSFRow row, int col, String format) {

SimpleDateFormat formater = new SimpleDateFormat(format);
String ret = "";
HSSFCell cell = row.getCell((short)col);
if (cell != null) {
if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
ret = cell.getStringCellValue();
} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
ret = formater.format(cell.getDateCellValue());
} else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
ret = formater.format(new Date());
}
}
return ret;
}


/**
* 根据指定的行列值,确定单元格,并获取单元格内容,其中单元格的内容是时间值。
* @param row
* @param col
* @param format
* @return
*/
public String getDateValue(int row, int col, String format) {

SimpleDateFormat formater = new SimpleDateFormat(format);
String ret = "";
HSSFRow hrow = currentSheet.getRow(row);
HSSFCell cell = hrow.getCell((short)col);
if (cell != null) {
if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
ret = cell.getStringCellValue();
} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
ret = formater.format(cell.getDateCellValue());
} else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
ret = formater.format(new Date());
}
}
return ret;
}



/**
* todo 根据指定行列值,确定单元格,并设置单元格内容。excel单元格下标从(0,0)开始
*@author 
*@date 2010-10-24 下午04:28:36
*@param row
*@param col
*/
public void setCellValue(int row ,int col,Object value)
{

HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
HSSFCell cell = hrow.getCell((short)col);
if(cell==null) cell = hrow.createCell((short)col);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if(value instanceof String)
{
cell.setCellValue((String)value);

}else if(value instanceof Double)
{
cell.setCellValue(Double.parseDouble(value.toString()));

}else if(value instanceof Integer)
{
cell.setCellValue(Double.parseDouble(value.toString()));

}else if(value instanceof Date)
{
cell.setCellValue((Date) value);

}else if(value instanceof HSSFRichTextString)
{
HSSFRichTextString va = (HSSFRichTextString) value;
cell.setCellValue(va.getString());

}else if(value instanceof Boolean)
{
cell.setCellValue(Boolean.parseBoolean(value.toString()));

}else 
{
cell.setCellValue(value.toString());
}

// 自动换行
cell.getCellStyle().setWrapText(true);


}





/**
* todo 根据指定行列值,确定单元格,并设置单元格内容和单元格样式。excel单元格下标从(0,0)开始
*@author 
*@date 2010-10-26 上午08:57:56
*@param row
*@param col
*@param value
*@param style
*/
public void setCellValue(int row ,int col,Object value,HSSFCellStyle style)
{

HSSFRow hrow = currentSheet.getRow(row);
if(hrow==null) hrow = currentSheet.createRow(row);
HSSFCell cell = hrow.createCell((short)col);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

if(value==null)value="";
if(value instanceof String)
{

cell.setCellValue((String)value);

}else if(value instanceof Double)
{
cell.setCellValue(Double.parseDouble(value.toString()));

}else if(value instanceof Integer)
{
cell.setCellValue(Double.parseDouble(value.toString()));

}else if(value instanceof Date)
{
cell.setCellValue((Date) value);

}else if(value instanceof HSSFRichTextString)
{
cell.setCellValue(((HSSFRichTextString) value).getString());

}else if(value instanceof Boolean)
{
cell.setCellValue(Boolean.parseBoolean(value.toString()));

}else 
{
cell.setCellValue(value.toString());
}
cell.setCellStyle(style);

// 自动换行
cell.getCellStyle().setWrapText(true);
}




/**
* todo 匹配对比两个数据集,获取两者之间的差异信息。返回的结果集数组包括两部分信息:差异List 、没有匹配上的数据Map
*@author 
*@date 2011-2-22 下午02:04:33
*@param zDataList:主要数据集,如数据库数据集
*@param cDataList:次要数据集,如Excel数据集、SAP数据集
*@param keyIndexs: 业务主键下标
*@return
*/
public Object[] getDifferen4TowDataSet(List zDataList,List cDataList , String[] keyIndexs )
{

// 结合业务主键,把次要数据集转换为Map
Map excelDataMap = this.changeList2Map(cDataList,keyIndexs);

// 差异数据集
List diffList = new ArrayList();

for(int i=0;i<zDataList.size();i++)
{
Object[] objs_x  = (Object[])zDataList.get(i);
String key ="";
// 获取Excel中单条数据的业务主键值。
for(int j=0;j<keyIndexs.length;j++)
{
Object o = objs_x[Integer.parseInt(keyIndexs[j])];
key = key + o.toString().replaceAll("^\\s*|\\s*$", "");
}

// 两个数据集包含同一条数据,需要计算差异,并记录差异信息。在计算完差异后,需要把该条数据从次要数据集中删除,避免页面重复显示。
if(excelDataMap.containsKey(key))
{
Object[]  objs_y = (Object[])excelDataMap.get(key);

for(int j=0;j<objs_x.length;j++)
{
if(!objs_x[j].equals(objs_y[j]))
{
String[] cyItem = new String[]{""+i,""+j,objs_y[j].toString()};
diffList.add(cyItem);
}
}

//把已经进行了差异匹配计算的数据从次要数据集中删除。
excelDataMap.remove(key);
}

}

Object[] returnObj = new Object[2];
returnObj[0] =  diffList;
returnObj[1] =  excelDataMap;

return returnObj;
}




/**
* todo  获取Excel指定的Sheet下所有的数据。要求去除空行。
*@author 
*@date 2011-2-22 上午09:28:19
*@param beginRowNum :有效数据集开始行,行位置。
*@return
*/
public List getExcelDataList(int beginRowNum)
{
List dataLsit = new ArrayList();
// 数据Excel的一个空行
int dataExcelFirstNullRowIndex = this.getFirstNullRowIndex();

// 如果数据Excel的第一个空行位置<=模板第一个空行的位置,说明数据Excel为空,或者没有按照模板填写,此时返回数据集为空。
if(dataExcelFirstNullRowIndex<=beginRowNum)
{
return dataLsit;
}

int rowNum = this.currentSheet.getLastRowNum()+1; // 行数
int cellNum = this.currentSheet.getRow(beginRowNum).getLastCellNum(); // 列数

for(int i=beginRowNum;i<rowNum; i++)
{
Object[] objs  = new Object[cellNum];

// 如果当前行第一个单元格内容为:合计,填报人,填报说明,表示已经到了行尾,后续的内容无需再读取。
String strValue = this.getCellValue(i, 0)==null?"":this.getCellValue(i, 0);
if(strValue.matches(".*合计.*")||strValue.matches(".*填报人.*")||strValue.matches(".*填报说明.*"))
{
break;
}

boolean isNullRow = true;
for(int j=0;j<cellNum;j++)
{
objs[j]=this.getCellValue(i, j);
//  判断当前行是否为空行。判断空行的标准时:该行所有单元格值都为null;
if(objs[j]!=null&&!"".equals(objs[j])&&isNullRow)
{
isNullRow = false;
}
}

if(!isNullRow){
dataLsit.add(objs);
}
}

return dataLsit;
}

/**
* todo  获取Excel指定的Sheet下所有的数据。要求去除空行。
*@author 
*@date 
*@param beginRowNum :有效数据集开始行,行位置。
*@return
*/
public List getGztExcelDataList(int beginRowNum)
{
List dataLsit = new ArrayList();
// 数据Excel的一个空行
int dataExcelFirstNullRowIndex = this.getFirstNullRowIndex();

// 如果数据Excel的第一个空行位置<=模板第一个空行的位置,说明数据Excel为空,或者没有按照模板填写,此时返回数据集为空。
if(dataExcelFirstNullRowIndex<=beginRowNum)
{
return dataLsit;
}

int rowNum = this.currentSheet.getLastRowNum()+1; // 行数
int cellNum = 30; // 列数

for(int i=beginRowNum;i<rowNum; i++)
{
Object[] objs  = new Object[cellNum];

// 如果当前行所有单元格都为null时,表示已经到了行尾,后续的内容无需再读取。

String strValue = this.getCellValue_Gzt(i, 0)==null?"":this.getCellValue_Gzt(i, 0);
if(strValue == null)
{
break;
}

for(int j=0;j<cellNum;j++)
{
objs[j]=this.getCellValue_Gzt(i, j);
}
dataLsit.add(objs);
}

return dataLsit;
}




/**
* todo 把List转换为Map,key值由Excel业务主键下标决定。
*@author 
*@date 2011-2-22 下午01:27:46
*@param dataList :待转换的list
*@param excelKeyStr :Excel业务主键下标字符串 如:“1,2”
*@return
*/
public Map changeList2Map(List dataList,String[] keyIndexs)
{
Map dataMap = new HashMap();
for(int i=0;i<dataList.size(); i++)
{
Object[] objs  = (Object[])dataList.get(i);
String key ="";
// 获取Excel中单条数据的业务主键值。
for(int j=0;j<keyIndexs.length;j++)
{
Object o = objs[Integer.parseInt(keyIndexs[j])];
key = key + o.toString();
}

dataMap.put(key, objs);
}

return dataMap;
}









/**
* todo 获取Excel下当前活动sheet的第一个空行所处位置,下标从“0”开始计算。
*@author 
*@date 2011-2-22 上午09:16:03
*@return
*/
public int getFirstNullRowIndex()
{
int rowNum = this.currentSheet.getLastRowNum()+1; // 行数
//int cellNum = this.currentSheet.getRow(1).getLastCellNum(); // 列数
for(int i=0;i<rowNum; i++)
{
boolean isNullRow = true;
// 由于不知道Excel到底有多少列,以不变应万变,假设报表最多有100列(很难想象一个报表会有100多列)。
for(int j=0;j<100;j++)
{
Object obj =this.getCellValue(i, j);

//  判断当前行是否为空行。判断空行的标准时:该行所有单元格值都为null;
if(obj!=null&&!"".equals(obj)&&isNullRow)
{
isNullRow = false;
}
}

// 返回当前空行的行位置
if(isNullRow){
return i;
}
}

// 如果当前没有空行,则返回最后行的行位置
return rowNum;
}



/**-----------------------------------------------------样式相关-----------------------------------------------------------*/

    /**
     * todo 创建样式渲染器
     *@author 
     *@date 2010-10-26 上午09:01:09
     *@return
     */
public HSSFCellStyle createCellStyle()
{
return this.workbook.createCellStyle();
}

/**
* todo 返回字体渲染器
*@author 
*@date 2010-10-26 上午09:02:01
*@return
*/
public HSSFFont createFont()
{
return this.workbook.createFont();
}

/**
* todo 为指定的单元格设置样式
*@author 
*@date 2010-10-26 上午08:40:08
*@param row
*@param col
*@param style
*/
public void setStyle(int row ,int col ,HSSFCellStyle style)
{
this.currentSheet.getRow(row).getCell((short)col).setCellStyle(style);
}

/**
* todo 获取单元格默认的的边框样式,包括边框线条颜色,边框线条粗细。
*@author 
*@date 2010-10-25 下午06:11:18
*@return
*/
public  HSSFCellStyle getDefaultBorderStyle()
{
HSSFCellStyle style = this.workbook.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
return style;

}

/**
* todo 设置单元格边框样式。
*@author 
*@date 2010-10-25 下午06:12:25
*@param row
*@param col
*@param style
*/
public void setBorderStyle(int row ,int col ,HSSFCellStyle style )
{
this.currentSheet.getRow(row).getCell((short)col).setCellStyle(style);

}



/**
* todo 设置单元格前景颜色(背景颜色),颜色值由RGB数值表示。
*@author 
*@date 2010-10-26 上午09:36:46
*@param R
*@param G
*@param B
*@param style
*/
public void setFillForegroundColor(int R , int G ,int B , HSSFCellStyle style )
{

HSSFWorkbook hworkbook = (HSSFWorkbook)workbook;
HSSFPalette palette = hworkbook.getCustomPalette();
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) R,  //RGB red (0-255)
(byte) G,    //RGB green
(byte) B     //RGB blue
);

style.setFillForegroundColor(HSSFColor.RED.index);
}


public HSSFSheet getCurrentSheet() {
return currentSheet;
}


public void setCurrentSheet(HSSFSheet currentSheet) {
this.currentSheet = currentSheet;
}


public HSSFWorkbook getWorkbook() {
return workbook;
}


public void setWorkbook(HSSFWorkbook workbook) {
this.workbook = workbook;
}





}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值