java打印excel文档实例

本文介绍了一个用于打印流转单的Java类实现。该类利用Apache POI库操作Excel文件,填充特定字段,并通过COM接口调用Excel应用进行打印。涉及单元格定位、日期格式化等细节。
注意:此类需要引入poi.jar和jcom.jar,并且需要把jcom.dll这个文件放到jdk的bin目录下和tomcat的bin目录下。


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;
}

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值