java中excel导出包括合并单元格和单元格样式

/**
*名称:outExcel
*功能描述:导出
*输入参数:P_ARR 导出的集合,length列数,P_ARR1 合并的集合
*输出参数:1,0
*创建日期:2015-6-25
*创建人:mxt
*修订日期:
*修订人:
*修订原因:
**/
public String outExcel(ArrayList P_ARR,int collength,int rowlength,String nametitle,ArrayList P_ARR1)
{
String flag="0";
//初始一个workbook 
HSSFWorkbook workbook = new HSSFWorkbook(); 




//获取第一张
   HSSFSheet sheet = workbook.createSheet("sheet"); 
HSSFRow rowTitle = sheet.createRow(0); 
rowTitle.setHeightInPoints(30);
HSSFCell ctitle = rowTitle.createCell(0); 

ctitle.setCellValue(new HSSFRichTextString(nametitle.split("\\.")[0])); 


HSSFCellStyle cellStyle1 = workbook.createCellStyle(); 
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平布局:居中
        // 设置字体  
        HSSFFont font = workbook.createFont();  
        font.setFontHeightInPoints((short) 20); //字体高度 
        cellStyle1.setFont(font);
        
ctitle.setCellStyle(cellStyle1);



HSSFCellStyle cellStyle = workbook.createCellStyle(); 
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 


HSSFCellStyle cellStyle2 = workbook.createCellStyle(); 
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平布局:居中
cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

for (int i = 0; i < rowlength; i++) {
//创建合并单元格的第一个单元格数据 
HSSFRow row = sheet.createRow(i+1); 
Map map = new HashMap();
map = P_ARR.get(i);
for(int j=1;j<=collength;j++)
{
HSSFCell c0 = row.createCell(j-1); 
c0.setCellValue(new HSSFRichTextString(map.get("a"+j))); 
c0.setCellStyle(cellStyle2);

}
}

//设置合并单元格的区域  行开始,列开始,行结束,列结束  标题
Region region1 = new Region(0, (short)0, 0, (short)(collength-1)); 
sheet.addMergedRegion(region1); 



for (int i = 0; i < P_ARR1.size(); i++) {
//创建合并单元格的第一个单元格数据 
Map map = new HashMap();
map = P_ARR1.get(i);
Region regionReport = new Region(Integer.parseInt(map.get("srow")), (short)Integer.parseInt(map.get("scol")), Integer.parseInt(map.get("erow")), (short)Integer.parseInt(map.get("ecol")));
sheet.addMergedRegion(regionReport); 
}

OutputStream outputStream = null;
try {
Properties prop = new Properties();
try {
prop.load(this.getClass().getClassLoader()
.getResourceAsStream("com/my.properties")); 
} catch (IOException e) {
e.printStackTrace();
}
flag=prop.getProperty("excleOutPath");
flag=flag+"/"+  System.currentTimeMillis()+".xls";
outputStream = new FileOutputStream(new File(flag));
} catch (FileNotFoundException e) {
flag="1";
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.write(outputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
flag="1";
e.printStackTrace();
}
finally
{
if(outputStream!=null)
{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}

你可能感兴趣的:(心血来潮)