Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muhdkhokhar/3f68886308a7b0e88eb0d3080e6e8b5d to your computer and use it in GitHub Desktop.
Save muhdkhokhar/3f68886308a7b0e88eb0d3080e6e8b5d to your computer and use it in GitHub Desktop.
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