欢迎来到天天文库
浏览记录
ID:6039291
大小:64.00 KB
页数:13页
时间:2017-12-31
《在java中处理excel文件》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、在Java中处理Excel文件packagedemo.excel;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjxl.Cell;importjxl.Workbook;importjxl.write.Label;importjxl.write.WritableWorkbook;importorg.apache.poi.hssf.usermodel.HSSFCell;importor
2、g.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;publicclassCreateExcelDemo{publicstaticStringoutFilePOI="E:/excel/testPOI.xl
3、s";publicstaticStringoutFileJXL="E:/excel/testJXL.xls";publicstaticvoidmain(Stringargs[]){createExcelUsePOI();createExcelUseJXL();}publicstaticvoidcreateExcelUseJXL(){try{//createexcelfile&writeinfotoitFileOutputStreamfos=newFileOutputStream(outFileJXL);Writable
4、Workbookwwb=Workbook.createWorkbook(fos);wwb.createSheet("WillTest",0);jxl.write.WritableSheetsht=wwb.getSheet(0);//setcolumnwidthsht.setColumnView(0,10000);sht.setColumnView(6,30000);//addcellLabellabel=newLabel(6,6,"我靠!");sht.addCell(label);wwb.write();wwb.clo
5、se();fos.close();//readinfomationfromexcelfileInputStreamis=newFileInputStream(outFileJXL);jxl.Workbookbook=jxl.Workbook.getWorkbook(is);jxl.Sheetsheet=book.getSheet(0);Cellcell=sheet.getCell(6,6);System.out.println(cell.getContents());is.close();book.close();}c
6、atch(Exceptione){e.printStackTrace();}}publicstaticvoidcreateExcelUsePOI(){shortcellPos=0;try{HSSFWorkbookbook=newHSSFWorkbook();HSSFSheetsheet=book.createSheet("Williamtest");//设置column宽度sheet.setColumnWidth((short)2,(short)2000);sheet.setColumnWidth((short)6,(
7、short)10000);HSSFRowrow=sheet.createRow(4);cellPos=2;HSSFCellcell=row.createCell(cellPos);cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);cell.setCellValue(99.99);cellPos=6;cell=row.createCell(cellPos);cell.setCellType(HSSFCell.CELL_TYPE_STRING);HSSFRichTextStrings
8、tr=newHSSFRichTextString("KKSDLFLSLKDsdfsdfsdfsfsdf");cell.setCellValue(str);FileOutputStreamfos=newFileOutputStream(outFilePOI);book.write(fos);fos.flush();fos.close
此文档下载收益归作者所有