不是标准execl的主要原因就是原本的html、xml、txt尾椎的文件,更改成了xls尾椎的文件
面对这种问题,最开始我用了jawin.jar,但是始终会报错,NoClassDefFoundError,网上找了很长时间,才知道仅仅支持win32...
好吧,再找
终于,找到了一个jacob.jar
亲测,这个可以把文件转换成你想要的文件,但是有一个缺点,解压后,要把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 | Value | Description |
---|---|---|
xlAddIn | 18 | Microsoft Excel 97-2003 Add-In |
xlAddIn8 | 18 | Microsoft Excel 97-2003 Add-In |
xlCSV | 6 | CSV |
xlCSVMac | 22 | Macintosh CSV |
xlCSVMSDOS | 24 | MSDOS CSV |
xlCSVWindows | 23 | Windows CSV |
xlCurrentPlatformText | -4158 | Current Platform Text |
xlDBF2 | 7 | DBF2 |
xlDBF3 | 8 | DBF3 |
xlDBF4 | 11 | DBF4 |
xlDIF | 9 | DIF |
xlExcel12 | 50 | Excel12 |
xlExcel2 | 16 | Excel2 |
xlExcel2FarEast | 27 | Excel2 FarEast |
xlExcel3 | 29 | Excel3 |
xlExcel4 | 33 | Excel4 |
xlExcel4Workbook | 35 | Excel4 Workbook |
xlExcel5 | 39 | Excel5 |
xlExcel7 | 39 | Excel7 |
xlExcel8 | 56 | Excel8 |
xlExcel9795 | 43 | Excel9795 |
xlHtml | 44 | HTML format |
xlIntlAddIn | 26 | International Add-In |
xlIntlMacro | 25 | International Macro |
xlOpenDocumentSpreadsheet | 60 | OpenDocument Spreadsheet |
xlOpenXMLAddIn | 55 | Open XML Add-In |
xlOpenXMLTemplate | 54 | Open XML Template |
xlOpenXMLTemplateMacroEnabled | 53 | Open XML Template Macro Enabled |
xlOpenXMLWorkbook | 51 | Open XML Workbook |
xlOpenXMLWorkbookMacroEnabled | 52 | Open XML Workbook Macro Enabled |
xlSYLK | 2 | SYLK |
xlTemplate | 17 | Template |
xlTemplate8 | 17 | Template 8 |
xlTextMac | 19 | Macintosh Text |
xlTextMSDOS | 21 | MSDOS Text |
xlTextPrinter | 36 | Printer Text |
xlTextWindows | 20 | Windows Text |
xlUnicodeText | 42 | Unicode Text |
xlWebArchive | 45 | Web Archive |
xlWJ2WD1 | 14 | WJ2WD1 |
xlWJ3 | 40 | WJ3 |
xlWJ3FJ3 | 41 | WJ3FJ3 |
xlWK1 | 5 | WK1 |
xlWK1ALL | 31 | WK1ALL |
xlWK1FMT | 30 | WK1FMT |
xlWK3 | 15 | WK3 |
xlWK3FM3 | 32 | WK3FM3 |
xlWK4 | 38 | WK4 |
xlWKS | 4 | Worksheet |
xlWorkbookDefault | 51 | Workbook default |
xlWorkbookNormal | -4143 | Workbook normal |
xlWorks2FarEast | 28 | Works2 FarEast |
xlWQ1 | 34 | WQ1 |
xlXMLSpreadsheet | 46 | XML Spreadsheet |