Jacob项目:https://sourceforge.net/projects/jacob-project/
转自:https://blog.csdn.net/ZY_extreme/article/details/80019009
转自:http://www.360doc.com/content/14/0310/11/12385684_359224303.shtml
转自:https://blog.csdn.net/ZY_extreme/article/details/80007232
转自:https://www.cnblogs.com/vczh/p/5692527.html
转自:https://blog.csdn.net/javadakangxiaobai/article/details/83422396
转自:https://blog.csdn.net/a0701302/article/details/62236470
/** 2018年4月20日 **/ import com.jacob.com.*; import com.jacob.activeX.*; public class ReadExcel { private static ActiveXComponent xl; private static Dispatch workbooks = null; private static Dispatch workbook = null; private static Dispatch sheet = null; private static String filename = null; private static boolean readonly = false; public static void main(String[] args) { String file = "E:\\frequently\\study\\ex.xlsx"; OpenExcel(file, false);// false为不显示打开Excel SetValue("1","A1","Value","2"); System.out.println(GetValue("基础设施情况","G10")); CloseExcel(false); } // 打开Excel文档 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) }, // 是否以只读方式打开 new int[1]).toDispatch(); } catch (Exception e) { e.printStackTrace(); } } // 关闭Excel文档 private static void CloseExcel(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[] {}); } } // 写入值--以编号读写sheet private static void SetValue(String sheetItem ,String position, String type, String value) { // sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch(); Dispatch sheets = Dispatch.get(workbook, "Sheets").toDispatch(); // 以编号读写sheet sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[] { new String(sheetItem) }, new int[1]) .toDispatch(); Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position }, new int[1]) .toDispatch(); Dispatch.put(cell, type, value); } // 读取值--以名称读写sheet private static String GetValue(String sheetItem,String position) { // sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch(); Dispatch sheets = Dispatch.get(workbook, "Sheets").toDispatch(); // 以名称读写sheet sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[] { new String(sheetItem) }, new int[1]) .toDispatch(); Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position }, new int[1]) .toDispatch(); String value = Dispatch.get(cell, "Value").toString(); return value; } }