单元格合并已经完成,先看下结果:
Java代码:
public class createExcelLa {
/*
* exportXLSFile
*描述: 在这个方法中,生成EXCEL,并填充数据
* */
public static void createXLSFile() throws Exception {
String strFileName = "E:\\wdkFile\\user_test.xls";
File f = new File(strFileName);
f.createNewFile();
WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream(f));//Excel工作薄
WritableSheet ws = wwb.createSheet("sheet1", 0);//Excel工作表
ws.setColumnView(0, 30); // 设置列的宽度
ws.setColumnView(1, 30); // 设置列的宽度
ws.setColumnView(2, 20); // 设置列的宽度
ws.setColumnView(3, 30); // 设置列的宽度
//设置标题的字体
WritableFont wfc = new WritableFont(
WritableFont.ARIAL, 10, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
wfc);
wcfFC.setBackground(jxl.format.Colour.GRAY_25);// 设置单元格的颜色为灰色
wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框为黑色
//设置内容的字体
WritableFont wfcInfo = new WritableFont(
WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFCInfo = new jxl.write.WritableCellFormat(
wfcInfo);
wcfFCInfo.setBackground(jxl.format.Colour.GRAY_25);
wcfFCInfo.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//合并框的字体
WritableCellFormat wcfFCMerge = new WritableCellFormat(
wfc);
wcfFCMerge.setBackground(jxl.format.Colour.GRAY_25);// 设置单元格的颜色为灰色
wcfFCMerge.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);//设置边框为黑色
wcfFCMerge.setAlignment(jxl.format.Alignment.CENTRE);
wcfFCMerge.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfFCMerge.setWrap(true);
int allIndex = 0; //行数
//标题的字体
Label labelTit =null;
Label label1 =null;
Label label2 = null;
Label label3 =null;
Label label4 = null;
//第一行
labelTit = new Label(0, allIndex, "一级分类",wcfFCMerge);
ws.addCell(labelTit);
labelTit = new Label(1, allIndex, "二级分类",wcfFCMerge);
ws.addCell(labelTit);
labelTit = new Label(2, allIndex, "",wcfFCMerge);
ws.addCell(labelTit);
labelTit = new Label(3, allIndex, "三级分类",wcfFCMerge);
ws.addCell(labelTit);
ws.mergeCells(1, 0, 2, 0);//列 行 跨列 跨行
allIndex++;
int oneMagStart = allIndex;//一级分类 需要 合并的单元格 开始的行数
label1 = new Label(0, allIndex, "基本属性",wcfFCMerge);
ws.addCell(label1);
label2 = new Label(1, allIndex, "姓名",wcfFCInfo);
label4 = new Label(3, allIndex,"张三",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
allIndex++;
label2 = new Label(1, allIndex, "年龄",wcfFCInfo);
label4 = new Label(3, allIndex,"26",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
allIndex++;
//联系方式对应手机号和QQ ,所以 联系方式 需作为合并后的单元格
label2 = new Label(1, allIndex, "联系方式",wcfFCInfo);
ws.addCell(label2);
int twoMagStart = allIndex;//二级级分类 需要 合并的单元格 开始的行数
label3 = new Label(2, allIndex, "手机号",wcfFCInfo);
label4 = new Label(3, allIndex,"18041028280",wcfFCInfo);
ws.addCell(label3);
ws.addCell(label4);
allIndex++;
label3 = new Label(2, allIndex, "QQ",wcfFCInfo);
label4 = new Label(3, allIndex,"64383147",wcfFCInfo);
ws.addCell(label3);
ws.addCell(label4);
int twoMagEnd=allIndex;//二级合并 结束行数
ws.mergeCells(1, twoMagStart, 1, twoMagEnd);//进行合并
allIndex++;
label2 = new Label(1, allIndex, "工领",wcfFCInfo);
label4 = new Label(3, allIndex,"2年",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
for (int j = oneMagStart;j <= allIndex;j++) {
if(j<twoMagStart || j>twoMagEnd){
ws.mergeCells(1, j, 2, j);
}
}
ws.mergeCells(0, oneMagStart, 0, allIndex);//一级分类进行合并
allIndex++;
oneMagStart=allIndex;//第二个一级分类
label1 = new Label(0, allIndex, "收入信息",wcfFCMerge);
ws.addCell(label1);
label2 = new Label(1, allIndex, "应发金额",wcfFCInfo);
label4 = new Label(3, allIndex,"5000元",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
allIndex++;
label2 = new Label(1, allIndex, "五险一金",wcfFCInfo);
label4 = new Label(3, allIndex,"1000元",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
allIndex++;
label2 = new Label(1, allIndex, "实发金额",wcfFCInfo);
label4 = new Label(3, allIndex,"4000元",wcfFCInfo);
ws.addCell(label2);
ws.addCell(label4);
for (int j = oneMagStart;j <= allIndex;j++) {
ws.mergeCells(1, j, 2, j);
}
ws.mergeCells(0, oneMagStart, 0, allIndex);//一级分类进行合并
wwb.write();
wwb.close();
}
public static void main(String args[]) throws Exception {
createXLSFile();
}
}
下拉单元格,插入图像,后续我会用到了再发
另外给大家推荐一个EXCEL开发文档,很详细,值得学习
http://www.cr173.com/html/10377_1.html