JXL导出EXCEL合并单元格、文字对齐方式 .

public void createNoLotteryAlarmExcel(String filePath,String fileName,List<NoLotteryAlarm> alarmLimits) throws Exception{

//这里要注意每一行标题要留出要合并的位置,我这里是“”
String[] tittles={"序号","整机缺货状态","区域","彩票机号","整机余量","货道一","","货道二","","货道三","","货道四","","售货机号"};
String[] tittles2={"","","","","","票种-面额","余量","票种-面额","余量","票种-面额","余量","票种-面额","余量"};
OutputStream os=null;
WritableWorkbook ww=null;
File file=new File(filePath);
if(!file.exists()){
file.mkdir();
}
os=new FileOutputStream(filePath+fileName);
ww=Workbook.createWorkbook(os);
WritableSheet sheet=ww.createSheet(fileName, 0);

//文字对齐方式
        WritableCellFormat wcf = new WritableCellFormat();  
        wcf.setAlignment(Alignment.CENTRE);//把水平对齐方式指定为居中 
        wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//把垂直对齐方式指定为居中 

这里大家可以参考我上面导出的结果看看,下面的如何合并单元格的
//合并单元格
//WritableSheet.mergeCells(int m,int n,int p,int q);
        //作用是从(m,n)到(p,q)的单元格全部合并[我的理解:m列、n行和p列、q行合并]
sheet.mergeCells(0, 0, 0, 1);//设置第一列、第一行和 第一列、第二行合并
sheet.mergeCells(1, 0, 1, 1);
sheet.mergeCells(2, 0, 2, 1);//这里是标题“区域” 它合并的是第3列第1行和第3列第2行
sheet.mergeCells(3, 0, 3, 1);
sheet.mergeCells(4, 0, 4, 1);
sheet.mergeCells(5, 0, 6, 0);//货道一
sheet.mergeCells(7, 0, 8, 0);//货道二
sheet.mergeCells(9, 0, 10,0);//货道三
sheet.mergeCells(11, 0, 12,0);//货道四
sheet.mergeCells(13, 0, 13, 1);
try {//添加第一行标题
for (int i = 0; i < tittles.length; i++) {
sheet.addCell(new Label(i, 0, tittles[i],wcf));//i列,1行,title内容,wcf样式
}//添加第二行标题
for (int i = 0; i < tittles2.length; i++) {
sheet.addCell(new Label(i, 1, tittles2[i],wcf));//i列,2行,title内容,wcf样式
}
sheet.setColumnView(1, 15);//设置第2列的宽度
sheet.setColumnView(3, 15);//设置第4列的宽度
int count=2;
if(alarmLimits!=null){
for (int i = 0; i < alarmLimits.size(); i++) {
NoLotteryAlarm alarmLimit=alarmLimits.get(i);
sheet.addCell(new Label(0, count, i+1+"",wcf));

if (StringUtil.isNotEmpty(alarmLimit.getTotal_alerm_content())) {
Integer str=alarmLimit.getTotal_alerm_content();
if(str.equals(Constant.NO_LOTTERY_ALERM_JJ)){
sheet.addCell(new Label(1,count, "紧急缺货",wcf));
}
else if(str.equals(Constant.NO_LOTTERY_ALERM_ZY)){
sheet.addCell(new Label(1,count,"重要缺货",wcf));
}
else{
sheet.addCell(new Label(1,count,"余量充足",wcf));
}
}else {
sheet.addCell(new Label(1,count,"",wcf));
}
if(StringUtil.isNotEmpty(alarmLimit.getArea_name())){
sheet.addCell(new Label(2, count, String.valueOf(alarmLimit.getArea_name()),wcf));
}else {
sheet.addCell(new Label(2, count, "",wcf));
}
if(StringUtil.isNotEmpty(alarmLimit.getLottery_hard_seq())){
sheet.addCell(new Label(3, count, alarmLimit.getLottery_hard_seq(),wcf));
}else {
sheet.addCell(new Label(3, count, "",wcf));
}
sheet.addCell(new Label(4, count, alarmLimit.getTotal_count()+"",wcf));
String str1="";
if(alarmLimit.getLottery_name1()!=null)str1+=alarmLimit.getLottery_name1()+"-";
if(alarmLimit.getLottery_price1()!=null)str1+=alarmLimit.getLottery_price1();
sheet.addCell(new Label(5, count, str1,wcf));
sheet.setColumnView(5, 15);//设置第6列的宽度
if(alarmLimit.getA01_count()!=null)sheet.addCell(new Label(6, count, alarmLimit.getA01_count()+"",wcf));
else sheet.addCell(new Label(6, count, "",wcf));
String str2="";
if(alarmLimit.getLottery_name2()!=null)str2+=alarmLimit.getLottery_name2()+"-";
if(alarmLimit.getLottery_price2()!=null)str2+=alarmLimit.getLottery_price2();
sheet.addCell(new Label(7, count, str2,wcf));
sheet.setColumnView(7, 15);//设置第8列的宽度
if(alarmLimit.getA02_count()!=null)sheet.addCell(new Label(8, count, alarmLimit.getA02_count()+"",wcf));
else sheet.addCell(new Label(8, count, "",wcf));
String str3="";
if(alarmLimit.getLottery_name3()!=null)str3+=alarmLimit.getLottery_name3()+"-";
if(alarmLimit.getLottery_price3()!=null)str3+=alarmLimit.getLottery_price3();
sheet.addCell(new Label(9, count,str3,wcf));
sheet.setColumnView(9, 15);//设置第10列的宽度
if(alarmLimit.getA03_count()!=null)sheet.addCell(new Label(10, count, alarmLimit.getA03_count()+"",wcf));
else sheet.addCell(new Label(10, count, "",wcf));
String str4="";
if(alarmLimit.getLottery_name4()!=null)str4+=alarmLimit.getLottery_name4()+"-";
if(alarmLimit.getLottery_price4()!=null)str4+=alarmLimit.getLottery_price4();
sheet.addCell(new Label(11, count,str4,wcf));
sheet.setColumnView(11, 15);//设置第12列的宽度
if(alarmLimit.getA04_count()!=null)sheet.addCell(new Label(12, count, alarmLimit.getA04_count()+"",wcf));
else sheet.addCell(new Label(12, count, "",wcf));
sheet.addCell(new Label(13, count, alarmLimit.getUbox_code(),wcf));
count++;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
ww.write();
ww.close();
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

猜你喜欢

转载自blog.csdn.net/lhzjj/article/details/41144367