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を使用します。