扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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 //---------------------------------------------------------------- |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者