/*
* Copyright (c) 2000-2011 TeamDev Ltd. All rights reserved.
* Use is subject to license terms.
*/
import com.jniwrapper.Int32;
import com.jniwrapper.win32.automation.IDispatch;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.com.types.LocaleID;
import com.jniwrapper.win32.excel.*;
import com.jniwrapper.win32.excel.impl.PicturesImpl;
import com.jniwrapper.win32.jexcel.*;
import com.jniwrapper.win32.jexcel.Application;
import com.jniwrapper.win32.jexcel.Workbook;
import com.jniwrapper.win32.jexcel.Worksheet;
import java.io.File;
/**
* <p>This sample demonstrates how to merge workbooks and then insert a picture into
* merged workbook using JExcel.
*
* <p>This sample requires following preset to run successfully:
* <ol><li>the standard jexcel.jar must be replaced with jexcel-full.jar which can be retrieved from
* <a href="https://sites.google.com/a/teamdev.com/jexcel-support/hotfixes"> our hotfixes </a> page </li>
* <li>the MS Excel workbook stored in MS Excel 2003 format with name Other.xls must be present in the
* project working directory</li>
* <li>the JPEG image named logo.jpg must be present in the project working directory</li></ol>
*/
public class MergeAndInsertSample {
public static void main(String[] args) throws Exception{
//Start application
Application application = new Application();
//Create the new workbook
final Workbook workbook = application.createWorkbook("Sample Workbook");
//Mark the existing worksheet
Worksheet activeWorksheet = workbook.getActiveWorksheet();
Cell a1 = activeWorksheet.getCell("A1");
a1.setValue("First");
//Cell object must be released explicitly
a1.release();
//Merge the current workbook with the existing one
workbook.mergeWorkbook(new File("Other.xls"));
// Add picture. Since interfaces returned by native peers are bound to current thread but not
// to Ole thread we need to explicitly run the following action in Ole Message Loop
application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
public void run() {
//Get the active worksheet again because it has been changed after merge
Worksheet activeWorksheet = workbook.getActiveWorksheet();
//Get its native peer to address MS Excel Object model
_Worksheet worksheet = activeWorksheet.getPeer();
//Retrieve the Pictures object
Variant unspecifiedParameter = Variant.createUnspecifiedParameter();
Int32 locale = new Int32(LocaleID.LOCALE_USER_DEFAULT);
IDispatch pictures = worksheet.pictures(unspecifiedParameter, locale);
PicturesImpl iPictures = new PicturesImpl(pictures);
//Insert the image
File file = new File("logo.jpg");
//You have to specify absolute path to image file
BStr bStr = new BStr(file.getAbsolutePath());
iPictures.insert(bStr,unspecifiedParameter);
}
});
// Save workbook. WORKBOOKNORMAL file format constant allows to save workbook in
// MS Excel 2003 format embedding either MS Excel 2007 or MS Excel 2003
workbook.saveAs(new File("Result.xls"), FileFormat.WORKBOOKNORMAL, true);
//Close workbook and quit application
workbook.close(false);
application.close(true);
}
}
|
 Updating...
MergeAndInsertSample.zip (11k) Unknown user, Mar 1, 2011, 6:19 AM
|