if(path.endsWith(".doc")){ ...... } |
ReleaseManager rm = new ReleaseManager(); IDispatch xlsApp=null; |
xlsApp = new IDispatch(rm, "Excel.Application"); |
IDispatch excel = (IDispatch) xlsApp.get("Workbooks"); |
xlsApp.put("Visible", new java.lang.Boolean(false)); |
IDispatch workbook = (IDispatch) excel.method("open", new Object[] { FilePath(EXCEL存放的路径) }); |
File f = new File(outPath); if (f.exists()) f.delete(); |
workbook.method("saveAs", new Object[] { outPath, new Integer(9) }); |
IDispatch sheets = (IDispatch) workbook.get("Sheets"); |
int sheetsCount = Integer.parseInt(sheets.get("Count").toString()); |
for(int sheetInx=1;sheetInx <= sheetsCount;sheetInx++) { IDispatch sheet = (IDispatch) sheets.get("item", new Object[] { new Integer(sheetInx) }); String sheetName = sheet.get("name").toString(); } |
IDispatch asheet = (IDispatch) xlsApp.get("ActiveSheet"); |
IDispatch row = (IDispatch)cursheet.get("Rows"); int rows = Integer.parseInt(row.get("Count").toString()); |
IDispatch col = (IDispatch)sheet.get("Columns"); int cols = Integer.parseInt(col.get("Count").toString()); |
IDispatch row = (IDispatch) ((IDispatch)cursheet.get("UsedRange")).get("Rows"); int rows = Integer.parseInt(row.get("Count").toString()); |
IDispatch col = (IDispatch) ((IDispatch)cursheet.get("UsedRange")).get("Columns"); int cols = Integer.parseInt(col.get("Count").toString()); |
String cellVal=""; String cellTxt=""; String hasFormula= ""; String cellFormula=""; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ IDispatch cells = (IDispatch) cursheet.get("Cells", new Object[] { new Integer(i+1),new Integer(j+1) }); cellVal = cells.get("value").toString(); cellTxt = cells.get("text").toString(); hasFormula = cells.get("HasFormula").toString(); cellFormula = cells.get("FormulaR1C1").toString(); } } |
asheet.method("Protect", new Object[] { password, new java.lang.Boolean(true), new java.lang.Boolean(true) }); |
asheet.method("Unprotect", new Object[] { password }); |
if (xlsApp != null) { ((IDispatch) xlsApp.get("ActiveWorkbook")).put("Saved", new java.lang.Boolean(true)); //保存工作薄 xlsApp.method("quit", null); //quit 是关闭的是整个EXCEL xlsApp = null; } rm.release(); rm = null; |
workbook.method("close", null); xlsApp.method("quit", null); xlsApp=null; rm.release(); rm = null; |
//rm 就是ReleaseManager的实例 rm.release(); rm = null; 如果你打开EXCEL不加上面两句的话在进程里面还运行着EXCEL.EXE //---------------------------------------------------------------- |
欢迎光临 学考易 (http://xuekao1.com/) | Powered by Discuz! X3 |