package com.hongk.common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.hongk.file.bean.BillFiles;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 文件相关操作的工具类
* @author Xietz
*
*/
public class FilesUtil {
public static final int EXCEL_HTML = 44;
public static final int WORD_TXT = 7;
public static final int WORD_HTML = 8;
/**
* 次方法验证xls文件里面是否包含关键字:keyword , 返回false | true
*
* @param b
* @param keyword
* @return
*/
public static boolean findExcelContent(BillFiles b,String keyword){
// jxl.Workbook book;
boolean result = false;
try {
if(b == null || b.getXlsUrl() == null || "".equals(b.getXlsUrl()) || keyword == null || "".equals(keyword)){
return false;
}
File file = new File(b.getXlsUrl());
if(!file.exists()){
return false;
}
String fileName = file.getName().toLowerCase();// 统一转成小写做查找
keyword = keyword.toLowerCase(); 统一转成小写做查找 模糊大小写查询
if(fileName.indexOf(keyword) != -1){ //如果文件名包括了关键字也要列出
return true;
}
//如果需要检索文件内容. 把下面的注释打开
// book = Workbook.getWorkbook(file);
// int shets = book.getNumberOfSheets();
// for(int k = 0 ; k < shets ; k ++){
// jxl.Sheet sheet=book.getSheet(k);
System.out.println("文件:"+b.getName()+"sheet="+k+";name="+sheet.getName());
// if(sheet.getName().indexOf(keyword) != -1){ // 如果sheet里面包含了关键字也需要显示
// return true;
// }
// int cols=sheet.getColumns();//5 列。A,B,C,D,E
// int rows=sheet.getRows();//37行
// jxl.Cell cell = null;
// for(int i = 1 ; i < rows ; i++){
// for(int j = 0; j < cols ; j++){
// cell = sheet.getCell(j, i);
// String cellContent = cell.getContents();
// if(cellContent != null && !"".equals(cellContent)){
System.out.println("文件:"+b.getName()+"行i="+(i+1)+";列j="+(j+1)+";cell.getContents()="+cell.getContents());
// if(cell.getContents().indexOf(keyword) != -1){ // 只要内容里面包含了关键字也需要显示
// return true;
// }
// }
// }
// }
// }
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
/**
* EXCEL转HTML
* * 首先下载Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未测试),与先前的Jacob1.7差别不大
1、将压缩包解压后,Jacob.jar添加到Libraries中;
2、将Jacob.dll放至“WINDOWS/SYSTEM32”下面。
然后:
java.library.path=C:\Program Files\java\jdk1.5.0_13\bin;C:\apache-tomcat-5.5.33\bin
1) 把jacob.dll在 C:\Program Files\Java\jdk1.5.0_08\bin、C:\Program Files\Java\jdk1.5.0_08\jre\bin、C:\WINDOWS\system32 目录下各.放一份
2) 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。.
3) 运行项目即可编译通过.
* @param xlsfile EXCEL文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile)
{
File file = new File(htmlfile);
if(file.exists()){
if(!file.delete()){
return;
}
}
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
app = null;
// 针对windows下面
// Process process;
// int pid = 0;
// try {
// process = Runtime.getRuntime().exec("tasklist");
// Scanner in = new Scanner(process.getInputStream());
// while (in.hasNextLine()) {
// String p = in.nextLine(); // 打印所有进程 System.out.println(p);
// if (p.contains("EXCEL.EXE")) {
// StringBuffer buf = new StringBuffer();
// for (int i = 0; i < p.length(); i++) {
// char ch = p.charAt(i);
// if (ch != ' ') {
// buf.append(ch);
// }
// }
// // 打印pid,根据pid关闭进程
// System.out.println("excel.exe进程id="+buf.toString().split("Console")[0].substring("EXCEL.EXE".length()));
// pid = Integer.parseInt(buf.toString().split("Console")[0].substring("EXCEL.EXE".length()));
// Runtime.getRuntime().exec("tskill" + " " + pid);
// }
// }
//
// } catch (IOException e) {
// e.printStackTrace();
// }
ComThread.Release();
}
}
// 复制文件
public static void copyFile(String sourceFilePath, String targetFilePath) throws IOException {
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
public static void main(String[] args) throws IOException
{
String xlsUrl="C:/orderLines.xls" ;
String htmlUrl="D:/orderLines.xls";
// String htmlUrl2="C:/orderLines2.html";
// excelToXml(new File("C:/GISComputerServ.xls"),"GISComputerServ.xls");
// excelToHtml(xlsUrl,htmlUrl);
copyFile(xlsUrl,htmlUrl);
try {
//测试转换类型
// for (int i = 1; i < 50; i++) {
// String toFile = "c:\\test\\s" + i;
// EXCEL_HTML = i;
// excelToHtml(xlsUrl, toFile);
// }
// com.hongk.common.IOCVUtils.changeEncoding(htmlUrl,htmlUrl2,"gb2312","utf-8");
} catch (Exception e) {
e.printStackTrace();
}
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
// String date = sdf.format(new Date());
// System.out.println(date);
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.hongk.file.bean.BillFiles;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 文件相关操作的工具类
* @author Xietz
*
*/
public class FilesUtil {
public static final int EXCEL_HTML = 44;
public static final int WORD_TXT = 7;
public static final int WORD_HTML = 8;
/**
* 次方法验证xls文件里面是否包含关键字:keyword , 返回false | true
*
* @param b
* @param keyword
* @return
*/
public static boolean findExcelContent(BillFiles b,String keyword){
// jxl.Workbook book;
boolean result = false;
try {
if(b == null || b.getXlsUrl() == null || "".equals(b.getXlsUrl()) || keyword == null || "".equals(keyword)){
return false;
}
File file = new File(b.getXlsUrl());
if(!file.exists()){
return false;
}
String fileName = file.getName().toLowerCase();// 统一转成小写做查找
keyword = keyword.toLowerCase(); 统一转成小写做查找 模糊大小写查询
if(fileName.indexOf(keyword) != -1){ //如果文件名包括了关键字也要列出
return true;
}
//如果需要检索文件内容. 把下面的注释打开
// book = Workbook.getWorkbook(file);
// int shets = book.getNumberOfSheets();
// for(int k = 0 ; k < shets ; k ++){
// jxl.Sheet sheet=book.getSheet(k);
System.out.println("文件:"+b.getName()+"sheet="+k+";name="+sheet.getName());
// if(sheet.getName().indexOf(keyword) != -1){ // 如果sheet里面包含了关键字也需要显示
// return true;
// }
// int cols=sheet.getColumns();//5 列。A,B,C,D,E
// int rows=sheet.getRows();//37行
// jxl.Cell cell = null;
// for(int i = 1 ; i < rows ; i++){
// for(int j = 0; j < cols ; j++){
// cell = sheet.getCell(j, i);
// String cellContent = cell.getContents();
// if(cellContent != null && !"".equals(cellContent)){
System.out.println("文件:"+b.getName()+"行i="+(i+1)+";列j="+(j+1)+";cell.getContents()="+cell.getContents());
// if(cell.getContents().indexOf(keyword) != -1){ // 只要内容里面包含了关键字也需要显示
// return true;
// }
// }
// }
// }
// }
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
/**
* EXCEL转HTML
* * 首先下载Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未测试),与先前的Jacob1.7差别不大
1、将压缩包解压后,Jacob.jar添加到Libraries中;
2、将Jacob.dll放至“WINDOWS/SYSTEM32”下面。
然后:
java.library.path=C:\Program Files\java\jdk1.5.0_13\bin;C:\apache-tomcat-5.5.33\bin
1) 把jacob.dll在 C:\Program Files\Java\jdk1.5.0_08\bin、C:\Program Files\Java\jdk1.5.0_08\jre\bin、C:\WINDOWS\system32 目录下各.放一份
2) 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。.
3) 运行项目即可编译通过.
* @param xlsfile EXCEL文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile)
{
File file = new File(htmlfile);
if(file.exists()){
if(!file.delete()){
return;
}
}
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
app = null;
// 针对windows下面
// Process process;
// int pid = 0;
// try {
// process = Runtime.getRuntime().exec("tasklist");
// Scanner in = new Scanner(process.getInputStream());
// while (in.hasNextLine()) {
// String p = in.nextLine(); // 打印所有进程 System.out.println(p);
// if (p.contains("EXCEL.EXE")) {
// StringBuffer buf = new StringBuffer();
// for (int i = 0; i < p.length(); i++) {
// char ch = p.charAt(i);
// if (ch != ' ') {
// buf.append(ch);
// }
// }
// // 打印pid,根据pid关闭进程
// System.out.println("excel.exe进程id="+buf.toString().split("Console")[0].substring("EXCEL.EXE".length()));
// pid = Integer.parseInt(buf.toString().split("Console")[0].substring("EXCEL.EXE".length()));
// Runtime.getRuntime().exec("tskill" + " " + pid);
// }
// }
//
// } catch (IOException e) {
// e.printStackTrace();
// }
ComThread.Release();
}
}
// 复制文件
public static void copyFile(String sourceFilePath, String targetFilePath) throws IOException {
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
public static void main(String[] args) throws IOException
{
String xlsUrl="C:/orderLines.xls" ;
String htmlUrl="D:/orderLines.xls";
// String htmlUrl2="C:/orderLines2.html";
// excelToXml(new File("C:/GISComputerServ.xls"),"GISComputerServ.xls");
// excelToHtml(xlsUrl,htmlUrl);
copyFile(xlsUrl,htmlUrl);
try {
//测试转换类型
// for (int i = 1; i < 50; i++) {
// String toFile = "c:\\test\\s" + i;
// EXCEL_HTML = i;
// excelToHtml(xlsUrl, toFile);
// }
// com.hongk.common.IOCVUtils.changeEncoding(htmlUrl,htmlUrl2,"gb2312","utf-8");
} catch (Exception e) {
e.printStackTrace();
}
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
// String date = sdf.format(new Date());
// System.out.println(date);
}
}