java简单解析xls文件的方法示例【读取和写入】
本文实例讲述了java简单解析xls文件的方法。分享给大家供大家参考,具体如下:
读取:
importjava.io.*; importjxl.*; importjxl.write.*; importjxl.format.*; classAa{ publicstaticvoidmain(Stringargs[]){ try{ Workbookworkbook=null; try{ workbook=Workbook.getWorkbook(newFile("d:\\a.xls")); }catch(Exceptione){ thrownewException("filetoimportnotfound!"); } Sheetsheet=workbook.getSheet(0); Cellcell=null; intcolumnCount=3; introwCount=sheet.getRows(); for(inti=0;i写入:
importjava.io.*; importjxl.*; importjxl.write.*; importjxl.format.*; classAa{ publicstaticvoidmain(Stringargs[]){ try{ FiletempFile=newFile("d:"+java.io.File.separator+"output00.xls"); System.out.println("d:"+java.io.File.separator+"output00.xls"); WritableWorkbookworkbook=Workbook.createWorkbook(tempFile); WritableSheetsheet=workbook.createSheet("TestCreateExcel",0); //一些临时变量,用于写到excel中 Labell=null; jxl.write.Numbern=null; jxl.write.DateTimed=null; //预定义的一些字体和格式,同一个Excel中最好不要有太多格式 WritableFontheaderFont=newWritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLUE); WritableCellFormatheaderFormat=newWritableCellFormat(headerFont); WritableFonttitleFont=newWritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); WritableCellFormattitleFormat=newWritableCellFormat(titleFont); WritableFontdetFont=newWritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); WritableCellFormatdetFormat=newWritableCellFormat(detFont); NumberFormatnf=newNumberFormat("0.00000");//用于Number的格式 WritableCellFormatpriceFormat=newWritableCellFormat(detFont,nf); DateFormatdf=newDateFormat("yyyy-MM-dd");//用于日期的 WritableCellFormatdateFormat=newWritableCellFormat(detFont,df); //剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中 l=newLabel(0,0,"用于测试的Excel文件",headerFormat); sheet.addCell(l); //addTitle intcolumn=0; l=newLabel(column++,2,"标题",titleFormat); sheet.addCell(l); l=newLabel(column++,2,"日期",titleFormat); sheet.addCell(l); l=newLabel(column++,2,"货币",titleFormat); sheet.addCell(l); l=newLabel(column++,2,"价格",titleFormat); sheet.addCell(l); //adddetail inti=0; column=0; l=newLabel(column++,i+3,"标题"+i,detFormat); sheet.addCell(l); d=newDateTime(column++,i+3,newjava.util.Date(),dateFormat); sheet.addCell(d); l=newLabel(column++,i+3,"CNY",detFormat); sheet.addCell(l); n=newjxl.write.Number(column++,i+3,5.678,priceFormat); sheet.addCell(n); i++; column=0; l=newLabel(column++,i+3,"标题"+i,detFormat); sheet.addCell(l); d=newDateTime(column++,i+3,newjava.util.Date(),dateFormat); sheet.addCell(d); l=newLabel(column++,i+3,"SGD",detFormat); sheet.addCell(l); n=newjxl.write.Number(column++,i+3,98832,priceFormat); sheet.addCell(n); //设置列的宽度 column=0; sheet.setColumnView(column++,20); sheet.setColumnView(column++,20); sheet.setColumnView(column++,10); sheet.setColumnView(column++,20); workbook.write(); workbook.close(); }catch(Exceptione){ } } }更多关于java相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。