java可以通过使用jacob.jar包中的方法实现将多个excel合并为一个,代码如下。
注意:该方法可以合成2010版本的excel,2013版本合成的时候可能会出现问题。
其他OFFICE版本(2013,2016)合成excel可以参考这个链接:
通过调用VBA代码合成EXCEL
// 首先将所有需要合成的excel放到combineFilePath路径上
String combineFilePath = "D:\test\combine\";
// excelPatn是合成excel的文件路径
String excelPath = "D:\test2\combineFile.xlsx";
JacobExcelTool jacobexcel = new JacobExcelTool ();
// 判断一下excel版本
String version = jacobexcel.getExcelApplicationVersion();
// 如果是excel2010则可以合成
if (version.equals("14.0")){
jacobexcel.openExcelFile(excelPath);
jacobexcel.copyExcelList(combineFilePath,jacobexcel.workbooks, jacobexcel.sheet);
jacobexcel.closeExcelFile(false);
}
共通JacobExcelTool 定义如下:
public class JacobExcelTool {
public ActiveXComponent activexcomponent;
public Dispatch workbooks = null;
public Dispatch workbook = null;
public Dispatch sheet = null;
public Vector< String > oldSheetName = new Vector< String >();
private HashMap<String,String> fileType=new HashMap<String,String>();
{
fileType.put("txt", "icons/txticon.exe");
fileType.put("doc", "icons/wordicon.exe");
fileType.put("xls", "icons/excelicon.exe");
fileType.put("pdf", "icons/pdficon.ico");
}
private Vector<String> imageType=new Vector<String>();
{
imageType.add("bmp");
imageType.add("jpg");
imageType.add("jpeg");
imageType.add("gif");
}
/**
* @param args
*/
public static void main(String[] args) {
JacobExcelTool jacobexcel=new JacobExcelTool ();
}
public void saveAsExcelFile(String fileName) {
try {
Dispatch.call(sheet, "Select");
File f = new File(fileName);
f.delete();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method,
new Object[] { fileName }, new int[1]);
} catch (Exception e) {
System.out.println("另存为时出错...");
e.printStackTrace();
}
}
public void closeExcelFile(boolean close) {
try {
Dispatch.call((Dispatch) workbook, "Save");
Dispatch.call((Dispatch) workbook, "Close", new Variant(close));
} catch (Exception e) {
System.out.println("保存关闭时出错...");
e.printStackTrace();
} finally {
activexcomponent.invoke("Quit", new Variant[] {});
}
}
public void openExcelFile(String filename,String sheetName){
try {
activexcomponent = new ActiveXComponent("Excel.Application");
activexcomponent.setProperty("Visible", new Variant(false));
workbooks = activexcomponent.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,
new Object[] { filename, new Variant(false), new Variant(false)},// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) workbook,"Sheets").toDispatch();
sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get,new Variant[] { new Variant(sheetName) },
new int[1]).toDispatch();
//激活sheet工作表
Dispatch.call(sheet, "Activate");
} catch (Exception e) {
System.out.println("打开EXCEL时出错...");
e.printStackTrace();
}
}
public void openExcelFile(String filename){
try {
activexcomponent = new ActiveXComponent("Excel.Application");
activexcomponent.setProperty("Visible", new Variant(false));
workbooks = activexcomponent.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,
new Object[] { filename, new Variant(false), new Variant(false)},// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) workbook,"Sheets").toDispatch();
int Count = Dispatch.get( sheets , "Count").toInt();
for(int i = 0; i < Count; i ++ )
{
int SheetIdx = i +1;
Dispatch tmpsheet = Dispatch.invoke( sheets , "Item" , Dispatch.Get , new Object[]{SheetIdx} , new int[1] ).toDispatch();
if( tmpsheet != null )
{
oldSheetName.add( new String( Dispatch.get( tmpsheet , "Name" ).toString() ));
}
}
sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get,new Variant[] { new Variant(1) },
new int[1]).toDispatch();
//激活sheet工作表
Dispatch.call(sheet, "Activate");
} catch (Exception e) {
System.out.println("打开EXCEL时出错...");
e.printStackTrace();
}
}
// 写入值
private void setValue(String position, String type, String value) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position },
new int[1]).toDispatch();
Dispatch.put(cell, type, value);
}
// 写入值
public void insertValue(String position, String type, String value) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position },
new int[1]).toDispatch();
String str=getValue(position);
Dispatch.put(cell, type, str+value);
}
// 读取值
public String getValue(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 String getisDIS(String position) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position },
new int[1]).toDispatch();
String value = Dispatch.get(cell, "Row").toString();
return "";
}
//拷贝行,例如:拷贝行A2,L2到行A5,L5(copyInsertRow("A2","L2","A5","L5"))
public void copyInsertRow(Dispatch srcSheet,String a,String b,Dispatch toSheet,String c,String d){
Dispatch copyRow = Dispatch.invoke(srcSheet, "Range", Dispatch.Get,
new Variant[] { new Variant(a+":"+b)}, new int[1]).toDispatch();
Dispatch.invoke(copyRow, "Copy", Dispatch.Method,
new Variant[] {Variant.VT_MISSING }, new int[1]);
Dispatch pastRow = Dispatch.invoke(toSheet, "Range", Dispatch.Get,
new Variant[] { new Variant(c+":"+d)}, new int[1]).toDispatch();
Dispatch.call(pastRow, "Insert");
}
//拷贝粘贴cell
public void copyPastCell(String x,String y){
Dispatch copyCell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Variant[] { new Variant(x)}, new int[1]).toDispatch();
Dispatch.invoke(copyCell, "Copy", Dispatch.Method,
new Variant[] {Variant.VT_MISSING }, new int[1]);
Dispatch pastCell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Variant[] { new Variant(y)}, new int[1]).toDispatch();
Dispatch.call(pastCell, "Select");
Dispatch.call(sheet, "Paste");
}
//插入图片,例如: insertImages("F21","c:/cc.bmp")
public void insertImages(String picPosition, String picFilePath) {
Dispatch.call(sheet, "Activate");
Dispatch d = Dispatch.invoke(sheet, "Range", Dispatch.Get,new Variant[] { new Variant(picPosition) },
new int[1]).toDispatch();
Dispatch.call(d, "Select");
Dispatch pictures = Dispatch.call(sheet, "Pictures").toDispatch();
Dispatch.call(pictures, "Insert", picFilePath).toDispatch();
Dispatch.call(sheet, "Activate");
}
public void copyFromAnotherExcel(String path,String sheetname,String form_index,String to_index){
JacobEReportTool jacobexcel1=new JacobEReportTool();
jacobexcel1.openExcelFile(path,sheetname);
Dispatch copyCell = Dispatch.invoke(jacobexcel1.sheet, "Range", Dispatch.Get,
new Variant[] { new Variant(form_index)}, new int[1]).toDispatch();
Dispatch.invoke(copyCell, "Copy", Dispatch.Method,
new Variant[] {Variant.VT_MISSING }, new int[1]);
Dispatch.call(sheet, "Activate");
Dispatch pastCell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Variant[] { new Variant(to_index)}, new int[1]).toDispatch();
Dispatch.call(pastCell, "Select");
Dispatch.call(sheet, "Paste");
Dispatch.put((Dispatch) jacobexcel1.activexcomponent, "CutCopyMode", new Variant(false));
jacobexcel1.closeExcelFile(false);
}
public void copySheetFromAnotherExcel(String path, String sheetname, Dispatch workbooks,
Dispatch sheet_to) {
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[] { path, new Variant(false), new Variant(false) },// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) workbook, "Sheets").toDispatch();
Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get,
new Variant[] { new Variant(sheetname) }, new int[1]).toDispatch();
//将sheet复制到 sheet_to后面
Dispatch.invoke(sheet, "Copy", Dispatch.Method, new Variant[] { Variant.VT_MISSING,
new Variant(sheet_to) }, new int[1]);
Dispatch.put((Dispatch) activexcomponent, "CutCopyMode", new Variant(false));
try {
Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
} catch (Exception e) {
System.out.println("保存关闭时出错...");
e.printStackTrace();
}
}
public void copyAnotherExcel(String path, Dispatch workbooks,
Dispatch sheet_to)
{
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[] { path, new Variant(false), new Variant(false) },// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) workbook, "Sheets").toDispatch();
int Count = Dispatch.get( sheets , "Count").toInt();
for(int i = 0; i < Count; i ++ )
{
int SheetIdx = i +1;
Dispatch tmpsheet = Dispatch.invoke( sheets , "Item" , Dispatch.Get , new Object[]{SheetIdx} , new int[1] ).toDispatch();
if( tmpsheet != null )
{
//将tmpsheet复制到sheet_to前面
Dispatch.invoke(tmpsheet, "Copy", Dispatch.Method, new Variant[] { new Variant(sheet_to) ,
Variant.VT_MISSING }, new int[1]);
Dispatch.put((Dispatch) activexcomponent, "CutCopyMode", new Variant(false));
String tmpsheetName = Dispatch.get( tmpsheet , "Name" ).toString();
}
}
try {
Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
} catch (Exception e) {
System.out.println("保存关闭时出错...");
e.printStackTrace();
}
}
public void copyExcelList( String folderPath , Dispatch workbooks,
Dispatch sheet_to )
{
File folder = new File( folderPath );
if( folder.isDirectory() )
{
File[] listFile = folder.listFiles();
for( int fileIdx = 0; fileIdx < listFile.length; fileIdx ++ )
{
if(listFile[fileIdx].getName().startsWith("~$")) continue; //add by xiaolei 20161207
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[] { listFile[fileIdx].getAbsolutePath(), new Variant(false), new Variant(false) },// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) workbook, "Sheets").toDispatch();
int Count = Dispatch.get( sheets , "Count").toInt();
for(int i = 0; i < Count; i ++ )
{
int SheetIdx = i +1;
Dispatch tmpsheet = Dispatch.invoke( sheets , "Item" , Dispatch.Get , new Object[]{SheetIdx} , new int[1] ).toDispatch();
if( tmpsheet != null )
{
//将tmpsheet复制到sheet_to前面
Dispatch.invoke(tmpsheet, "Copy", Dispatch.Method, new Variant[] { new Variant(sheet_to) ,
Variant.VT_MISSING }, new int[1]);
Dispatch.put((Dispatch) activexcomponent, "CutCopyMode", new Variant(false));
String tmpsheetName = Dispatch.get( tmpsheet , "Name" ).toString();
}
}
try {
// Dispatch.call((Dispatch) workbook, "Save");
// Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
} catch (Exception e) {
System.out.println("保存关闭时出错...");
e.printStackTrace();
}/* finally {
activexcomponent.invoke("Quit", new Variant[] {});
}*/
}
for( int i = 0; i < oldSheetName.size() ; i ++ )
{
Dispatch tmpsheets = Dispatch.get((Dispatch) this.workbook, "Sheets").toDispatch();
Dispatch tmpsheet = Dispatch.invoke(tmpsheets, "Item", Dispatch.Get,
new Variant[] { new Variant(oldSheetName.get(i)) }, new int[1]).toDispatch();
Dispatch.call((Dispatch) tmpsheet, "Delete");
}
}
}
public String getExcelApplicationVersion()
{
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
String version = new String( xl.getProperty("Version").toString() );
System.out.println("version=" + xl.getProperty("Version"));
xl.safeRelease();
return version;
}