1.java怎么操作excel合并单元格
java怎么操作excel合并单元格
利用java操作Excel源东东-jxl.jar读取前单元格值 String cellValue = cells[k].getContents(); sb.append(cellValue+" "); } } sb.append("
"); } sb.append("
"); } } //关闭资源释放内存 wb.close(); return sb.toString(); } 二.写入Excel文件格式比文本内容加粗加某些颜色等参考jxlapi同推荐篇错文章:/developerworks/cn/java/l-javaExcel/?ca=j-tjava 代码public static void writeExcel(String fileName){ WritableWorkbook wwb = null; try { //首先要使用Workbook类工厂创建写入工作薄(Workbook)象 wwb = Workbook.createWorkbook(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } if(wwb!=null){ //创建写入工作表 //WorkbookcreateSheet两参数第工作表名称第二工作表工作薄位置 WritableSheet ws = wwb.createSheet("sheet1", 0); //面始添加单元格 for(int i=0;i0){ //每工作表进行循环 for(int i=0;iif(breakSheet) break; //前工作表行数 int rowNum = sheet[i].getRows(); boolean breakRow = false; for(int j=0;jif(breakRow) break; //前行所单元格 Cell[] cells = sheet[i].getRow(j); if(cells!=null&&cells.length>0){ boolean breakCell = false; //每单元格进行循环 for(int k=0;kif(breakCell) break; //读取前单元格值 String cellValue = cells[k].getContents(); if(cellValue==null) continue; if(cellValue.contains(keyWord)){ res = true; breakCell = true; breakRow = true; breakSheet = true; } } } } } } //关闭资源释放内存 wb.close(); return res; } 四.往Excel插入图标插入实现容易参看代码:java 代码public static void insertImg(WritableSheet dataSheet, int col, int row, int width, int height, File imgFile){ WritableImage img = new WritableImage(col, row, width, height, imgFile); dataSheet.addImage(img); } 代码注释已经清楚概用再解释我用程序验证:java 代码try { //创建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入工作表 WritableSheet imgSheet = workbook.createSheet("Images",0); //要插入文件 File imgFile = new File("D:/1.png"); //插入第二行第单元格宽各占六单元格 insertImg(imgSheet,0,1,6,6,imgFile); workbook.write(); workbook.close(); catch (IOException e) { e.printStackTrace(); catch (WriteException e) { e.printStackTrace(); jxl支持png格式jpg格式gif格式都支持五.插入页眉页脚般页眉页脚都三部,左,,右三部,利用代码实现插入页眉页脚java 代码public static void setHeader(WritableSheet dataSheet,String left,String center,String right){ HeaderFooter hf = new HeaderFooter(); hf.getLeft().append(left); hf.getCentre().append(center); hf.getRight().append(right); //加入页眉 dataSheet.getSettings().setHeader(hf); //加入页脚 //dataSheet.getSettings().setFooter(hf); } 我用代码测试该:java 代码try { //创建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入工作表 WritableSheet dataSheet = workbook.createSheet("加入页眉",0); ExcelUtils.setHeader(dataSheet, "chb", "--", "第1页,共3页"); workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } 合并单元格://首先创建sheetWritableSheet sheetWrite=writeWorkbook.createSheet("sheet名称",0);// 单元格(column, row)单元格(column1, row1)进行合并sheetWrite.mergeCells(column, row, column1, row1);java 操作exceljxl加边框jxl合并单元格,单元格设置单元WritableCellFormat headerFormat = new WritableCellFormat(HEADER_FONT_STYLE);//水平居齐headerFormat.setAlignment(Alignment.CENTRE);//竖直向居齐headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//建立标签参数依:列索引、行索引、tftphpa源码内容、猫咪社区 网站源码格式Label seqLabel = new Label(0,仙凡幻想源码 0, "序号", headerFormat);//增加单元格sheet.addCell(seqLabel);//合并单元格参数依:列索引、行索引、钓鱼源码干嘛的列索引+需要合并列数、2020秒余额源码行索引+需要合并行数sheet.mergeCells(0, 0, 0, 1);//设置单元格宽度字符单位sheet.setColumnView(0, "序号".length() + );Label basicInfoLabel = new Label(1, 0, "基本信息",headerFormat);sheet.addCell(basicInfoLabel);sheet.mergeCells(1, 0, 3, 0);//合并垮3行sheet.mergeCells(1, 1, 1, 3);//合并垮3列label = new Label(0, 1, "苹);sheet.mergeCells(0,1, 0, 3- 1);//合并垮3列WritableCellFormat cellFormat = new WritableCellFormat();cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居label.setCellFormat(cellFormat);sheet.addCell(label);注:再使cellFormat需要重新NEW WritableCellFormatjava怎么操作excel合并单元格