excel中插入一行

怎样在excel中插入一行,下面的代码是在excel2003中插入一行,而且是插在第一行上面,并在插入行中放入一个图片
	/**
	 * 在Excle2003文档插入图片
	 * @author sha1064616837
	 * @param fileWordPath
	 *            Excel文档绝对路径
	 * @param newText
	 *            需要插入的条形码图片的绝对路径
	 */
	public static boolean insertImage2Excel03(String fileExcelPath,
			String fileImagePath) {
		FileOutputStream fileOut = null;
		BufferedImage bufferImg = null;
		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
		FileInputStream fis = null;
		try {
			bufferImg = ImageIO.read(new File(fileImagePath));
			//将一个图像写入outputStream
			ImageIO.write(bufferImg, "jpeg", byteArrayOut);
			
			HSSFWorkbook wb = null;
			fis = new FileInputStream(new File(fileExcelPath));
			wb = new HSSFWorkbook(fis);

			HSSFSheet sheet1 = null;
			//遍历该excel下的所有页签
			  for (int i = 0; i < wb.getNumberOfSheets(); i++)
            {
                byteArrayOut = new ByteArrayOutputStream();
                HSSFSheet sheet = wb.getSheetAt(i);
                // 将图像写入outputStream
                ImageIO.write(bufferImg, "jpeg", byteArrayOut);
                // 获得区域,在第一行上面插入一行
                sheet.shiftRows(0, sheet.getLastRowNum(), 1, false, false);
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                // 如果第一行不为空
                if (null != sheet.getRow(0))
                {
                    // 设置第一行高度
                    sheet.getRow(0).setHeightInPoints((float) 65);
                    // 设置条码位置在(0列,0行,1列,1行)
                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
                            (short) 0, 0, (short) 2, 1);
                    // 把jpeg图片通过流写入该sheet1
                    HSSFPicture picture = patriarch.createPicture(anchor, wb
                            .addPicture(byteArrayOut.toByteArray(),
                                    HSSFWorkbook.PICTURE_TYPE_JPEG));
                    // 把图片设置成原始大小
                    picture.resize();
                }
                byteArrayOut.flush();
                byteArrayOut.close();
                byteArrayOut = null;
            }
            fileOut = new FileOutputStream(fileExcelPath);
            bos = new BufferedOutputStream(fileOut);
            wb.write(bos);
            bos.flush();
            bos.close();
                 }
        catch (IOException io)
        {
            io.printStackTrace();
            debugLog.error("io erorr :  " + io.getMessage());
            return false;
        }
        finally
        {
            // 关闭byteArrayOut流
            if (null != byteArrayOut)
                try
                {
                    byteArrayOut.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            // 关闭fis流
            if (null != fis)
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            // 关闭fileOut流
            if (null != fileOut)
                try
                {
                    fileOut.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            // 关闭fileOut流
            if (null != bos)
                try
                {
                    bos.close();
                    bos = null;
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
        return true;
    }

你可能感兴趣的:(java,poi,Excel)