1, download the Jacob package: http://danadler.com/jacob/
2, installation configuration
Unzip Jacobbin_17.zip
(1) The directory where Jacob.dll is located in PATH.
(2) Jacob.jar is placed in ClassPath
3, use Jacob to perform Excel control, below is a test program code I wrote
Import com.jacob.com. *; import com.jacob.activex. *;
class ExcelTest {private static ActiveXComponent xl; private static Object workbooks = null; private static Object workbook = null; private static Object sheet = null; private static String filename = null; private static boolean readonly = false; public static void main (String [ ] args) {string file = "f: //java/test.xls"; OpenExcel (file, false); // False is not displayed Open Excel SetValue ("A1", "Value", "2"); System.out.println (GetValue)); CloseExcel (False);} // Open Excel Document Private Static Void OpenExcel (String File, Boolean F) {Try {FileName = file; xl = New ActiveXComponent ("Excel" .Application "); XL.SetProperty (" visible ", new variant (f)); workbooks = xl.getProperty (" Workbooks "). Todispatch (); Workbook = dispatch.invoke (Workbooks," Open ", Dispatch.Method New Object [] {filename, new variant (false), NEW VARIANT (READONLY)}, // Do you open new int [1]) .todispatch () ();} catch (exception e) {E.PrintStackTrace ();}} // Close Excel Document Private Static Void Closexcel (Boolean F) {Try {Dispatch.call (Workbook, "Save"); Dispatch.call (Workbook, "Close", New Variant (f));} catch (exception e) {E.PrintStackTrace ();} finally {Xl.invoke ("quit", new variant [] {});}} // write value private static void setValue (string position, string type, string value) {shop = dispatch.get (Workbook, "Activeesheet" .todispatch ();
Object cell = dispatch.invoke (Sheet, "Range", Dispatch.get, new object [] {position}, new int [1]). Todispatch (); Dispatch.put (Cell, Type, Value);} // Read value private static string getValue (String position) {Object Cell = dispatch.invoke (Sheet, "Range", Dispatch.get, new object [] {position}, new int [1]). Todispatch (); string value = Dispatch.get (Cell, "Value"). TOSTRING (); return value;}}