import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class test { public static void main(String[] args) throws Exception{ InputStream is = new FileInputStream("C:\\test.xlsx"); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\text.txt"))); for(int i=0;i<xssfWorkbook.getNumberOfSheets();i++){//处理每个sheet XSSFSheet xs=xssfWorkbook.getSheetAt(i); if(xs==null){ continue; } for (int rowNum = 33; rowNum <= xs.getLastRowNum(); rowNum++) { XSSFRow hssfRow = xs.getRow(rowNum); if (hssfRow != null) { XSSFCell number = hssfRow.getCell(2); XSSFCell name = hssfRow.getCell(3); XSSFCell net = hssfRow.getCell(4); XSSFCell pass = hssfRow.getCell(5); if(number==null){ System.exit(0); } System.out.println("\n 序号:"+rowNum+"; 公司:"+getValue(name)+"; 网址:"+getValue(net)+"; \n序列号和密码:"+getValue(pass).replace("\n", "")); writer.write("\r\n序号:"+rowNum+"; 公司:"+getValue(name)+"; 网址:"+getValue(net)+"; \r\n序列号和密码:"+getValue(pass).replace("\n", "")); System.out.println("------------------------------------------------------------------------------------------------------------------------------"); writer.write("\r\n-----------------------------------------------------------------------------------------"); } } } writer.close(); } private static String getValue(XSSFCell 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()); } } }
时间: 2024-10-12 19:42:49