Created
July 19, 2024 07:26
-
-
Save muhdkhokhar/3f68886308a7b0e88eb0d3080e6e8b5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.jacob.activeX.ActiveXComponent; | |
import com.jacob.com.Dispatch; | |
import com.jacob.com.Variant; | |
public class UpdateExcel { | |
public static void main(String[] args) { | |
// Create an instance of the Excel application | |
ActiveXComponent excelApp = new ActiveXComponent("Excel.Application"); | |
try { | |
// Make the Excel application visible | |
excelApp.setProperty("Visible", new Variant(true)); | |
// Get the active workbook | |
Dispatch activeWorkbook = excelApp.getProperty("ActiveWorkbook").toDispatch(); | |
// Get the active worksheet | |
Dispatch activeSheet = Dispatch.get(activeWorkbook, "ActiveSheet").toDispatch(); | |
// Specify the cell to update (e.g., A1) | |
Dispatch cell = Dispatch.invoke(activeSheet, "Range", Dispatch.Get, new Object[] {"A1"}, new int[1]).toDispatch(); | |
// Update the cell value | |
Dispatch.put(cell, "Value", "Updated Value"); | |
// Save the workbook | |
Dispatch.call(activeWorkbook, "Save"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
// Quit the Excel application | |
excelApp.invoke("Quit"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment