") } sb.append("
") } } //关闭资源释放内存 wb.close() return sb.toString() } 二.写入Excel文件格式比文本内容加粗加某些颜色等参考jxlapi同推荐篇错文章:/developerworks/cn/java/l-javaExcel/?ca=j-t10java 代码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=0i0){ //每工作表进行循环 for(int i=0iif(breakSheet) break //前工作表行数 int rowNum = sheet[i].getRows() boolean breakRow = false for(int j=0jif(breakRow) break //前行所单元格 Cell[] cells = sheet[i].getRow(j) if(cells!=null&&cells.length>0){ boolean breakCell = false //每单元格进行循环 for(int k=0kif(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", "2007-03-06", "第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)//建立标签参数依:列索引、行索引、内容、格式Label seqLabel = new Label(0, 0, "序号", headerFormat)//增加单元格sheet.addCell(seqLabel)//合并单元格参数依:列索引、行索引、列索引+需要合并列数、行索引+需要合并行数sheet.mergeCells(0, 0, 0, 1)//设置单元格宽度字符单位sheet.setColumnView(0, "序号".length() + 10)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合并单元格
获取合并单元格的值 @param sheet @param row @param column @return。
public String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions()
for (int i = 0i <sheetMergeCounti++) {
CellRangeAddress ca = sheet.getMergedRegion(i)
int firstColumn = ca.getFirstColumn()
int lastColumn = ca.getLastColumn()
int firstRow = ca.getFirstRow()
int lastRow = ca.getLastRow()
if (row >= firstRow &&row <= lastRow) {
if (column >= firstColumn &&column <= lastColumn) {
Row fRow = sheet.getRow(firstRow)
Cell fCell = fRow.getCell(firstColumn)
return getCellValue(fCell)
}
}
}
return null
}
POIexcel表格何合并单元格Java代码
import java.io.FileOutputStream
import java.io.IOException
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFCellStyle
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.util.Region
public class ExcelTest {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
try {
HSSFWorkbook wb = new HSSFWorkbook()
HSSFSheet sheet = wb.createSheet("new sheet")
HSSFCellStyle style = wb.createCellStyle()// 式象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER)// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER)// 水平
HSSFRow row = sheet.createRow((short) 0)
HSSFRow row2 = sheet.createRow((short) 1)
sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0))
HSSFCell ce = row.createCell((short) 0)
ce.setEncoding(HSSFCell.ENCODING_UTF_16)// 文处理
ce.setCellValue("项目\\期")// 表格第行第列显示数据
ce.setCellStyle(style)// 式居
int num = 0
for (int i = 0i <9i++) { // 循环9每都要跨单元格显示
// 计算单元格跨格
int celln = 0
int celle = 0
if (i == 0) {
celln = 0
celle = 1
} else {
celln = (i * 2)
celle = (i * 2 + 1)
}
// 单元格合并
// 四参数别:起始行起始列结束行结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)))
HSSFCell cell = row.createCell((short) (celln + 1))
cell.setCellValue("merging" + i)// 跨单元格显示数据
cell.setCellStyle(style)// 式
// 跨单元格显示数据:两行行别两格格行两格数量金额
HSSFCell cell1 = row2.createCell((short) celle)
HSSFCell cell2 = row2.createCell((short) (celle + 1))
cell1.setEncoding(HSSFCell.ENCODING_UTF_16)
cell1.setCellValue("数量")
cell1.setCellStyle(style)
cell2.setEncoding(HSSFCell.ENCODING_UTF_16)
cell2.setCellValue("金额")
cell2.setCellStyle(style)
num++
}
// 面加合计百比
// 合计 加要跨单元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)))
HSSFCell cell = row.createCell((short) (2 * num + 1))
cell.setEncoding(HSSFCell.ENCODING_UTF_16)
cell.setCellValue("合计")
cell.setCellStyle(style)
HSSFCell cell1 = row2.createCell((short) (2 * num + 1))
HSSFCell cell2 = row2.createCell((short) (2 * num + 2))
cell1.setEncoding(HSSFCell.ENCODING_UTF_16)
cell1.setCellValue("数量")
cell1.setCellStyle(style)
cell2.setEncoding(HSSFCell.ENCODING_UTF_16)
cell2.setCellValue("金额")
cell2.setCellStyle(style)
// 百比 同
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)))
HSSFCell cellb = row.createCell((short) (2 * num + 3))
cellb.setEncoding(HSSFCell.ENCODING_UTF_16)
cellb.setCellValue("百比")
cellb.setCellStyle(style)
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3))
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4))
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16)
cellb1.setCellValue("数量")
cellb1.setCellStyle(style)
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16)
cellb2.setCellValue("金额")
cellb2.setCellStyle(style)
FileOutputStream fileOut = new FileOutputStream("workbook.xls")
wb.write(fileOut)
fileOut.close()
System.out.print("OK")
} catch (Exception ex) {
ex.printStackTrace()
}
}
}