2017-07-30 9 views
0

xlsxファイルのセルコンテンツを削除します。 私のコード:apacheを使用してxlsxファイルのCellコンテンツを削除する方法

static void RemoveCell(XSSFSheet mySheet) throws FileNotFoundException, IOException { 
    int rownum = mySheet.getLastRowNum(); 
    for (int i = 0; i < rownum; i++) { 
     Row currentRow = mySheet.getRow(i); 
     Cell cell = currentRow.getCell(0); 
     if (cell.getCellType() != Cell.CELL_TYPE_BLANK) { 
      cell.setCellType(Cell.CELL_TYPE_BLANK); 
     } 
    } 
} 

しかし、それは動作しません。 ありがとう!

答えて

1

Clear cells of contents or formatsに記載されるような要件は、唯一のセルの内容を削除するが、形式やコメントを残っている場合は、動作する必要があり、次の

import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*; 

import java.io.*; 

class ExcelRemoveCellContent { 

static void removeCellContentsColumnA(XSSFSheet sheet) throws Exception { 
    int rownum = sheet.getLastRowNum(); 
    for (int i = 0; i < rownum+1; i++) { 
    XSSFRow row = sheet.getRow(i); 
    if (row != null) { 
    XSSFCell cell = row.getCell(0); 
    if (cell != null) { 
    if (cell.getCTCell().isSetT()) cell.getCTCell().unsetT(); 
    if (cell.getCTCell().isSetV()) cell.getCTCell().unsetV(); 
    } 
    } 
    } 
} 

public static void main(String[] args) throws Exception{ 

    InputStream inp = new FileInputStream("ExcelRemoveCellContent.xlsx"); 
    Workbook workbook = WorkbookFactory.create(inp); 

    Sheet sheet = workbook.getSheetAt(0); 

    removeCellContentsColumnA((XSSFSheet)sheet); 

    workbook.write(new FileOutputStream("ExcelRemoveCellContent.xlsx")); 
    workbook.close(); 

} 
} 

cell.getCTCell()CTCellを返します。その方法のリンクを参照してください。

選択したセルに含まれるすべての内容、形式、およびコメントを消去するには(すべてをクリアする)、単にRow.removeCellを使用します。

関連する問題