이 자습서에서는 간단한 코드를 사용하여 Java에서 수식을 제거하지만 Excel에서 데이터를 유지하는 방법을 설명합니다. Excel 파일을 로드한 다음 각 셀에 개별적으로 액세스하여 수식을 제거하도록 처리하는 방법에 대한 세부 정보를 제공합니다. 결국 이 파일을 그대로 또는 XLSX 파일과 같은 다른 형식으로 디스크에 저장할 수 있습니다.
수식을 제거하지만 Java에서 Excel의 데이터를 유지하는 단계
- Maven 저장소에서 Aspose.Cells 라이브러리에 대한 참조를 프로젝트에 추가합니다.
- 프로그램에서 가져오기를 사용하여 Workbook 및 Cell 클래스에 대한 참조 추가
- 수식을 제거할 통합 문서 로드
- 수식이 제거될 대상 워크시트의 셀에 대한 참조 가져오기
- 셀 참조를 사용하여 기존 값을 임시 변수에 저장
- 대상 셀에서 수식을 비워 두십시오.
- 임시 변수에서 셀 값 설정
- 데이터만 있는 수식 없이 출력 파일 저장
이 단계를 사용하면 대상 Excel 파일의 워크시트에 액세스한 다음 수식이 제거될 원하는 셀에 대한 참조를 얻을 수 있습니다. 이 작업을 수행하는 동안 먼저 임시 변수에 값을 저장한 다음 공식을 제거합니다. 마지막으로 동일한 셀에 원래 값이 다시 설정됩니다.
Java에서 데이터를 삭제하지 않고 Excel에서 수식을 삭제하는 코드
import com.aspose.cells.Cell; | |
import com.aspose.cells.License; | |
import com.aspose.cells.Workbook; | |
public class HowToRemoveFormulaButKeepDataInExcelInJava { | |
public static void main(String[] args) throws Exception { //main function for HowToRemoveFormulaButKeepDataInExcelInJava | |
// Initialize a license to avoid trial version watermark in the output file after removing formulas | |
License license = new License(); | |
license.setLicense("Aspose.Cells.lic"); | |
// Load the Excel file from which formula is to be removed | |
Workbook excelWorkbookWithFormula = new Workbook("SampleExcelWithFormula.xlsx"); | |
// Get a reference of the cell where formula is to be removed | |
Cell cellWithFormula = excelWorkbookWithFormula.getWorksheets().get(0).getCells().get("C1"); | |
// Store the value in a temporary variable for later use | |
Object tempData = cellWithFormula.getValue(); | |
// Remove the formula by setting its value empty | |
cellWithFormula.setFormula(""); | |
// Save the value back from the temporary variable | |
cellWithFormula.setValue(tempData); | |
// Save the workbook with data without the formula | |
excelWorkbookWithFormula.save("WorkbookWithDataOnly.xlsx"); | |
} | |
} |
위의 샘플 코드는 수식을 제거하지만 셀 단위로 Java에서 Excel의 데이터를 유지하는 방법을 보여줍니다. 그러나 대용량 파일이 있고 전체 통합 문서에서 모든 수식을 한 번에 제거하려는 경우 다음 샘플 코드에 표시된 대로 워크시트의 Cell 컬렉션에서 removeFormulas()를 호출하면 됩니다.
Java에서 데이터를 삭제하지 않고 Excel에서 모든 수식을 제거하는 코드
import com.aspose.cells.License; | |
import com.aspose.cells.Workbook; | |
import com.aspose.cells.Worksheet; | |
public class HowToRemoveAllFormulasButKeepDataInExcelInJava { | |
public static void main(String[] args) throws Exception { //main function for HowToRemoveAllFormulasButKeepDataInExcelInJava | |
// Initialize a license to avoid trial version watermark in the output file after removing all formulas | |
License license = new License(); | |
license.setLicense("Aspose.Cells.lic"); | |
// Load the target workbook from which all formulas are to be removed | |
Workbook workbookWithFormula = new Workbook("WorkbookWithFormulas.xlsx"); | |
// Iterate through all the worksheets in the workbook to remove formulas | |
for(Object object : workbookWithFormula.getWorksheets()) | |
{ | |
Worksheet worksheet = (Worksheet)object; | |
worksheet.getCells().removeFormulas(); | |
} | |
// Save the resultant workbook having data only | |
workbookWithFormula.save("WorkbookWithoutFormulas.xlsx"); | |
} | |
} |
Interop 또는 MS Excel과 같은 다른 타사 도구나 소프트웨어는 이 코드를 실행하는 데 필요하지 않습니다. 또한 출력 파일을 PDF와 같은 다른 형식으로 저장하려는 경우 Java의 Excel에서 PDF를 생성하는 방법에 대한 문서를 참조할 수 있습니다.