不是标准execl的主要原因就是原本的html、xml、txt尾椎的文件,更改成了xls尾椎的文件

面对这种问题,最开始我用了jawin.jar,但是始终会报错,NoClassDefFoundError,网上找了很长时间,才知道仅仅支持win32...

好吧,再找

终于,找到了一个jacob.jar

jacob-1.18.zip

亲测,这个可以把文件转换成你想要的文件,但是有一个缺点,解压后,要把jacob-1.18.dll放到运行的jdk(或jre)的bin里面,这样才能运行

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; public class JacobUtil {
/*
https://technet.microsoft.com/zh-tw/library/bb241279(v=office.12).aspx
查看对应value
*/
/**
*
* @param file1 G:/file1.xls
* @param file2 G:/file2.xls(或G:/file2.xml或G:/file2.html)
* @param type 转换类型
*/
public static void changeFile(String file1,String file2,int type){
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,
new Object[] { file1, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {file2, new Variant(type) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
public static void excelToExecl(String xlsfile, String xmlfile) {
changeFile(xlsfile,xmlfile,56);
} }

这个方法仅仅是为了读取execl,将文件转换成自己输入的文件

比如一个execl.html尾椎改成了execl.xls,文件不是一个标准的execl,poi不能解析,所以我把这个文件通过jacob转换成标准的xls

这些是转换的类型

NAME ValueDescription
xlAddIn18Microsoft Excel 97-2003 Add-In
xlAddIn818Microsoft Excel 97-2003 Add-In
xlCSV6CSV
xlCSVMac22Macintosh CSV
xlCSVMSDOS24MSDOS CSV
xlCSVWindows23Windows CSV
xlCurrentPlatformText-4158Current Platform Text
xlDBF27DBF2
xlDBF38DBF3
xlDBF411DBF4
xlDIF9DIF
xlExcel1250Excel12
xlExcel216Excel2
xlExcel2FarEast27Excel2 FarEast
xlExcel329Excel3
xlExcel433Excel4
xlExcel4Workbook35Excel4 Workbook
xlExcel539Excel5
xlExcel739Excel7
xlExcel856Excel8
xlExcel979543Excel9795
xlHtml44HTML format
xlIntlAddIn26International Add-In
xlIntlMacro25International Macro
xlOpenDocumentSpreadsheet60OpenDocument Spreadsheet
xlOpenXMLAddIn55Open XML Add-In
xlOpenXMLTemplate54Open XML Template
xlOpenXMLTemplateMacroEnabled53Open XML Template Macro Enabled
xlOpenXMLWorkbook51Open XML Workbook
xlOpenXMLWorkbookMacroEnabled52Open XML Workbook Macro Enabled
xlSYLK2SYLK
xlTemplate17Template
xlTemplate817Template 8
xlTextMac19Macintosh Text
xlTextMSDOS21MSDOS Text
xlTextPrinter36Printer Text
xlTextWindows20Windows Text
xlUnicodeText42Unicode Text
xlWebArchive45Web Archive
xlWJ2WD114WJ2WD1
xlWJ340WJ3
xlWJ3FJ341WJ3FJ3
xlWK15WK1
xlWK1ALL31WK1ALL
xlWK1FMT30WK1FMT
xlWK315WK3
xlWK3FM332WK3FM3
xlWK438WK4
xlWKS4Worksheet
xlWorkbookDefault51Workbook default
xlWorkbookNormal-4143Workbook normal
xlWorks2FarEast28Works2 FarEast
xlWQ134WQ1
xlXMLSpreadsheet46XML Spreadsheet
05-11 20:57