Jacob 读写Excel
JacobExcelUtil
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* Jacob excel Util
*
*/
public class JacobExcelUtil {
private String file;
private boolean readonly = false;
private static String[] ABC = { "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z" };
public JacobExcelUtil(String file) throws FileNotFoundException {
this.file = file;
File f = new File(file);
if(!f.exists()) {
throw new FileNotFoundException();
}
}
/**
* 写入数据
* @param cells
*/
public void putData(List<ExcelCell> cells) throws Exception {
if(cells == null || cells.size() == 0 ) {
return;
}
ActiveXComponent excel = null;
Dispatch workbooks = null;
Dispatch workbook = null;
Dispatch sheet = null;
String filename = null;
// 初始化
ComThread.InitSTA();
// open file
try {
filename = file;
excel = new ActiveXComponent("Excel.Application");
excel.setProperty("Visible", new Variant(false));
workbooks = excel.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(
workbooks,
"Open",
Dispatch.Method,
new Object[] { filename, new Variant(false),
new Variant(readonly) }, // 是否以只读方式打开
new int[1]).toDispatch();
// put data
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
String position = null;
int row = 0;
int col = 0;
int max = 26*26-1;
for (ExcelCell c : cells) {
row = c.getRow();
col = c.getCol();
if (row < 0 || col < 0 || col > max || c.getValue() == null
|| c.getValue().trim().equals("")) {
continue;
}
position = translateLocation(c.getRow(), c.getCol());
setValue(sheet, position, c.getValue());
}
} finally {
// close file
try {
Dispatch.call(workbook, "Save");
Dispatch.call(workbook, "Close", new Variant(false));
} finally {
excel.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
}
/**
* 转换单元格位置
* 最多支持26*26列
* @param i
* @param j
* @return
*/
public String translateLocation(int i, int j) {
String loc = "";
if (i <= 26) {
loc = ABC[i-1] + (j);
} else {
loc = ABC[i/26-1] + ABC[i%26-1] + (j);
}
return loc;
}
/**
* 写入值
*
* @param sheet
* @param position
* @param value
*/
protected void setValue(Dispatch sheet, String position, String value) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
Dispatch.put(cell, "Value", value);
}
/**
* 读取值
*
* @param sheet
* @param position
* @return
*/
protected String GetValue(Dispatch sheet, String position) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
String value = Dispatch.get(cell, "Value").toString();
return value;
}
public static void main(String[] args) {
}
}
ExcelCell
public class ExcelCell {
private int row;
private int col;
private String value;
public ExcelCell() {
}
public ExcelCell(int row, int col, String value) {
this.row = row;
this.col = col;
this.value = value;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
分享到:
相关推荐
运用jacob方式,java操作 excel文件读写 包含内容: 实例,jacob.jar,jacob.dll
纵向读取数据绘制chart 设置线条气泡颜色 黄色 Dispatch seriesChartFillFormat Dispatch get series "Fill" toDispatch ; Dispatch seriesForeColor Dispatch get seriesChartFillFormat &...
jacob调用的是vba宏语言 ,代码只包含部分读取功能,若要进行更多操作请查看autocad vba开发手册,
java读取word,excel等文件的方法,及jacob.jar包。避免了用字节流读取时出现的乱码问题。
此程序借用poi和jacob 包实现了对Excel和Word的读取操作功能
jdk:1.6.0_27 直接把我测试用的java文件压缩...最终结果是,iText创建*.rtf -> jacob转换 -> POI读取*.word。 目前没有找到一个单独类就能完美支持“创建/读取/转换”的方法,另有xml的方法,因为过于繁琐,没有测试。
itext:读取txt和图片;...jxl:读取excel; 其中jxl可运行与windows和Linux下,jacob读取word完美读取, 不会有图片读取不了或者颜色样式丢失问题, 采用的支持jar基本都是最新版, 献给和我一样苦苦追寻的人
从指定文件读取上下标,对替换文件进行选择相应替换,相应控件用jxl,jacob
POI本来相对就不稳定,而且操作word功能较弱.而且操作Word的例子很少,读写excel的倒是蛮多的 。jacob是个不错的选择,也就是java-com桥
对于读写Office文档问题,有用服务器自动化技术(ASP.NET引入Office类型库,Java用Jacob库)读写Word、Excel的,代码复杂、运行稳定性差,并且服务器只能采用Windows平台;有用POI库的,代码复杂、只能读写格式简单...
一种是pio但是他的excel处理很程序 word模块还局限于读取word的文本内容,写word文件就更弱项了 本文介绍的是itext生成rtf文件并保存格式为word 此方案本人已实践过 并已在项目中使用 用到的jar包: iText-2.1.5...
Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。 3:Java2word是一个在java程序中调用 MS ...
该源码递归读取某个文件下的excel和word转成pdf,pdf最后一页若果有空白页会自动去掉。异步把上传到服务器上的excel和word转成pdf
好久以前的了,可能有点乱,见谅哈,压缩解压缩zip大概是Test9和ZipFile这两个类,然后Test9里也可以 解压rar,但是压缩文件为rar只能调用...另OfficeToHtml是实现word,ppt,excel转html的功能,是用jacob这个组件。
另一种是pio但是它是excel处理程序 对于word模块还局限于读取word的文本内容,写word文件就更显弱项了。因此,Itext无疑会比上述两种方式更优,它是一种生成PDF、Word报表的Java组件,专业的人做专业的事
POI读取Word文档比较适合、对于生成文档样式比较难控制,iText操作Excel还可以,对Word的操作功能有限,JACOB操作Word实现复杂,并且无法将服务部署到Linux平台,要求安装office,对于实现固定格式的报表实现困难,...