JCom可以支持打印,支持生成word,生成Excel,并且可以将文本转换成pdf
这里给出生成word和添加表格的代码:import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
public class TestCreatWord {
/**
* @param filePath --文件的路径
* @param text --插入的文本的内容
* @param fontName --字体的名字
* @param fontSize --字号
* @param fontBold --是否加粗
* @param align --对齐方式:comment--0:left,1:center,2:right
*/
public synchronized void createWord(String filePath, String text,
String fontName, String fontSize, Boolean fontBold, int align) {
ReleaseManager rm = new ReleaseManager();
IDispatch docApp = null;
try {
docApp = new IDispatch(rm, "Word.Application");
IDispatch documents = (IDispatch) docApp.get("Documents");
documents.method("add", null);
IDispatch selection = ((IDispatch) docApp.get("Selection"));
IDispatch paragraphFormat = ((IDispatch) selection
.get("ParagraphFormat"));
paragraphFormat.put("Alignment", new Integer(align));// 对齐方式0:left,1:center,2:right
IDispatch font = ((IDispatch) selection.get("Font"));
font.put("name", fontName);
font.put("Size", fontSize);
font.put("Bold", fontBold);
selection.method("TypeText", new Object[] { text });
((IDispatch) docApp.get("ActiveDocument")).method("saveAs",
new Object[] { filePath, new Integer(0) });
} catch (JComException e) {
e.printStackTrace();
} finally {
try {
if (docApp != null) {
((IDispatch) docApp.get("ActiveDocument")).put("Saved",
new Boolean(true));
docApp.method("quit", null);
docApp = null;
}
rm.release();
rm = null;
} catch (JComException e) {
e.printStackTrace();
}
}
}
/**
* @param filePath --文件路径
* @param rowsNum --行数
* @param colsNum --列数
* @param vals --数组
*/
public synchronized void addTable(String filePath, int rowsNum,
int colsNum, String[][] vals) {
ReleaseManager rm = new ReleaseManager();
IDispatch docApp = null;
try {
docApp = new IDispatch(rm, "Word.Application");
IDispatch documents = (IDispatch) docApp.get("Documents");
IDispatch doc = (IDispatch) documents.method("open",
new Object[] { filePath });// open
IDispatch selection = ((IDispatch) docApp.get("Selection"));
selection.method("endKey", new Object[] { new Integer(6) });// 光标到文档末尾
//selection.method("InsertBreak", new Object[] { new Integer(7) });// 插入一个分页符
IDispatch range = (IDispatch) doc.method("Range", new Object[] {
selection.get("start"), selection.get("start") });// 获得一个range,不知道干什么的
range.method("InsertBreak", new Object[] { new Integer(2) });// 插入一个分页符
selection.put("start", ((Integer) selection.get("start")) + 1);//选取的开始点右移一个位置,不知道为什么,但是不加这一行不行,变成整篇文档横排了
// selection = ((IDispatch) docApp.get("Selection"));
range = (IDispatch) doc.method("Range", new Object[] {
selection.get("start"),
((IDispatch) doc.get("Content")).get("end") });//获得一个范围
IDispatch pageSetup = (IDispatch) range.get("PageSetup");//获得页面设置
pageSetup.put("Orientation", new Integer(1));//横排
IDispatch tables = ((IDispatch) doc.get("Tables"));// 得到doc中的表格集合
tables.method("add", new Object[] { selection.get("range"),
rowsNum, colsNum });// 增加一张表
IDispatch table = (IDispatch) tables.method("item",
new Object[] { new Integer(1) });// 获得刚增加的表格
IDispatch rows = ((IDispatch) table.get("rows"));// 得到行集合
for (int i = 1; i <= rowsNum; i++) {
IDispatch row = (IDispatch) rows.method("item",
new Object[] { new Integer(i) });
IDispatch cells = (IDispatch) row.get("Cells");// 单元格集合
for (int j = 1; j <= colsNum; j++) {
IDispatch cell = (IDispatch) cells.method("item",
new Object[] { new Integer(j) });
((IDispatch) cell.get("Range")).put("Text",
vals[i - 1][j - 1]);//为表格中的格子赋值
}
}
((IDispatch) docApp.get("ActiveDocument")).method("saveAs",
new Object[] { filePath, new Integer(0) });
} catch (JComException e) {
e.printStackTrace();
} finally {
try {
if (docApp != null) {
((IDispatch) docApp.get("ActiveDocument")).put("Saved",
new Boolean(true));
docApp.method("quit", null);
docApp = null;
}
rm.release();
rm = null;
} catch (JComException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String [][] aa = new String[10][10];
for (int i = 0; i < aa.length; i++)
{
for (int j = 0; j < aa[i].length; j++)
{
aa[i][j] = "empty";
}
}
new TestCreatWord().addTable("c:\\aa.doc", 10, 10, aa) ;
}
}
使用JCom生成Excel
import java.util.Date;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelRange;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheets;
public class TestJcom{
private static ReleaseManager rm = null; // ReleaseManager相当于一个容器,
//与你机器上的所有JCOM组建交互,根据你传的参数他会去寻找你机器上的所有JCOM能操作的组建;
public static void main(String[] args) throws Exception {
rm = new ReleaseManager();//查找JCOM能够操作的组件
IDispatch appl = null;
try {
System.out.println("EXCEL loading...");
ExcelApplication excel = new ExcelApplication(rm);//创建可加载excel组件
excel.Visible(true);//表示显示操作的excel文件
System.out.println("Version=" + excel.Version());
System.out.println("UserName=" + excel.UserName());
System.out.println("Caption=" + excel.Caption());
System.out.println("Value=" + excel.Value());
ExcelWorkbooks xlBooks = excel.Workbooks();//创建工作薄对象
ExcelWorkbook xlBook = xlBooks.Add();//添加工作薄
ExcelWorksheets xlSheets = xlBook.Worksheets();//获得工作薄中的工作表,返回的是以数组形式存放
ExcelWorksheet xlSheet = xlSheets.Item(2);//选中第2个工作表
xlSheet.Name("new sheet name");//修改当前sheet的名字
ExcelRange xlRange = xlSheet.Cells();//得到工作表的单元格
//向指定的单元格中添加值
xlRange.Item(1, 1).Value("第1行,第1列");
xlRange.Item(1, 2).Value("第1行,第2列");
xlRange.Item(1, 3).Value("第1行,第3列");
xlRange.Item(1, 4).Value("第1行,第4列");
xlRange.Item(1, 5).Value("第1行,第5列");
xlRange.Item(1, 6).Value("第1行,第6列");
xlRange.Item(1, 7).Value("第1行,第7列");
File path = new File("c:/");//创建一个文件对象(.表示当前路径下或者使用(./))
String[] filenames = path.list();//列出该文件加下的所有文件
for (int i = 0; i < filenames.length; i++) {
File file = new File(filenames[i]);//得到目录下当前文件对象
System.out.println("file:"+file);//
xlRange.Item(i + 2, 1).Value(file.getName());//文件的名字
xlRange.Item(i + 2, 2).Value((int) file.length());//**返回文件大小**
xlRange.Item(i + 2, 3).Value(new Date(file.lastModified()));//文件最后更新时间
xlRange.Item(i + 2, 4).Value(file.isDirectory() ? "Yes" : "No");//判断是否是目录
xlRange.Item(i + 2, 5).Value(file.isFile() ? "Yes" : "No");//判断是否是文件
xlRange.Item(i + 2, 6).Value(file.canRead() ? "Yes" : "No");//判断是否可写
xlRange.Item(i + 2, 7).Value(file.canWrite() ? "Yes" : "No");//判断是否可读
}
String expression = "=Sum(B2:B" + (filenames.length + 1) + ")";
System.out.println("计算公式:" + expression);
xlRange.Item(filenames.length + 2, 1).Value("大小合计");
xlRange.Item(filenames.length + 2, 2).Formula(expression);//添加使用的表达式
xlRange.Columns().AutoFit();//可以自动调整列宽以适应文字
// xlSheet.PrintOut();//是否打印该文件
xlBook.SaveAs("testExcel3.xls");//保存在上面的目录下
System.out.println("[Enter]");
System.in.read();
xlBook.Close(false, null, false);
excel.Quit();
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
} finally {
rm.release();
}
}
}
用JCOM实现打印如打印word和Excel等的
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
/**
* @author admin
*/
public class TestPrint {
/**
* @param args
* 打印word
*/
public static void main(String[] args) {
TestPrint tp = new TestPrint();
String path="c:\\1111.doc";
tp.print("Word.Application", "Documents", path);
}
/***
* @param docApplication Application类型
* @param docProperty 文档的属性
* @param filePath 文件的绝对路径
*/
public void print(String docApplication,String docProperty,String filePath){
ReleaseManager rm = new ReleaseManager();
try {
IDispatch docApp = new IDispatch(rm, docApplication);
docApp.put("Visible", new Boolean(false));
IDispatch wdDocuments = (IDispatch) docApp.get(docProperty);
Object[] arglist1 = new Object[1];
arglist1[0] = (Object)filePath;
IDispatch docDocument = (IDispatch) wdDocuments.method("Open",
arglist1);
docDocument.method("PrintOut", null);
docApp.method("Quit", null);
}catch(JComException e){
e.printStackTrace();
}
rm.release();
rm = null;
}
/**
* @param fname 文件的路径名称
* @return
*/
public boolean printExcel(String fname) {
ReleaseManager rm = new ReleaseManager();
try {
ExcelApplication excel = new ExcelApplication(rm);
ExcelWorkbooks xlBooks = excel.Workbooks();
ExcelWorkbook xlBook = xlBooks.Open(fname);
ExcelWorksheet xlSheet = excel.ActiveSheet();
xlSheet.PrintOut();
xlBook.Close(false, null, false);
excel.Quit();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
rm.release();
}
return true;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.lccert.crm.project.ChemProject;
import com.lccert.crm.project.ProjectAction;
/**
* 打印流转单类
* @author eason
*
*/
public class PrintFlowAction {
private static PrintFlowAction instance = null;
private PrintFlowAction() {
}
public static PrintFlowAction getInstance() {
if (instance == null) {
instance = new PrintFlowAction();
}
return instance;
}
/**
* 打印流转单
* @param flowfile
* @param flow
* @return
*/
public synchronized boolean Printflow(String flowfile,Flow flow) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
FileOutputStream fileOut = null;
boolean isok = false;
ChemProject cp = ProjectAction.getInstance().getProjectByRid(flow.getRid());
try {
// 读取文件内容
fs = new POIFSFileSystem(new FileInputStream(flowfile));
wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(1);
HSSFCell cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getRid());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getPid());
row = sheet.getRow(3);
cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getPdtime());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getRptime());
row = sheet.getRow(5);
cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getLevel());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getRptype());
row = sheet.getRow(7);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getTestparent());
row = sheet.getRow(12);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getTestchild());
row = sheet.getRow(21);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getSampledesc());
row = sheet.getRow(34);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getAppform());
row = sheet.getRow(45);
cell = row.getCell((short) 4);
if (cell == null)
cell = row.createCell((short) 4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(flow.getPdtime()));
String icode = "*" + flow.getRid().substring(4,12) + "*";
HSSFHeader header = sheet.getHeader();
header.setCenter(HSSFHeader.fontSize((short) 28)
+ HSSFHeader.font("C39HrP24DmTt", "Normal") + icode);
header.setLeft(HSSFHeader.fontSize((short) 24)
+ HSSFHeader.font("宋体", "常规") + "LC-WS-003" + flow.getFlowtype().substring(0,2));
HSSFFooter footer = sheet.getFooter();
footer.setLeft(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
footer.setRight("共1页,第1页");
// 把内容写入文件
fileOut = new FileOutputStream(flowfile);
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
//String path="D:\\test.doc";
//print("Word.Application", "Documents", path);
if(print("Excel.Application", "Workbooks", flowfile)) {
isok = true;
}
return isok;
}
/***
*
* @param docApplication Application类型
* @param docProperty 文档的属性
* @param filePath 文件的绝对路径
*/
private boolean print(String docApplication,String docProperty,String filePath){
ReleaseManager rm = new ReleaseManager();
boolean isok = false;
try {
IDispatch docApp = new IDispatch(rm, docApplication);
docApp.put("Visible", new Boolean(false));
IDispatch wdDocuments = (IDispatch) docApp.get(docProperty);
Object[] arglist1 = new Object[1];
arglist1[0] = (Object)filePath;
IDispatch docDocument = (IDispatch) wdDocuments.method("Open",
arglist1);
docDocument.method("PrintOut", null);
docApp.method("Quit", null);
isok = true;
}catch(JComException e){
e.printStackTrace();
} finally {
rm.release();
rm = null;
}
return isok;
}
}