2014-09-28 25 views
-2

我有一个项目中,我必须从现有的Excel(.xlsx)删除几行,并且我必须从数据库更新几列的要求。更新和删除Excel中的行使用poi与XML的方法

例如,如果我在Excel中有10行,我必须从数据库中删除8行并更新其余两行的2列。最后,我的Excel应该只有两行更新值。

请让我知道如果我能做到这一点,使用POI在java.I知道XML的方法如何使用XML来读取,但我不知道有关更新和删除xml.please帮我在这

+0

这可能有助于http://stackoverflow.com/questions/1834971/removing-a-row-from-an- excel-sheet-with-apache-poi-hssf – 2014-09-28 08:21:31

+0

这就是使用hssf,请让我知道如果我可以用xml做xml方法 – 2014-09-28 08:29:36

+0

为什么选择XML over XSSF?如果您使用SAX方法,我认为它不支持删除行。由于数据分布在多个工作表(sheet.xml,styles.xml和sharedStrings.xml)中,因此很难做到这一点。 – Renjith 2014-09-28 08:48:52

回答

0

你可以使用“row.getCell()。setCellValue()”来设置值。和删除你可以使用“sheet.removeRow()”

0

一个示例代码。我尝试遵循SSCCE原则。

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 


public class Test { 

    /** 
    * @param args 
    * @throws IOException 
    * @throws FileNotFoundException 
    */ 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     String filePath = "test.xls"; 
     HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath)); 
     HSSFSheet sheet = wb.getSheetAt(0); 
     //update the 1st cell in 1st row with a value. 
     updateCell(sheet, 0, 0, 0, "modifedValue"); 
     //delete row 2; 
     removeRow(sheet, 2); 
    } 

    public static void updateCell(HSSFSheet sheet, int sheetNum, int rowNum, int cellNum, String value){ 
     HSSFRow row = sheet.getRow(rowNum); 
     HSSFCell cell = row.getCell(cellNum); 
     cell.setCellValue(value); 
    } 


    public static void removeRow(HSSFSheet sheet, int rowIndex) { 
     int lastRowNum=sheet.getLastRowNum(); 
     if(rowIndex>=0&&rowIndex<lastRowNum){ 
      sheet.shiftRows(rowIndex+1,lastRowNum, -1); 
     } 
     if(rowIndex==lastRowNum){ 
      HSSFRow removingRow=sheet.getRow(rowIndex); 
      if(removingRow!=null){ 
       sheet.removeRow(removingRow); 
      } 
     } 
    } 

} 

更多的例子可在http://poi.apache.org/

被发现卡思提到为约删除行的陷阱的问题。 http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#getRow(org.apache.poi.ss.usermodel.Row)

使用Apache POI,删除行功能将会像在Removing a row from an Excel sheet with Apache POI HSSF

/** 
* Remove a row by its index 
* @param sheet a Excel sheet 
* @param rowIndex a 0 based index of removing row 
*/ 
    public static void removeRow(HSSFSheet sheet, int rowIndex) { 
     int lastRowNum=sheet.getLastRowNum(); 
     if(rowIndex>=0&&rowIndex<lastRowNum){ 
      sheet.shiftRows(rowIndex+1,lastRowNum, -1); 
     } 
     if(rowIndex==lastRowNum){ 
      HSSFRow removingRow=sheet.getRow(rowIndex); 
      if(removingRow!=null){ 
       sheet.removeRow(removingRow); 
      } 
     } 
    } 
+0

如何在xml和execel之间进行转换。 JExcel或POI。你可以在这里看到其他的问题和答案。 http://stackoverflow.com/questions/1900647/how-to-convert-excel-to-xml-using-java?rq=1 – 2014-09-28 09:46:30