poi给word的表格添加新的一行并添加数据,怎么所有的数据都挤在一行啊

public void newRow(XWPFDocument document,int number,List list) throws Exception{
//获取所有表格

List tables = document.getTables();

//这里简单取第一个表格

XWPFTable table = tables.get(number);

    //获取表头
    XWPFTableRow header = table.getRow(1); 

    for (int h = 1 ; h <= list.size() ; h++) {
        //添加新的一行  
        //第一个参数是新增行的样式,在原表中取XWPFTableRow header = table.getRow(1);  
        //第二个参数是表示插入到第几行
        table.addRow(header,h);
        }

    List<XWPFTableRow> rows = table.getRows();
    System.out.println("list数量"+rows.size());


    for(int i = 0; i < list.size();i++){

// XWPFTableRow row = table.getRow(i+1);
String[] string = list.get(i);
for(int j = 0 ;j < string.length;j++){
System.out.println("行"+rows.get(i+2).getCell(j).getText());
// rows.get(i+2).getCell(j).setText(string[j]);

                rows.get(i+2).getTableCells().get(j).setText(string[j]);
            }
    }
}

你的问题的原因是:XWPFTableRow header = table.getRow(1); 这句取到的是一个指针地址,当你用table.addRow(header,h);的时候创建了XWPFTableRow的实例,这个被创建的实例里的cell的指针都是直接引用的header里cell的地址值,意思就是你用这个方法创建的行里的每个cell实际地址都是指向了header里的cell的地址,所以你所有的操作都会被填充到了header这行里的cell中去。
二楼其实给出了部分解决方法,但高人说话很简约,所以你没看懂,我这里给出一个方案就是先用XWPFTable.insertNewTableRow(int pos)在指定行创建XWPFTableRow实例,然后将header的TrPr值set到新创建的XWPFTableRow实例中,如果这个新的行实例里有cell的话,也要把header里的cell的TrPr值对应的set到新行的cell里去。

赋值行时使用以下 copyTableRow 方法。

直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。

/**
 * 
 * 复制RUN,从source到target
 * @param target
 * @param source
 * 
 */
public void copyRun(XWPFRun target, XWPFRun source) {
    // 设置run属性
    target.getCTR().setRPr(source.getCTR().getRPr());
    // 设置文本
    target.setText(source.text());
    // 处理图片
    List<XWPFPicture> pictures = source.getEmbeddedPictures();

    for (XWPFPicture picture : pictures) {
        try {
            copyPicture(target, picture);
        } catch (InvalidFormatException e) {
            logger.error("copyRun", e);
        } catch (IOException e) {
            logger.error("copyRun", e);
        }
    }
}

/**
 * 
 * 复制图片到target
 * @param target
 * @param picture
 * @throws IOException
 * @throws InvalidFormatException
 * 
 */
public void copyPicture(XWPFRun target, XWPFPicture picture)throws IOException, InvalidFormatException {

    String filename = picture.getPictureData().getFileName();
    InputStream pictureData = new ByteArrayInputStream(picture
            .getPictureData().getData());
    int pictureType = picture.getPictureData().getPictureType();
    int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
            .getCx();

    int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
            .getCy();

    // target.addBreak();
    target.addPicture(pictureData, pictureType, filename, width, height);
    // target.addBreak(BreakType.PAGE);
}

/**
 * 复制段落,从source到target
 * @param target
 * @param source
 * 
 */
public void copyParagraph(XWPFParagraph target, XWPFParagraph source) {

    // 设置段落样式
    target.getCTP().setPPr(source.getCTP().getPPr());

    // 移除所有的run
    for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
        target.removeRun(pos);
    }

    // copy 新的run
    for (XWPFRun s : source.getRuns()) {
        XWPFRun targetrun = target.createRun();
        copyRun(targetrun, s);
    }

}

/**
 * 复制单元格,从source到target
 * @param target
 * @param source
 * 
 */
public void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
    // 列属性
    if (source.getCTTc() != null) {
        target.getCTTc().setTcPr(source.getCTTc().getTcPr());
    }
    // 删除段落
    for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
        target.removeParagraph(pos);
    }
    // 添加段落
    for (XWPFParagraph sp : source.getParagraphs()) {
        XWPFParagraph targetP = target.addParagraph();
        copyParagraph(targetP, sp);
    }
}

/**
 * 
 * 复制行,从source到target
 * @param target
 * @param source
 * 
 */
public void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
    // 复制样式
    if (source.getCtRow() != null) {
        target.getCtRow().setTrPr(source.getCtRow().getTrPr());
    }
    // 复制单元格
    for (int i = 0; i < source.getTableCells().size(); i++) {
        XWPFTableCell cell1 = target.getCell(i);
        XWPFTableCell cell2 = source.getCell(i);
        if (cell1 == null) {
            cell1 = target.addNewTableCell();
        }
        copyTableCell(cell1, cell2);
    }
}
  /**
   * 复制表,从source到target
   * @param target
   * @param source
   */
  public void copyTable(XWPFTable target, XWPFTable source) {
    // 表格属性
    target.getCTTbl().setTblPr(source.getCTTbl().getTblPr());

    // 复制行
    for (int i = 0; i < source.getRows().size(); i++) {
      XWPFTableRow row1 = target.getRow(i);
      XWPFTableRow row2 = source.getRow(i);
      if (row1 == null) {
        target.addRow(row2);
      } else {
        copyTableRow(row1, row2);
      }
    }
  }

http://blog.csdn.net/liuqz2009/article/details/76639726

解决问题了吗 老哥 我也遇到了这个问题

首先感谢给了我一个复制行例子,然后是我解决了这个问题。分享一下解决方案。我在添加行列之后把这个流生成了具体的word文档文件,然后再把新的word文档文件拿出来再赋值即可