如何在java中的excel文件的同一表中插入值的新行

时间:2014-07-24 07:22:36

标签: java apache-poi

我想在Excel工作表中写入时插入新行。

这是我的代码:

public static void addValuesInWorkbook(String pathAndFileName, String sheetName,
            int rowNum, String valuesString,String delimeter) {
        if(pathAndFileName.length() > 0 && sheetName.length() > 0 && rowNum >= 0 && valuesString.length() > 0 && delimeter.length() > 0)
        {
            String[] colValues= null;
            if("|".equals(delimeter))
                colValues = valuesString.split("\\|");
            else
                colValues = valuesString.split(delimeter);
            int cellnum = 0;
            FileInputStream fsIP;
            try {
                fsIP = new FileInputStream(new File(pathAndFileName));
             //Read the spreadsheet that needs to be updated
            if(rowNum > 0)
            {
                rowNum--;               //As row indexing starts from 0
            }
            HSSFWorkbook wb = new HSSFWorkbook(fsIP); //Access the workbook
            HSSFSheet sheet = wb.getSheet(sheetName);
            HSSFRow row = sheet.getRow(((rowNum>0)?rowNum:0));
            HSSFCell cell=null;
            for(String colValue:colValues)
            {   if(row!=null){
                cell = row.getCell(cellnum);
                if(cell==null)
                    cell = row.createCell(cellnum);
                }
            else if (row == null)
                {
                    row =sheet.createRow(rowNum);                   //Create a row if it does not exist
                    cell = row.createCell(cellnum);
                }

                cell.setCellValue(colValue);
                cellnum++;
            }
            fsIP.close(); //Close the InputStream

            FileOutputStream output_file =new FileOutputStream(new File(pathAndFileName));  //Open FileOutputStream to write updates

            wb.write(output_file); //write changes

            output_file.close();  //close the stream  
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }

        }

使用标题插入值(列名称 )但是每当插入新行时,它都会替换旧的行值。

请帮忙。

1 个答案:

答案 0 :(得分:4)

这是因为您在已有值的行索引上创建新行。 您需要将现有行向下移动1行,因此要插入行的索引是“空闲”。

换句话说,“createRow()”将始终创建一个新行和索引x,无论是否已经存在。

使用sheet.shiftRows(..) 看到: https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#shiftRows(int,int,int)

第一个参数应该是要插入内容的行的索引,第二个参数应该是最后一行的编号,第三个应该是1.因此从x开始的所有行都将向下移动一行你可以在x。

插入你的行

示例:每一行都是包含内容的行。

1 ----------------------
2 ----------------------
3 ---------------------- < you want to insert a new row at index 3
4 ----------------------
5 ----------------------
6 ----------------------
7 ----------------------

shiftRows(3,7,1)将执行此操作:

1 ----------------------
2 ----------------------
3 empty row 
4 ---------------------- < your "old" row #3
5 ----------------------
6 ----------------------
7 ----------------------
8 ----------------------

createRow(3),设置单元格值:

1 ----------------------
2 ----------------------
3 ////////////////////// < your new row #3 witht he new content
4 ---------------------- < your "old" row #3
5 ----------------------
6 ----------------------
7 ----------------------
8 ----------------------