问题描述
有没有将TXT转换EXCEl的小程序?急
解决方案
解决方案二:
packagecom.sitech.tools.parseexcel;importjava.io.File;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.IOException;importjava.io.LineNumberReader;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;publicclassCreateBossExcel{/***@paramargs*/privatestaticfinalStringpath="D://youhua//yangshuang";publicvoidcreateExcel(Stringpath)throwsIOException{Filefile=newFile(path);if(file.exists()&&file.isDirectory()){File[]subFile=file.listFiles();for(intj=0;j<subFile.length;j++){create(subFile[j],path);System.out.println("*********************************************"+subFile[j].getName());}}else{System.out.println(path+"doesnotexist!");}}privatevoidcreate(Filesubfile,Stringpath)throwsIOException{Stringfilename=subfile.getName()+".xls";FileOutputStreamfos=newFileOutputStream(filename);HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet();wb.setSheetName(0,filename);HSSFRowrow=null;HSSFCellcell=null;row=sheet.createRow(0);cell=row.createCell((short)0);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("定时时间");cell=row.createCell((short)1);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("定时代码路径");cell=row.createCell((short)2);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("备注");cell=row.createCell((short)3);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("添加人员");cell=row.createCell((short)4);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("是否涉及数据库");cell=row.createCell((short)5);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("是否需要修改");LineNumberReaderlnr=newLineNumberReader(newFileReader(subfile));Stringline=null;intttt=1;Stringstr1="";Stringstr2="";Stringstr3="";while((line=lnr.readLine())!=null){if(line.startsWith("#")){str1=line.substring(line.indexOf("#"));System.out.println(str1);}else{if(line.lastIndexOf("*")==-1){}else{str2=line.substring(0,line.indexOf("*"));str3=line.substring(line.indexOf("/"),line.indexOf(".sh"));}}if(!str1.equals("")&&!str2.equals("")&&!str3.equals("")){row=sheet.createRow(ttt);cell=row.createCell((short)0);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue(str2);cell=row.createCell((short)1);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue(str3+".sh");cell=row.createCell((short)2);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue(str1);cell=row.createCell((short)3);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("");cell=row.createCell((short)4);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("");cell=row.createCell((short)5);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("");ttt++;str1="";str2="";str3="";}}System.out.println("ttt="+ttt);wb.write(fos);fos.close();}publicstaticvoidmain(String[]args)throwsIOException{CreateBossExcelcbe=newCreateBossExcel();cbe.createExcel(path);}}