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

/**
*名称:outExcel
*功能描述:导出
*输入参数:P_ARR 导出的集合,length列数,P_ARR1 合并的集合
*输出参数:1,0
*创建日期:2015-6-25
*创建人:mxt
*修订日期:
*修订人:
*修订原因:
**/
public String outExcel(ArrayList<Map> P_ARR,int collength,int rowlength,String nametitle,ArrayList<Map> 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<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值