注意:此类需要引入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;
}
}
分享到:
相关推荐
java通过jacob实现包,实现与打印机通信打印excel文档,实例简单易用
详细讲述java解析excel代码的一个简单小例子,导入直接可以运行,简单实用。
使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用
java导出excel的小实例程序,是初学java者学习导出EXCEL文档的好案例。
java如何读取excel文档实例.pdf
java通过jxl生成excel实例,读取excel,复制,修改excel文件
java 导出 excel 实例 ,是文档,希望对大家有帮助!
java+Excel+使用方法,java上传excel 详解,java上传excel 实例分析
JAVA读取Excel实例, 解压后需要修改一下文件路径名和Excel文件名, 然后可直接运行
Java POI 导入导出Excel简单实例源代码 该源代码的jar包,参见以下博文附录截图 Java POI导出EXCEL经典实现 Java导出Excel弹出下载框 ...web页面导出Excel文档,路径:http://localhost:8080/poi/export
poi解密excel文件实例代码.zip是java使用poi解密excel文件的实例代码,代码只实现了.xls类型的excel,.xlsx类型的excel只需把HSSF前缀的类更换成XSSF开通的前缀类即可。 本代码所实现的解密是指使用密码解密,不是...
Java Excel 是一开放源码项目,通过它 Java 开发人员可以读取 Excel 文件的内容、创建新的 Excel 文件、更新已经存在的 Excel 文件。使用该 API 非 Windows 操作系统也可以通过纯 Java 应用来处理 Excel 数据表。...
JAVA100例之实例38 操作EXCEL文件
java 对excel 的操作实例,解压后即可用,包括jar文件。
代码实例: String path = ""; byte[] bytes; vo.setCurrentPage(0); vo.setPageSize(Integer.MAX_VALUE); List<ShOutOrderDetail> list = stockOrderController.exportShOutOrderDetail(vo); Map, Object[]> ...
解释:该方法相当于新建了一个excel文件,HSSFWorkbook : excel的文档对象。 2.//创建第一个sheet(页),命名为 page1 Sheet sheet = wb.createSheet("page1"); 解释:该方法相当于在excel文件里新建了一个工作页...
运用jacob方式,java操作 excel文件读写 包含内容: 实例,jacob.jar,jacob.dll
初学者的JAVA作业,poi方法导入excel文件,分析数据并用jfreechart绘制统计图讨论成绩分布,包括完成的程序包,可以直接运行。
Apache POI 是基于 Office Open XML 标准(OOXML)和 ...本代码实例是使用java语言写的poi解密excel文件实例代码,代码只实现了.xls类型的excel,.xlsx类型的excel只需把HSSF前缀的类更换成XSSF开通的前缀类即可。
内含源码和文档说明 示例1将演示如何利用jakarta poi api 创建excel 文档。 示例2将演示如何读取excel文档中的数据。假定在d盘jtest目录下有一个文件名为gongye.xls的excel文件。