本文主要是介绍jacob方式,java实现excel的写数据过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
配置:1,将jacob.jar导入到工程的classpath下 2,把jacob.dll文件拷贝到jdk/bin目录下
package excelTest1;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobExcelSample {
private static ActiveXComponent xl;
private static Dispatch workbooks = null;//
private static Dispatch workbook = null;
private static Dispatch currentSheet = null;// 当前sheet
private static Dispatch sheets = null;// 获得sheets集合对象
private static Dispatch sheet = null;// 未活动sheet
private static String filename = null;
private static boolean readonly = false;
public static void main(String[] args) {
String file = "D://workcontent//excel//jacobExcel.xls";
OpenExcel(file, true);// false为不显示打开Excel
addSheet();// 添加sheet
modifySheetName("new sheet name8");// 修改当前sheet的名字
setValue("A1", "Value", "2");//设值
System.out.println("currentSheet的名字:"+getSheetName());// 得到工作薄的名字
System.out.println("workbook的名字:"+getWorkbookName());// 得到工作薄的名字
System.out.println("sheet的总数:" + getSheetCount());
System.out.println("当前sheet的A1位置的值:" + getValue("A1", currentSheet));
CloseExcel(false);
}
// 添加新的工作表(sheet),(添加后为默认为当前激活的工作表)
private static void addSheet() {
Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add");
}
// 修改当前工作表的名字
private static void modifySheetName(String newName) {
Dispatch.put(getCurrentSheet(), "name", newName);
}
// 得到当前工作表的名字
private static String getSheetName() {
return Dispatch.get(currentSheet, "name").toString();
}
// 得到工作薄的名字
private static String getWorkbookName() {
return Dispatch.get(workbook, "name").toString();
}
// 得到sheets的集合对象
private static Dispatch getSheets() {
sheets = Dispatch.get(workbook, "sheets").toDispatch();
return sheets;
}
// 通过工作表名字得到工作表(未实现)
private static Dispatch getSheetByName(String name) {
//
return sheet;
}
// 通过工作表索引得到工作表(未实现)
private static Dispatch getSheetByIndex(String name) {
//
return sheet;
}
// 得到当前sheet
private static Dispatch getCurrentSheet() {
currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
return currentSheet;
}
// 得到sheet的总数
private static int getSheetCount() {
int count = Dispatch.get(getSheets(), "count").toInt();
return count;
}
// // 写入值
private static void setValue(String position, String type, String value) {
Dispatch cell = Dispatch.invoke(getCurrentSheet(), "Range",
Dispatch.Get, new Object[] { position }, new int[1])
.toDispatch();
Dispatch.put(cell, type, value);
}
// 读取值
private static String getValue(String position, Dispatch currentSheet) {
Dispatch cell = Dispatch.invoke(currentSheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
String value = Dispatch.get(cell, "Value").toString();
return value;
}
// 打开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[] {});
}
}
//以上有两个地方没有实现,希望知道的朋友,留个言,最好把代码附上,非常感谢!
这篇关于jacob方式,java实现excel的写数据过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!