标签:
1、读取excel文件
InputStream is = new FileInputStream(filesrc); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
2、获取全部行数
int rows = sheet.getLastRowNum();
3、创建工作簿
Workbook wb = new HSSFWorkbook();//定义一个新的工作簿 FileOutputStream fileOut = new FileOutputStream("d:\\poiDemo.xls"); wb.write(fileOut); fileOut.close();
4、创建sheet页
Workbook wb = new HSSFWorkbook();//定义一个新的工作簿wb.createSheet("FirstSheet");//创建一个sheet页 wb.createSheet("SecondSheet");//创建第二个sheet页 FileOutputStream fileOut = new FileOutputStream("d:\\poiSheetDemo.xls");
5、创建单元格
Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("firstSheet"); Row row = sheet.createRow(0);//创建一行 Cell cell = row.createCell(0);//创建一个单元格 cell.setCellValue(1); row.createCell(1).setCellValue(1.2);//创建一个单元格第二列,值是1.2 row.createCell(2).setCellValue("这是一个字符串");//创建一个单元格第三列,值是1.2 FileOutputStream fileOut = new FileOutputStream("d:\\poiSheet.xls"); wb.write(fileOut); fileOut.close();
6、遍历工作簿
public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("d:\\遍历名单.xls"); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet hssfSheet = wb.getSheetAt(0); if(hssfSheet==null){ return; } //遍历行row for(int rowNum = 0;rowNum<=hssfSheet.getLastRowNum();rowNum++){ HSSFRow hssfRow = hssfSheet.getRow(rowNum); if(hssfRow==null){ continue; } //遍历裂cell for(int cellNum = 0;cellNum<=hssfRow.getLastCellNum();cellNum++){ HSSFCell hssfCell = hssfRow.getCell(cellNum); if(hssfCell==null){ continue; } System.out.print(" "+getValue(hssfCell)); } System.out.println(); } } private static String getValue(HSSFCell hssfCell){ if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){ return String.valueOf(hssfCell.getBooleanCellValue()); }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){ return String.valueOf(hssfCell.getNumericCellValue()); }else{ return String.valueOf(hssfCell.getStringCellValue()); } }
7、文本提取
InputStream is = new FileInputStream("d:\\遍历名单.xls"); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); ExcelExtractor excelExtractor = new ExcelExtractor(wb); excelExtractor.setIncludeSheetNames(false);//不需要sheet页的名字 System.out.println(excelExtractor.getText());
8、单元格合并
Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("sheet1"); Row row = sheet.createRow(1); Cell cell = row.createCell(1); cell.setCellValue("单元格合并"); sheet.addMergedRegion(new CellRangeAddress(1,2,1,2));//起始行,结束行,起始列,结束列
9、读取和重写工作簿
InputStream inp = new FileInputStream("d:\\demo13.xls"); POIFSFileSystem fs = new POIFSFileSystem(inp); Workbook wb = new HSSFWorkbook(fs); Sheet sheet = wb.getSheetAt(0);//获取第一个sheet Row row = sheet.getRow(0); Cell cell =row.getCell(0); if(cell==null){ cell = row.createCell(3); } cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("测试单元格"); FileOutputStream fileOut = new FileOutputStream("d:\\demo13.xls"); wb.write(fileOut); fileOut.close();
标签:
原文地址:http://www.cnblogs.com/sylovezp/p/4193717.html